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
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)