English 中文(简体)
Python/OpenCV: Converting images taken from capture
原标题:

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.

最佳回答

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)
问题回答

There is a common mistake here:

You re creating a single image in the newFrameImageGS variable before the 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.

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.

According to this manual the 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.





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签