python - 如何在opencv python中执行缩放和旋转不变模板(特征)匹配和对象检测

标签 python opencv image-processing computer-vision opencv-contrib

我需要代码来检测缩放和旋转不变的对象。图片中有 8 个笔式驱动器,它们的大小和旋转角度各不相同。我只能使用 matchTemplate() 检测到几个笔式驱动器。我需要带有 SURF、BRIEF 或任何其他可以检测所有 8 个笔式驱动器的算法的代码。

可以使用的包有:

  • opencv-contrib(因为 surf、brief 被移动到 contrib 包)
  • python3

  • Input image

    模板:

    enter image description here

    输出:

    enter image description here

    代码 :
    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()
    

    输出 :

    enter image description here

    最佳答案

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


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

    关于python - 如何在opencv python中执行缩放和旋转不变模板(特征)匹配和对象检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58920750/

    相关文章:

    python - Pybrain多维数据输入

    python - 当组都在同一元素中时,使用 BeautifulSoup 将 HTML 分成组

    python - 在Python中选择继承哪个父类

    c++ - 如何将数据写入opencv文件夹内的xml文件?

    visual-studio-2010 - 未定义值??[cvFindContours] [轮廓]

    delphi - 如何用delphi查找图片的方向

    python - 基于requests模块将curl执行的post请求转换为python请求

    python - 无法在python中打开VideoCapture

    c - 使用 OpenCV 计算特征向量

    c - 将提取的帧名称存储在数组中 - ffmpeg