简体   繁体   中英

while-loop in opencv causing error

While attempting to do real time planar object tracking with opencv I came across the "find object" demo for pyopencv . This script does what I want with the exception that it compares one static image against another whereas I'm trying to compare a static image against the current frame grabbed from a webcam. To this end I replaced this line

scene_filename = "box_in_scene.png"

with this

capture = cv.VideoCapture(0)
frame = Mat()
capture >> frame
imwrite("box_in_scene.png",frame)

This works as it should but when I then try to add a simple loop to make it do this continuously, it goes through one cycle then stops. When I exit the script I get the following error:

 OpenCV Error: Bad flag (parameter or structure field) (Unrecognized or unsupport
 ed array type) in cvGetMat, file M:\programming\packages\opencv\workspace\2.1\Op
 enCV-2.1.0\src\cxcore\cxarray.cpp, line 2476
 Traceback (most recent call last):
  File "find_obj.py", line 114, in <module>
    imageDescriptors = surf(image, mask, imageKeypoints)
 RuntimeError: M:\programming\packages\opencv\workspace\2.1\OpenCV-2.1.0\src\cxco
 re\cxarray.cpp:2476: error: (-206) Unrecognized or unsupported array type in fun
 ction cvGetMat

Anyone have a clue what might be causing this?

The loop I'm using is

myloop = 1
while myloop == 1 :

This link is the code in it's entirety.

A few things come to mind immediately upon perusing your code. Firstly, you are declaring a new Mat(), and two new namedWindow objects EVERY time you loop. While this could cause a memory error after a few thousand loops (depending on your computer and OS), it likely isn't your main problem. It is, however, a terrible way of doing things and a bad habit to get into.

My second issue with your code is that you scan an image from your video camera, save it to file, and then re-load the file back into memory so you can use the image! I understand the want to save off a copy of the image in memory from the camera, but you already have it in memory, so why reload it? If you are on windows, this could be the source of your bad matrix, since the VC++10 libs are known to have some issues with imwrite and imread. [EDIT] I know you're using python, but your program reports the error from a .cpp file, meaning the python imports are in fact linked to the c++ libs somewhere [/EDIT]

Try removing the imwrite and imread calls in your loop, and using the image directly from the camera. If your code works after this, then you'll know where your issues are. Let us know how it goes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM