Detecting an item in a package

Viewed 55

I am not 12 years old, I am just practicing my Python and OpenCV skills. I have two images, one of the “Bundle” or so-called “package”, and another one of one “item”. I want to detect if the item is part of the bundle. I cannot use the template matching algorithm due to its nature and way of functioning, so I chose the ORB keypoint detection method.

Bundle or package item

Now I just apply the simple ORB detection algorithm:

if __name__ == "__main__":
    img1 = cv2.imread(r"graphics\roi5.png")
    img2 = cv2.imread(r"graphics\roi2.png")

    orb = cv2.ORB_create(50)
    kp1, des1 = orb.detectAndCompute(img1, None)
    kp2, des2 = orb.detectAndCompute(img2, None)
    
    matcher = cv2.DescriptorMatcher_create(cv2.DESCRIPTOR_MATCHER_BRUTEFORCE_HAMMING)
    matches = matcher.match(des1, des2, None)
    matches = sorted(matches, key=lambda x: x.distance)

    img3 = cv2.drawMatches(img1, kp1, img2, kp2, matches[:20], None, flags=2)

    cv2.imshow("image", img3)
    cv2.imwrite(r"graphics\keypoint_result.png", img3)
    cv2.waitKey()
    cv2.destroyAllWindows()

This is the result

ORB result

I have tried to crop the item image so I can get a better detection, but the result is not favorable.

Cropped image result

I have realized that the algorithm will match some keypoints, and the result will always be positive, even though is not the case, giving it a false positive (the algorithm does match, but for a human it doesn’t). Should I consider looking further into this method or try to come up with something else?

0 Answers
Related