使用 Python OpenCV Moments 函数处理图像

发布于 2024-12-03 04:24:59 字数 938 浏览 0 评论 0原文

我尝试在灰度图像上使用 Python opencv 函数 Moments(),但我收到以下 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
>>> 

我相信这种用法是正确的,正如 opencv 文档中所演示的那样 此处,其中 GetHuMoments()使用 Moments() 的结果。

我相信我已经正确安装了 opencv 和 numpy,因为我已经成功地将它们用于许多其他事情,并且我在 OS X 10.6 和 Red Hat 6 上都遇到了这种情况。

同样的问题opencv用户组中提出,但我不想按照回复的指示首先将图像转换为轮廓。

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 numpy 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

幸福%小乖 2024-12-10 04:24:59

看起来您需要强制从 IplImage 转换为 cvMat

之前有一个问题“使用 Python OpenCV 将图像捕获为数组”

使用 Python OpenCV 将图像捕获为数组

例如对我来说

>>> 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

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
感性 2024-12-10 04:24:59

您使用的是哪个版本的 python 和 opencv?我在 python 2.6.6 和 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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文