Python/OpenCV: Converting images taken from capture

Dom M. picture Dom M. · Nov 27, 2009 · Viewed 11.6k times · Source

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

When trying to convert the image to grayscale, the program crashes. (Python.exe has stopped working)

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:

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

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.

Answer

Dom M. picture Dom M. · Nov 27, 2009

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.

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)