我需要用于检测缩放和旋转不变对象的代码。图片中有 8 个笔式驱动器,它们的大小和旋转角度不同。我只能用 matchTemplate() 检测到几个笔式驱动器。我需要使用 SURF、BRIEF 或任何其他可以检测所有 8 个笔式驱动器的算法的代码。我搜索了其他问题,他们只提供想法,但没有 python 代码。
可以使用的包有:
- opencv-contrib(因为 surf,brief 被移到 contrib 包中)
- 蟒蛇3
模板:
输出:
代码 :
import cv2
import numpy as np
image1 = cv2.imread("scale_ri.jpg")
scale_percent = 60 # percent of original size
width = int(image1.shape[1] * scale_percent / 100)
height = int(image1.shape[0] * scale_percent / 100)
dim = (width, height)
# resize image
image1 = cv2.resize(image1, dim, interpolation=cv2.INTER_AREA)
# template matching
# Convert it to grayscale
img_gray = cv2.cvtColor(image1, cv2.COLOR_BGR2GRAY)
# Read the template
template = cv2.imread('template.jpg', 0)
# Store width and heigth of template in w and h
w, h = template.shape[::-1]
# Perform match operations.
res = cv2.matchTemplate(img_gray, template, cv2.TM_CCOEFF_NORMED)
# Specify a threshold
threshold = 0.75
# Store the coordinates of matched area in a numpy array
loc = np.where(res >= threshold)
# Draw a rectangle around the matched region.
num=0
for pt in zip(*loc[::-1]):
cv2.rectangle(image1, pt, (pt[0] + w, pt[1] + h), (0, 255, 255), 2)
cv2.imwrite("output.jpg",image1)
cv2.imshow("output",image1)
cv2.waitKey(0)
编辑:我已将问题更改为比例和旋转不变模板匹配(特征匹配)和对象检测示例:https ://m.youtube.com/watch?v=lcJqinjHb90
我可以使用以下程序检测单个对象,但我需要检测多个对象。
代码:
import numpy as np
import cv2
from matplotlib import pyplot as plt
MIN_MATCH_COUNT = 2
img1 = cv2.imread('template.jpg',0) # queryImage
img2 = cv2.imread('scale_ri.jpg',0) # trainImage
# Initiate SIFT detector
sift = cv2.xfeatures2d.SIFT_create()
# find the keypoints and descriptors with SIFT
kp1, des1 = sift.detectAndCompute(img1,None)
kp2, des2 = sift.detectAndCompute(img2,None)
FLANN_INDEX_KDTREE = 0
index_params = dict(algorithm = FLANN_INDEX_KDTREE, trees = 5)
search_params = dict(checks = 50)
flann = cv2.FlannBasedMatcher(index_params, search_params)
matches = flann.knnMatch(des1,des2,k=2)
# store all the good matches as per Lowe's ratio test.
good = []
for m,n in matches:
if m.distance < 0.7*n.distance:
good.append(m)
if len(good)>MIN_MATCH_COUNT:
src_pts = np.float32([ kp1[m.queryIdx].pt for m in good ]).reshape(-1,1,2)
dst_pts = np.float32([ kp2[m.trainIdx].pt for m in good ]).reshape(-1,1,2)
M, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC,5.0)
matchesMask = mask.ravel().tolist()
h,w = img1.shape
pts = np.float32([ [0,0],[0,h-1],[w-1,h-1],[w-1,0] ]).reshape(-1,1,2)
dst = cv2.perspectiveTransform(pts,M)
img2 = cv2.polylines(img2,[np.int32(dst)],True,255,3, cv2.LINE_AA)
else:
print("Not enough matches are found - %d/%d" % (len(good),MIN_MATCH_COUNT))
matchesMask = None
draw_params = dict(matchColor = (0,255,0), # draw matches in green color
singlePointColor = None,
matchesMask = matchesMask, # draw only inliers
flags = 2)
img3 = cv2.drawMatches(img1,kp1,img2,kp2,good,None,**draw_params)
#plt.savefig("output_pendrive.png")
plt.imshow(img3, 'gray'),plt.show()
输出 :