5

The newer OpenCV documentation here says you can convert an IplImage to a Numpy array just like this:

arr = numpy.asarray( im )

but that doesn't work for my needs, because it apparently doesn't support math:

x = arr/0.01
TypeError: unsupported operand type(s) for /: 'cv2.cv.iplimage' and 'float'

If I try to specify data type, I can't even get that far:

arr = numpy.asarray( im, dtype=num.float32 )
TypeError: float() argument must be a string or a number

So I'm using the code provided in the older documentation here. Basically, it does this:

arr = numpy.fromstring( im.tostring(), dtype=numpy.float32 )

But the tostring call is really slow, perhaps because it's copying the data? I need this conversion to be really fast and not copy any buffers it doesn't need to. I don't think the data are inherently incompatible; I'm creating my IplImage with cv.fromarray in the first place, which is extremely fast and accepted by the OpenCV functions.

Is there a way I can make the newer asarray method work for me, or else can I get direct access to the data pointer in the IplImage in a way that numpy.fromstring will accept it? I'm using OpenCV 2.3.1 prepackaged for Ubuntu Precise.

2
  • OpenCV is under heavy development, also the Python bindings. You should try OpenCV 2.4.2 or 2.4.3rc first. Commented Oct 27, 2012 at 21:50
  • The question is valid. There is already some legacy Python code for OpenCV cv bindgins. They are still supported as of OpenCV 2.4.2. Converting them to cv2 may not always be an option. Commented Dec 28, 2012 at 18:15

2 Answers 2

9

Fun Fact:
Say you call:

import cv2.cv as cv    #Just a formality!

Capture = cv.CaptureFromCAM(0)
Img = cv.QueryFrame(Capture)

The object Img is an ipimage, and numpy.asarray(Img) is erratic at best. However! Img[:,:] is a cvmat type, and numpy.asarray(Img[:,:]) works fantastically, and more important: quickly!

This is by far the fastest way I've found to grab a frame and make it an ndarray for numpy processing.

Sign up to request clarification or add additional context in comments.

1 Comment

Awesome, I will try that. It won't be soon, unfortunately.
0

That page does not say about IplImage. It says about CvMat which is different.

Anyway you'd better use wrappers from newer cv2 namespace. It natively uses numpy arrays instead of own image containers. Also the whole cv module is considered deprecated and will be completely dropped in the nearest major release.

2 Comments

You mean I should use cv2.fromarray() or something like that, and then numpy.asarray() will magically work? Can you provide a link to docs?
Also, how the *#@$ was anybody supposed to know this? I've been using OpenCV off and on since version 0.1, and I always run away screaming. It's like a horror movie -- I'm afraid there will be a monster behind the door, but I open it anyway.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.