问题描述
我还有一个文件夹,其中包含一些我要在该图像中突出显示的子图像:
所以,这是我尝试的代码:
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 (将#修改为@)