4

I am doing some simple programs with opencv in python. I want to write a few algorithms myself, so need to get at the 'raw' image data inside an image. I can't just do image[i,j] for example, how can I get at the numbers?

Thanks

etarion
  • 16,935
  • 4
  • 43
  • 66
Ferguzz
  • 5,777
  • 7
  • 34
  • 41

3 Answers3

5

Quick example of using LoadImageM to load an image file directly into a cvmat:

import cv

path = 'stack.png'
mat = cv.LoadImageM(path, cv.CV_LOAD_IMAGE_UNCHANGED)
x, y = 42, 6
print type(mat)
print mat[y, x]

Output:

<type 'cv.cvmat'>
(21.0, 122.0, 254.0)

Quick example showing how to multiple one or more color channels by 0.5:

for x in xrange(mat.cols):
    for y in xrange(mat.rows):
        # multiply all 3 components by 0.5
        mat[y, x] = tuple(c*0.5 for c in mat[y, x])

        # or multiply only the red component by 0.5
        b, g, r = mat[y, x]
        mat[y, x] = (b, g, r * 0.5)
samplebias
  • 37,113
  • 6
  • 107
  • 103
  • cool thanks... is there a reason behind the order of [y,x]? Also if I want to say, multiply each element by 0.5, how can I achieve this? – Ferguzz May 25 '11 at 18:43
  • Sure thing. The matrix is indexed by `row,col`, and each color tuple is ordered `(B, G, R)` for color images. I'll update the answer to show you how to multiply one or more color components by 0.5. – samplebias May 25 '11 at 18:56
1

Both CvMat and IplImage provide tostring methods that return a string representing the raw data. Using the image data, you can figure out how to interpret the string data as a matrix.

You should be able to use fromarray to convert the data string back into an image object.

To convert the string to an array, consider using the array module in Python. For instance:

array.array('B', CvMat.tostring()) # 'B' is unsigned char, for rgb8 images

To get the 'stride' between pixels, use:

stride = CvMat.step / CvMat.cols

Then typical array indexing to get individual pixels. You would probably want to wrap all this up in a class that hides all the nasty complexity.

zdav
  • 2,752
  • 17
  • 15
0

I do not know opencv python bindings, but in C or C++ you have to get the buffer pointer stored in IplImage. This buffer is coded according to the image format (also stored in IplImage). For RGB you have a byte for R, a byte for G, a byte for B, and so on.

Look at the API of python bindings,you will find how to access the buffer and then you can get to pixel info.

my2c

neuro
  • 14,948
  • 3
  • 36
  • 59