Ask Your Question
2

supported input types for matchShapes

asked 2013-09-13 11:29:54 -0600

der.helge gravatar image

updated 2013-09-13 11:31:26 -0600

Hi everyone :-)

i am a bit confused with the matchShapes function in opencv with python. This is my sample code:

import cv2
import numpy as np
orig = cv2.imread('test.jpg',1)
orig_bin = cv2.cvtColor(orig, cv2.COLOR_BGR2GRAY)
ret,orig_bin = cv2.threshold(orig_bin,127,255,cv2.THRESH_BINARY_INV)
contours, hierarchy = cv2.findContours(orig_bin,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)

sec = cv2.imread('test2.jpg',1)
sec_bin = cv2.cvtColor(sec, cv2.COLOR_BGR2GRAY)
ret,sec_bin = cv2.threshold(sec_bin,127,255,cv2.THRESH_BINARY_INV)
contours2, hierarchy = cv2.findContours(sec_bin,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)

print cv2.matchShapes(contours,contours2,cv2.cv.CV_CONTOURS_MATCH_I1,0)

i am always getting the error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: contour1 is not a numpy array, neither a scalar

when i am converting the contours to numy arrays, i get this:

    >>> print cv2.matchShapes(np.array(contours),np.array(contours2),cv2.cv.CV_CONTOURS_MATCH_I1,0)
OpenCV Error: Assertion failed (contour1.checkVector(2) >= 0 && contour2.checkVector(2) >= 0 && (contour1.depth() == CV_32F || contour1.depth() == CV_32S) && contour1.depth() == contour2.depth()) in matchShapes, file /opt/local/var/macports/build/_opt_local_var_macports_sources_rsync.macports.org_release_tarballs_ports_graphics_opencv/opencv/work/opencv-2.4.6.1/modules/imgproc/src/contours.cpp, line 1936
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
cv2.error: /opt/local/var/macports/build/_opt_local_var_macports_sources_rsync.macports.org_release_tarballs_ports_graphics_opencv/opencv/work/opencv-2.4.6.1/modules/imgproc/src/contours.cpp:1936: error: (-215) contour1.checkVector(2) >= 0 && contour2.checkVector(2) >= 0 && (contour1.depth() == CV_32F || contour1.depth() == CV_32S) && contour1.depth() == contour2.depth() in function matchShapes

Anyone has a good idea? Also the cv2.cv.CV_CONTOURS_MATCH_I1 gave me same headache ;) - Is there any explanation for being this "complicated"? Thanks for reading, Helge

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
3

answered 2013-12-15 11:02:49 -0600

Guanta gravatar image

It's already been a while since the question was asked, but somehow I still had it as an open tab here on my browser...

So, the problem here is, that you try to compare a list of contours with another list of contours, which is not possible. If you know that you have each time only one contour, try it with: cv2.matchShapes(contours[0],contours2[0],cv2.cv.CV_CONTOURS_MATCH_I1,0)

edit flag offensive delete link more

Question Tools

Stats

Asked: 2013-09-13 11:29:54 -0600

Seen: 2,153 times

Last updated: Dec 15 '13