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