Python 高亮显示图像的子图像时出现奇怪的重叠块
我有以下图像: 我还有一个文件夹,其中包含一些我想在该图像中突出显示的子图像: 下面是我试过的代码:Python 高亮显示图像的子图像时出现奇怪的重叠块,python,image,opencv,image-processing,scikit-image,Python,Image,Opencv,Image Processing,Scikit Image,我有以下图像: 我还有一个文件夹,其中包含一些我想在该图像中突出显示的子图像: 下面是我试过的代码: 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) a
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图像创建了这些块,它们应该生成我不重叠的块。以下是此代码生成的图像:
因此,我在这里犯了什么错误?如何解决此错误
注:代码、图像和子图像是。我认为这是因为您将匹配矩形wrt作为置信值。对于模板,图像中有许多地方的置信度大于0.9,因此产生许多并排的矩形,从而产生重叠块。我建议不要选择置信度大于0.9的矩形,而是选择置信度最大的矩形。当期望同一模板在图像中多次匹配时,通常使用您使用的方法。另一种方法是增加值“0.9”,但我个人不建议这样做。