如何解决突出显示图像的子图像时,奇怪的重叠块
我还有一个文件夹,其中包含一些我要在该图像中突出显示的子图像:
所以,这是我尝试的代码:
import cv2
import numpy as np
import os
def getListOfFiles(dirName):
# create a list of file and sub directories
# names in the given directory
listOfFile = os.listdir(dirName)
allFiles = list()
# Iterate over all the entries
for entry in listOfFile:
# Create full path
fullPath = os.path.join(dirName,entry)
# If entry is a directory then get the list of files in this directory
if os.path.isdir(fullPath):
allFiles = allFiles + getListOfFiles(fullPath)
else:
allFiles.append(fullPath)
return allFiles
img = cv2.imread("139.jpg") #main image
gray_img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
listOfFiles = getListOfFiles("139")
# Iterate in the files
for elem in listOfFiles:
template = cv2.imread(elem,cv2.IMREAD_GRAYSCALE) #subimage
w,h = template.shape[::-1]
result = cv2.matchTemplate(gray_img,template,cv2.TM_CCOEFF_NORMED)
loc = np.where(result >= 0.9)
for pt in zip(*loc[::-1]):
cv2.rectangle(img,pt,(pt[0] + w,pt[1] +h),(0,255,0),3)
cv2.startWindowThread()
# Display an image
cv2.imshow("WINDOW_NAME",img)
cv2.waitKey(0)
cv2.destroyAllWindows()
print("finished")
但是,当我运行它时,有一些重叠的块,我使用scikit-image中的view_as_blocks创建了这些块,这些块应该生成非重叠的块。这是此代码生成的图像:
因此,我在这里的错误是什么?我该如何解决这个错误?
P.s:代码,图像和子图像为here。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。