Hello All,
We have a requirement for shape detection and the details is mentioned below. We need to compare a reference shapes ( available in the form of jpg or png) from a other image, containing all other different shapes (provided as a screenshot in an other jpg file) and detect if the image is present or not. If the image is available we need to highlight the draw a rectangle or circle in the screen shot image.
I am pasting the reference image and the screenshot image for your reference.
I would be glad if you could pls let me know the right approach to achieve the above mentioned objective.C:\fakepath\Screenshot.png(/upfiles/1550234205238557.png)
I have tried template method and contour matching and detection
In case of contour method. I see only one image is recognized and the others are detected falsely. Thanking you in advance
import sys import cv2 import numpy as np
Extract all the contours from the image
def get_all_contours(img): ref_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) ret, thresh = cv2.threshold(ref_gray, 127, 255, 0) contours, hierarchy = cv2.findContours(thresh.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE ) return contours
Extract reference contour from the image
def get_ref_contour(img): contours = get_all_contours(img)
for contour in contours:
area = cv2.contourArea(contour)
img_area = img.shape[0] * img.shape[1]
if 0.05 < area/float(img_area) < 0.8:
return contour
if __name__=='__main__':
img1 = cv2.imread(sys.argv[1])
# Input image containing all the different shapes
img2 = cv2.imread(sys.argv[2])
# Extract the reference contour
ref_contour = get_ref_contour(img1)
# Extract all the contours from the input image
input_contours = get_all_contours(img2)
closest_contour = None
min_dist = None
contour_img = img2.copy()
cv2.drawContours(contour_img, input_contours, -1, color=(0,0,0), thickness=3)
cv2.imshow('Contours', contour_img)
# Finding the closest contour
for i, contour in enumerate(input_contours):
# Matching the shapes and taking the closest one using
# Comparison method CV_CONTOURS_MATCH_I3 (second argument)
ret = cv2.matchShapes(ref_contour, contour, 3, 0.0)
print("Contour %d matchs in %f" % (i, ret))
if min_dist is None or ret < min_dist:
min_dist = ret
closest_contour = contour
cv2.drawContours(img2, [closest_contour], 0 , color=(0,0,0), thickness=3)
cv2.imshow('Best Matching', img2)
cv2.waitKey()