tags:

views:

53

answers:

3

Hi,

I am trying to make some transformations on an image with OpenCV and Python. I started by reading the image with cvLoadImage function, and then I got the image data with imageData function.

img = highgui.cvLoadImage("x.png",1)
data = img.imageData

The problem is, the imageData function returns a string data and when I try to do some calculations on the image data, it gives me error because e.g. it is not allowed to do substraction on strings in Python.

I have a C code as an example, and the following calculation works completely well:

x= data[100] + 4*data[40] -data[20]

But in Python, as I said, I can't do this. Any clue about this? What is the difference about Python vs C about this statement and how can apply this kind of calculations in Python?

A: 

If you're sure that the data you're getting as a string is actually an integer, you can cast it to an int.

i.e.

data = int(img.imageData)

http://docs.python.org/library/functions.html#int

This probably isn't the right way to achieve your goals however. Have you looked at the built-in library function examples?

http://opencv.willowgarage.com/documentation/python/operations_on_arrays.html

Paul McMillan
I doubt `int(imageData)` is what you're looking for, as `imageData` will be a binary string…
David Wolever
Good call on the array operations, though — they are very helpful.
David Wolever
+1  A: 

As you've said, the imageData property returns a binary string containing the "raw image data" (I don't recall what format, though). Instead, you should access the image data by indexing into the img object:

>>> img = cv.CreateImage((10, 10), 8, 1)
>>> img[0, 0]
0.0
>>> img[0, 3] = 1.3
>>>
David Wolever
Oh, dang, sorry — didn't notice that you're using version 1 of the Python bindings, so my answer may not apply (I only know version 2).
David Wolever
An example of my imageData => "\x1e\x1e)))***///111666@@@IIIWWW]]]<<<:::TTT^^^}}}jjjQQQDDD<<<SSSYYYQQQNNN;;;MMMqqq\x7f\x7f\x7f\x8d\x8d\x8dhhhWWWRRRQQQPPPUUUgggvvv\x8c\x8c\x8c\x84"
Resul
The problem is, I am implementing a function by looking at a C code, and as I said they were able to do the transformation by using the ImageData. But I don't understand how they can do it. Is there a way to convert binary string to a integer in Python? Maybe in C language the chars (i don't have experiencve in C) it automatically converted to integer..
Resul
To turn that "string" into an "integer" you'd use something like this: `data = [ord(ch) for ch in imageData]` — then you could do stuff like `data[3] = data[6] + data[8]`. However, the conversion would be (comparatively) slow, so if I were you I'd look into the methods available on `img` to see if there's something like `getPixel`. You can list all the properties on `img` (methods and fields) using `dir(img)`.
David Wolever
Of course, if it's possible to update your OpenCV bindings and use version 2, then you can use the syntax I've shown in my answer (and a bunch of other happy things)… But I realize that may not be possible.
David Wolever