Opencv... getting at the data in an IPLImage or CvMat
I am doing some simple programs with opencv in python. I want to write a few algorithms my开发者_JS百科self, 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
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)
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.
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
精彩评论