gpt4 book ai didi

python - 如何在 python 中使用 opencv 处理图像?

转载 作者:太空狗 更新时间:2023-10-30 00:25:37 25 4
gpt4 key购买 nike

我想使用 opencv 库中的边缘检测算法。这是一段 python 代码:

from opencv.cv import *
from opencv.highgui import *

img = cvLoadImage ('xxx.jpg')
cvNamedWindow ('img')
cvShowImage ('img', img)
cvWaitKey ()

canny = cvCreateImage (cvSize (img.width, img.height), 8, 3)
cvCanny (img, canny, 50, 200)

harris = cvCreateImage (cvSize (img.width, img.height), 8, 3)
cvCornerHarris (img, harris, 5, 5, 0.1)

加载和显示图像工作正常,但 canny 和 harris 转换失败。
cvCanny 失败:

RuntimeError:  openCV Error:
Status=Unsupported format or combination of formats
function name=cvCanny
error message=
file_name=cv/cvcanny.cpp
line=72

cvCornerHarris 失败并出现此错误:

RuntimeError:  openCV Error:
Status=Assertion failed
function name=cvCornerHarris
error message=src.size() == dst.size() && dst.type() == CV_32FC1
file_name=cv/cvcorner.cpp
line=370

从这些消息中我可以推断加载的图像格式无效。但是我不明白如何转换它。
以下是一些 img 字段的值:

img.type = 1111638032
img.nChannels = 3
img.depth = 8

最佳答案

对于对同一类型问题感兴趣的其他人,我建议查看 http://simplecv.org

这是我编写的一些代码,用于对从网络摄像头获取的图像进行线检测。它甚至会通过 http 显示图像。 beard detection

import SimpleCV
import time

c = SimpleCV.Camera(1)
js = SimpleCV.JpegStreamer()

while(1):
img = c.getImage()
img = img.smooth()
lines = img.findLines(threshold=25,minlinelength=20,maxlinegap=20)
[line.draw(color=(255,0,0)) for line in lines]
#find the avg length of the lines
sum = 0
for line in lines:
sum = line.length() + sum
if sum:
print sum / len(lines)
else:
print "No lines found!"
img.save(js.framebuffer)
time.sleep(0.1)

http://labs.radiantmachines.com/beard/ 查看我为之制作的项目它会检测你脖子上的 mustache 有多长:)

关于python - 如何在 python 中使用 opencv 处理图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5002922/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com