我试图围绕CV的基础知识。我有一段代码,我使用 for 循环从单个模板中检测到多个对象。
inputImage:Trolley_problem.jpg,humanTemplate:h1.jpg
以下是代码:
import numpy as np
import cv2
# Read the main image
inputImage = cv2.imread('Trolley_problem.jpg')
# Convert it to grayscale
inputImageGray = cv2.cvtColor(inputImage, cv2.COLOR_BGR2GRAY)
# Read the templates
humanTemplate = cv2.imread('h1.jpg')
# Convert it to grayscale
humanTemplateGray = cv2.cvtColor(humanTemplate, cv2.COLOR_BGR2GRAY)
h2,w2 = humanTemplateGray.shape
# Perform match operations.
result1 = cv2.matchTemplate(inputImageGray,humanTemplateGray, cv2.TM_CCOEFF_NORMED)
# Specify a threshold
threshold = 0.75
# Store the coordinates of matched area in a numpy array
loc2 = np.where( result1 >= threshold)
# Draw a rectangle around the matched region.
for pt2 in zip(*loc2[::-1]):
cv2.rectangle(inputImage,pt2, (pt2[0] + w2, pt2[1] + h2), (0,0,255), 1)
# Show result
cv2.imwrite(r'Result Image.jpg', inputImage)
现在,此代码工作正常,它可以检测到6个对象。问题是获取位置的坐标。由于cv2.rectangle在for循环中,因此我只得到6个对象的多个(超过6个)值。
当我打印pt2时,我得到:
((610, 29), (656, 69))
((609, 30), (655, 70))
((610, 30), (656, 70))
((611, 30), (657, 70))
((608, 31), (654, 71))
((609, 31), (655, 71))
((610, 31), (656, 71))
((611, 31), (657, 71))
((612, 31), (658, 71))
((607, 32), (653, 72))
((608, 32), (654, 72))
((609, 32), (655, 72))
((610, 32), (656, 72))
((611, 32), (657, 72))
((612, 32), (658, 72))
((613, 32), (659, 72))
((607, 33), (653, 73))
((608, 33), (654, 73))
((609, 33), (655, 73))
((610, 33), (656, 73))
((611, 33), (657, 73))
((612, 33), (658, 73))
((613, 33), (659, 73))
((607, 34), (653, 74))
((608, 34), (654, 74))
((609, 34), (655, 74))
((610, 34), (656, 74))
((611, 34), (657, 74))
((612, 34), (658, 74))
((613, 34), (659, 74))
((608, 35), (654, 75))
((609, 35), (655, 75))
((610, 35), (656, 75))
((611, 35), (657, 75))
((612, 35), (658, 75))
((609, 36), (655, 76))
((610, 36), (656, 76))
((611, 36), (657, 76))
((610, 37), (656, 77))
((1117, 106), (1163, 146))
((1118, 106), (1164, 146))
((1119, 106), (1165, 146))
((1120, 106), (1166, 146))
((1121, 106), (1167, 146))
((1116, 107), (1162, 147))
((1117, 107), (1163, 147))
((1118, 107), (1164, 147))
((1119, 107), (1165, 147))
((1120, 107), (1166, 147))
((1121, 107), (1167, 147))
((1122, 107), (1168, 147))
((965, 108), (1011, 148))
((966, 108), (1012, 148))
((967, 108), (1013, 148))
((968, 108), (1014, 148))
((969, 108), (1015, 148))
((1042, 108), (1088, 148))
((1043, 108), (1089, 148))
((1044, 108), (1090, 148))
((1079, 108), (1125, 148))
((1080, 108), (1126, 148))
((1081, 108), (1127, 148))
((1082, 108), (1128, 148))
((1083, 108), (1129, 148))
((1116, 108), (1162, 148))
((1117, 108), (1163, 148))
((1118, 108), (1164, 148))
((1119, 108), (1165, 148))
((1120, 108), (1166, 148))
((1121, 108), (1167, 148))
((1122, 108), (1168, 148))
((964, 109), (1010, 149))
((965, 109), (1011, 149))
((966, 109), (1012, 149))
((967, 109), (1013, 149))
((968, 109), (1014, 149))
((969, 109), (1015, 149))
((970, 109), (1016, 149))
((1004, 109), (1050, 149))
((1005, 109), (1051, 149))
((1006, 109), (1052, 149))
((1007, 109), (1053, 149))
((1041, 109), (1087, 149))
((1042, 109), (1088, 149))
((1043, 109), (1089, 149))
((1044, 109), (1090, 149))
((1045, 109), (1091, 149))
((1118, 109), (1164, 149))
((1119, 109), (1165, 149))
((1120, 109), (1166, 149))
((965, 110), (1011, 150))
((966, 110), (1012, 150))
((1004, 110), (1050, 150))
((1005, 110), (1051, 150))
((1006, 110), (1052, 150))
((1007, 110), (1053, 150))
有没有办法消除这么多的位置坐标并只获得6分?
我试过numpy.delete:
a = (pt2[0] == pt2[:,1]) | (pt2[0,:] == pt2[1,:])
vals = numpy.delete(pt2, numpy.where(a), axis=0)
但它显示错误:
tuple indices must be integers, not tuple
因为np.shape(pt2)是(2,)[这是我打印np.shape(pt2)时得到的结果。)
有人可以帮帮我吗?
注意:如果有人有更好的标题,请帮我编辑。
谢谢!
编辑1:
代码的结果:Result Image.jpg