使用OpenCV进行对象检测

时间:2015-12-15 12:05:13

标签: python image opencv computer-vision

我目前正在尝试学习计算机视觉和使用Python OpenCV 3。

我正在尝试实现的是带有徽标图像的检测器以及可能包含或不包含此徽标的其他图像,并在此图像上找到徽标的所有外观。此图像上的徽标可以缩放,旋转,以不同角度拍摄。这是测试数据集:https://imgur.com/a/aT5g1

前两张图片是'徽标图片',其他图片是搜索徽标的图片,然后是我当前实现返回的结果。最糟糕的情况是最后一张图片,其中带有火星徽标的卡车从“徽标图像”,角度拍摄不同。

这是我当前的实现,非常简单:

def match(im1,im2,detector=sift,iterations=5,matcher=BFmatch,min_matches=10,detector_kwargs={}):
    img1 = load_image(im1)
    img2 = load_image(im2)
    train_img = load_image(im2)

    src_img, kp1, des1 = detector(img1)
    train_img, kp2, des2 = detector(train_img)

    matches = matcher(des1,des2)

    for i in range(iterations):
        if len(matches) < min_matches:
            break
        src_pts = np.float32([ kp1[m.queryIdx].pt for m in matches ]).reshape(-1,1,2)
        dst_pts = np.float32([ kp2[m.trainIdx].pt for m in matches ]).reshape(-1,1,2)

        M, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC,5.0)

        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)
        train_img = cv2.fillPoly(train_img,[np.int32(dst)],True,255)

        train_img, kp2, des2 = detector(train_img)

        matches = matcher(des1,des2)


    img3 = cv2.drawMatches(img1,kp1,img2,kp2,None,None,flags=2)

    plt.imshow(img3),plt.show()

这里我使用SIFT探测器(筛选)和暴力匹配器(BFmatch)。

我很高兴听到有关如何推动这种探测器的建议和建议。

0 个答案:

没有答案