Reputation: 21
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?
Upvotes: 2
Views: 3970
Reputation: 93468
Try this:
Capture Image as Array with Python OpenCV
I encourage you to take a look at Python OpenCV Cookbook.
Upvotes: 0
Reputation: 154664
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
>>>
Upvotes: 1
Reputation: 20127
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
Upvotes: 0