Divyasha Agrawal
Divyasha Agrawal

Reputation: 43

Detecting similar objects in an Image

I have to detect same sized and same colored rectangles with same areas in an image for a project. This is an example image. I don't know how to go about it. I am using OpenCV and python which I am new to.

I tried SIFT and SURF feature descriptors to get the similar features. I also tried template matching but it is not feasible in the case as the trainImage could change. But the main idea is to get those similar rectangles from the image provided. I am using python3 and openCV3. I took this code from the opencv tutorial site.

import numpy as np
import cv2
from matplotlib import pyplot as plt

img1 = cv2.imread('template.jpg',0)          # queryImage
img2 = cv2.imread('input.jpg',0) # trainImage

sift=cv2.xfeatures2d.SIFT_create()

kp1, des1 = sift.detectAndCompute(img1,None)
kp2, des2 = sift.detectAndCompute(img2,None)

# BFMatcher with default params
bf = cv2.BFMatcher()
matches = bf.knnMatch(des1,des2, k=2)

# Apply ratio test
good = []
for m,n in matches:
    if m.distance < 0.75*n.distance:
        good.append([m])

# cv2.drawMatchesKnn expects list of lists as matches.
img3 = cv2.drawMatchesKnn(img1,kp1,img2,kp2,good,None,flags=2)

Image result for project reslut

Upvotes: 2

Views: 1312

Answers (1)

Mark Setchell
Mark Setchell

Reputation: 208052

Here's a simple approach.

generate a list of the unique colours in the image
for each unique colour
    make everything that colour in the image white and everything else black
    run findContours() and compare shapes and sizes
end for

For increased fun, do each colour in a separate thread :-)

Upvotes: 1

Related Questions