Python/OpenCV:转换捕获的图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1807528/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python/OpenCV: Converting images taken from capture
提问by Domenic
I'm trying to convert images taken from a capture (webcam) and do some processing on them with OpenCV, but I'm having a difficult time..
我正在尝试转换从捕获(网络摄像头)拍摄的图像并使用 OpenCV 对它们进行一些处理,但我遇到了困难..
When trying to convert the image to grayscale, the program crashes. (Python.exe has stopped working)
尝试将图像转换为灰度时,程序崩溃。(Python.exe 已停止工作)
Here is the main snippet of my code:
这是我的代码的主要片段:
newFrameImageGS = cv.CreateImage ((320, 240), cv.IPL_DEPTH_8U, 1)
for i in range(0,5):
newFrameImage = cv.QueryFrame(ps3eye)
cv.CvtColor(newFrameImage,newFrameImageGS,cv.CV_BGR2GRAY)
golfSwing.append(newFrameImageGS)
When I try using cvConvertScale I get the assertion error:
当我尝试使用 cvConvertScale 时,出现断言错误:
src.size() == dst.size() && src.channels() == dst.channels()
which makes sense, but I'm pretty confused on how to go about converting the input images of my web cam into images that can be used by functions like cvUpdateMotionHistory() and cvCalcOpticalFlowLK()
这是有道理的,但我很困惑如何将我的网络摄像头的输入图像转换为可由 cvUpdateMotionHistory() 和 cvCalcOpticalFlowLK() 等函数使用的图像
Any ideas? Thanks.
有任何想法吗?谢谢。
UPDATE:
更新:
I converted the image to grayscale manually with this:
我用这个手动将图像转换为灰度:
for row in range(0,newFrameImage.height):
for col in range(0,newFrameImage.width):
newFrameImageGS[row,col] = (newFrameImage8U[row,col][0] * 0.114 + # B
newFrameImage8U[row,col][1] * 0.587 + # G
newFrameImage8U[row,col][2] * 0.299) # R
But this takes quite a while.. and i still can't figure out why cvCvtColor is causing the program to crash.
但这需要很长时间..我仍然无法弄清楚为什么 cvCvtColor 会导致程序崩溃。
回答by Domenic
For some reason, CvtColor caused the program to crash when the image depths where 8 bit. When I converted them to 32 bit, the program no longer crashed and everything seemed to work OK. I have no idea why this is, but at least it works now.
出于某种原因,当图像深度为 8 位时,CvtColor 导致程序崩溃。当我将它们转换为 32 位时,程序不再崩溃,一切似乎都正常。我不知道为什么会这样,但至少它现在有效。
newFrameImage = cv.QueryFrame(ps3eye)
newFrameImage32F = cv.CreateImage((320, 240), cv.IPL_DEPTH_32F, 3)
cv.ConvertScale(newFrameImage,newFrameImage32F)
newFrameImageGS_32F = cv.CreateImage ((320,240), cv.IPL_DEPTH_32F, 1)
cv.CvtColor(newFrameImage32F,newFrameImageGS_32F,cv.CV_RGB2GRAY)
newFrameImageGS = cv.CreateImage ((320,240), cv.IPL_DEPTH_8U, 1)
cv.ConvertScale(newFrameImageGS_32F,newFrameImageGS)
回答by fviktor
There is a common mistake here:
这里有一个常见的错误:
You're creating a single image in the newFrameImageGS
variable beforethe loop, then overwrite its contents in the loop, which is then appended to a list. The result will not be what you would expect. The list will contain five references to the same image instance at the end, since only the object reference is appended to the list, no copy of the object made this way. This image will contain the very last frame, so you get five of that frame as a result, which is not what you want, I guess. Please review the Python tutorial if it is not clear for you. You can solve this by moving the first line of the above code into the body of the for loop.
您在循环之前在newFrameImageGS
变量中创建单个图像,然后在循环中覆盖其内容,然后将其附加到列表中。结果不会是您所期望的。该列表最后将包含对同一图像实例的五个引用,因为只有对象引用被附加到列表中,没有以这种方式制作的对象副本。该图像将包含最后一帧,因此您会得到该帧的五帧,我猜这不是您想要的。如果您不清楚,请查看 Python 教程。您可以通过将上述代码的第一行移动到 for 循环体中来解决此问题。
Another possibilities if fixing the above would not help you:
如果解决上述问题对您没有帮助,则另一种可能性:
The CvtColor
function seems to be the correct one for conversion to grayscale, since it can convert to a different number of channels.
该CvtColor
函数似乎是转换为灰度的正确函数,因为它可以转换为不同数量的通道。
According to this manualthe CvtColor
function requires a destination image of the same data type as the source. Please double check that newFrameImage
is a IPL_DEPTH_8U
image.
根据本手册,该CvtColor
功能需要与源数据类型相同的目标图像。请仔细检查这newFrameImage
是一个IPL_DEPTH_8U
图像。