Templates 使用opencvpython进行模板匹配
我使用OpenCV做模板匹配,它工作得很好,但我面临一些问题Templates 使用opencvpython进行模板匹配,templates,pdf,image-processing,object-detection,opencv-python,Templates,Pdf,Image Processing,Object Detection,Opencv Python,我使用OpenCV做模板匹配,它工作得很好,但我面临一些问题 过度检测:有时,它会多次检测对象 耗时:该程序实际需要500个模板,并尝试与简单图像匹配。这意味着,如果我想匹配1000个图像,那么它将尝试匹配至少500 x 1000次 留下一些未被检测到的图像(我认为这是内存崩溃,因为它一次处理大量图像) 有没有办法避免这个问题 以下是所附代码: def matchtemplatetiff(tifffile, file, outdir): img = np.array(PIL.Ima
def matchtemplatetiff(tifffile, file, outdir):
img = np.array(PIL.Image.open(tifffile))
tmp = np.array(PIL.Image.open(file))
w, h, c = tmp.shape
res = cv2.matchTemplate(img, tmp, cv2.TM_CCOEFF_NORMED)
# Adjust this threshold value to suit you, you may need some trial runs (critical!)
threshold = 0.4
loc = np.where(res >= threshold)
# create empty lists to append the coord of the
lspoint = []
lspoint2 = []
count = 0
font = cv2.FONT_HERSHEY_SIMPLEX
for pt in zip(*loc[::-1]):
# check that the coords are not already in the list, if they are then skip the match
if pt[0] not in lspoint and pt[1] not in lspoint2:
# draw a yellow boundary around a match
rect = cv2.rectangle(img, pt, (pt[0] + h, pt[1] + w), (0, 0, 255), 3)
size =w * h * (2.54/400) *( 2.54/400)
cv2.putText(rect, "{:.1f}cm^2".format(size), (pt[0] + h, pt[1] + w), font, 4,
(0, 0, 255), 3)
# data rows of csv file
rows = 0
rows = [ [tifffile, h, w , pt[0] + h, pt[1] + w, size]]
# name of csv file
filename = "records.csv"
# writing to csv file
with open(filename, 'a', newline='') as csvfile:
# creating a csv writer object
csvwriter = csv.writer(csvfile)
# writing the fields
csvwriter.writerow(fields)
# writing the data rows
csvwriter.writerows(rows)
for i in range(((pt[0]) - 9), ((pt[0]) + 9), 1):
# append the x cooord
lspoint.append(i)
for k in range(((pt[1]) - 9), ((pt[1]) + 9), 1):
# append the y coord
lspoint.append(k)
# count the number of matches
count += 1
else:
continue
print("total objects found ", count)
PIL.Image.fromarray(img, 'RGB').save(os.path.join(outdir, os.path.basename(tifffile)))
你是不是一意孤行?我的一位同事在过去使用yolov3()进行多目标检测,问题是我需要在一本书中检测巴基斯坦地图。internet上没有可用的数据集。我还尝试搜索矩形形状数据集(一个可以检测矩形的模型),因为所有这些贴图都在一个矩形框中。但不幸的是,没有任何.Hm,可能通过灰度匹配来减少工作量。。。然而,我不知道这会如何影响过度检测。你能更详细地描述你正在处理的问题吗?也许还有其他的想法,我不认为灰度是个好主意。该程序几乎可以将300个tiff文件转换为灰度,将其模板转换为灰度,并将其临时存储在某个地方。这可能不是一个好的选择。基本上,我已经扫描了几页书作为输入文件(巴基斯坦的蝴蝶),我想检测地图和它们的直角坐标。我从预处理步骤中得到了它们的模板,我想使用它们的模板检测书中的地图,并将坐标值保存在xml表中。我从您的代码中得到了可以预先准备模板的信息(因为您正在对目录进行全局化以获取图像文件)。我认为这是一项一次性任务,但如果这是每次都必须在飞行中发生的事情,我可以理解你的观点。你能举一些过度检测的例子吗?阈值0.4的值从何而来?