Using Python OpenCV Moments Function With An Image
I am trying to use the Python opencv function Moments()
on a grayscale image, but I receive the following TypeError
:
>>> img = cv.LoadImage('example_image.jpg', cv.CV_LOAD_IMAGE_GRAYSCALE)
>>> moments = cv.Moments(img)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: Argument '' must be CvSeq, CvArr, or a sequence of numbers
>>>
I am confident this usage is correct as it is demonstrated in the opencv docs here, where GetHuMoments()
uses the results from Moments()
.
I believe I have opencv and num开发者_开发技巧py installed correctly, as I have been successfully using them for many other things, and I encounter this on both OS X 10.6 and Red Hat 6.
The same question is posed in the opencv user group, but I don't want to convert the image to a contour first as the reply instructs.
Looks like you need to force conversion from IplImage to cvMat
There was a previous question "Capture Image as Array with Python OpenCV"
Capture Image as Array with Python OpenCV
e.g for me
>>> mm=cv.Moments(img)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: Argument '' must be CvSeq, CvArr, or a sequence of numbers
>>> mat=cv.GetMat(img)
>>> mm=cv.Moments(mat)
>>> mm.m00
181428.0
Which python and opencv version are you using? I am getting working results on python 2.6.6 and opencv 2.1.0
~/Desktop$ ./opencv-test.py
> `enter code here`/home/tyndyll/Desktop/opencv-test.py(8)<module>()
-> img = cv.LoadImage( 'iron-maiden-live-after-death-iron-maiden_1920x1200_79442.jpg', cv.CV_LOAD_IMAGE_GRAYSCALE )
(Pdb) n
> /home/tyndyll/Desktop/opencv-test.py(9)<module>()
-> moments = cv.Moments( img )
(Pdb) p img
<iplimage(nChannels=1 width=1920 height=1200 widthStep=1920 )>
(Pdb) n
> /home/tyndyll/Desktop/opencv-test.py(10)<module>()
-> print "Done"
(Pdb) p moments
<cv.cvmoments object at 0x7f31fb5437b0>
(Pdb) c
Done
精彩评论