Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

in Python trying to use cv2.matchShapes() from OpenCV

I have done a random drawing on a whiteboard and NAO robot has taken a picture and tried to re-create the same drawing.

My drawing:

enter image description here

NAO's drawing:

enter image description here

At this point I would like to write some conclusions about it, specifically I want to extract the contours from both pictures and match the contours using the OpenCV function cv2.matchShapes().

However, I wrote a small Python code script for this and it gives me some errors. Here is the code:

import numpy as np
import cv2

#get the pictures from the forlder
original = cv2.imread('eightgon.jpg')
drawn = cv2.imread('eightgon1.jpg')

#make them gray    
originalGray = cv2.cvtColor(original, cv2.COLOR_BGR2GRAY)
drawnGray = cv2.cvtColor(drawn, cv2.COLOR_BGR2GRAY)

#apply erosion
kernel = np.ones((2, 2),np.uint8)
originalErosion = cv2.erode(originalGray, kernel, iterations = 1)
drawnErosion = cv2.erode(drawnGray, kernel, iterations = 1)

#retrieve edges with Canny
thresh = 175
originalEdges = cv2.Canny(originalErosion, thresh, thresh*2)
drawnEdges = cv2.Canny(drawnErosion, thresh, thresh*2)

#extract contours
originalContours, Orighierarchy = cv2.findContours(originalEdges, cv2.cv.CV_RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)
drawnContours, Drawnhierarchy = cv2.findContours(drawnEdges, cv2.cv.CV_RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)

print cv2.matchShapes(drawnContours,originalContours,cv2.cv.CV_CONTOURS_MATCH_I1, 0.0)

When I run this simple code, it returns me this error:

File "C:/Python27/getResults.py", line 32, in <module>
    ret = cv2.matchShapes(drawnContours,originalContours,cv2.cv.CV_CONTOURS_MATCH_I1, 0.0)
TypeError: contour1 is not a numpy array, neither a scalar

Since the error tells me that the contours should be arrays.. I make a slight change in the code like this:

cnt1 = np.asarray(drawnContours, np.int0)
cnt2 = np.asarray(originalContours, np.int0)
print cv2.matchShapes(cnt1,cnt2,cv2.cv.CV_CONTOURS_MATCH_I1, 0.0)

and in this case it returns me this error: ValueError: setting an array element with a sequence.

What am I doing wrong? Any help is apreciated!

like image 884
Francesco Sgaramella Avatar asked Apr 11 '14 12:04

Francesco Sgaramella


People also ask

Is OpenCV Python and cv2 same?

cv2 (old interface in old OpenCV versions was named as cv ) is the name that OpenCV developers chose when they created the binding generators. This is kept as the import name to be consistent with different kind of tutorials around the internet.

How do I display cv2 images in python?

Python cv2 imshow() Often there would be a requirement to read images and display them if required. To read and display image using OpenCV Python, you could use cv2. imread() for reading the image to a variable and cv2. imshow() to display the image in a separate window.

What is cv2 in OpenCV?

cv2 is the module import name for opencv-python, "Unofficial pre-built CPU-only OpenCV packages for Python".

What is the function of cv2 in Python?

cv2. imshow() method is used to display an image in a window. The window automatically fits to the image size.

What is the correct Ret value for CV2 match shapes?

The return value of cv2.matchShapes is changed now. You should try to use ret > 5 to find the correct contour. I had the same problem, but when I used the biggest ret, my contour can match the correct contour.

How does threshold selection affect OpenCV shape matching?

This probably affects the shape matching in a big way, as OpenCV tries to match "edge for edge", and an open edge map would probably give different results from the compare image. See this python recipe to visualize the impact of threshold selection and an automatic threshold selector .

Why can't I find matchshapes or findcontours in Nao?

Check the opencv version, older release should have different matchShapes or findContours API. Because sadly some NAO software release enclosed some rather old opencv version. Think about updating your NAO Software, or try to get some beta one...


1 Answers

I faced a similar problem. The match shapes function takes a single contour pair, not the whole contour container pair.

cv2.matchShapes(drawnContours[i], originalContours[i], cv2.cv.CV_CONTOURS_MATCH_I1, 0.0)

Hope that helps.

like image 178
Divyanshu Grover Avatar answered Nov 14 '22 11:11

Divyanshu Grover