首页 > 解决方案 > 如何在opencv python中执行缩放和旋转不变模板(特征)匹配和对象检测

问题描述

我需要用于检测缩放和旋转不变对象的代码。图片中有 8 个笔式驱动器,它们的大小和旋转角度不同。我只能用 matchTemplate() 检测到几个笔式驱动器。我需要使用 SURF、BRIEF 或任何其他可以检测所有 8 个笔式驱动器的算法的代码。我搜索了其他问题,他们只提供想法,但没有 python 代码。

可以使用的包有:

输入图像

模板:

在此处输入图像描述

输出:

在此处输入图像描述

代码 :

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()

输出 :

在此处输入图像描述

标签: pythonopencvimage-processingcomputer-visionopencv-contrib

解决方案


模板匹配的问题在于,如果模板和要查找的对象在大小、旋转或强度方面不完全相同,它将无法工作。假设图像中只有想要检测的对象,这里有一个非常简单的轮廓阈值+过滤方法。

import cv2

image = cv2.imread('1.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (5,5), 0)
thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]

cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
    area = cv2.contourArea(c)
    if area > 10000:
        cv2.drawContours(image, [c], -1, (36,255,12), 3)

cv2.imwrite('thresh.png', thresh)
cv2.imwrite('image.png', image)
cv2.waitKey()

推荐阅读