首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >opencv python中如何实现缩放和旋转不变模板(特征)匹配和对象检测

opencv python中如何实现缩放和旋转不变模板(特征)匹配和对象检测
EN

Stack Overflow用户
提问于 2019-11-18 18:25:42
回答 1查看 9.8K关注 0票数 7

我需要的代码,以检测对象,是规模和旋转invariant.There是8笔驱动器在图片中,是根据大小和旋转角度变化。我只能用matchTemplate() .I检测到很少的钢笔驱动器,需要使用SURF、SURF或任何其他算法来检测所有8支钢笔驱动器。我搜索了其他问题,它们只提供了一些想法,但是没有用于python的代码。

可以使用的包有:

  • opencv-contrib(since冲浪,简讯被移到contrib package)
  • python3

模板:

产出:

代码:

代码语言:javascript
运行
复制
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

我能够用下面的程序检测单个对象,但我需要检测多个对象。

代码:

代码语言:javascript
运行
复制
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()

产出:

EN

回答 1

Stack Overflow用户

发布于 2019-11-18 20:42:40

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

代码语言:javascript
运行
复制
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()
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58920750

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档