How can I access the Elements of an IplImage (single channel and IPL_DEPTH_8U depth).
I want to change the pixel value at a particu
The fast way to get the pixel value is use macro.
CV_IMAGE_ELEM( image_header, elemtype, y, x_Nc )
And in your case, the Image is single channel.So you can get the i,j pixel value by
CV_IMAGE_ELEM(image, unsigned char, i, j)
opencv provide CV_IMAGE_ELEM method to access elements of IplImage,it's a macro,
define CV_IMAGE_ELEM( image, elemtype, row, col ) \
(((elemtype*)((image)->imageData + (image)->widthStep*(row)))[(col)])
second parameter is the type of
Pixels are stored inside imageData array. So, since your image is single channel you just have to do like:
myimage.imageData[y*myimage.width+x] = 100;
This ensure in imageData the right offset from beginning of buffer, and it's more readable than any other pointer algebra operation.
In N-channels images it's enough to multiply by N the array offset, and add the number of channel to read:
i.e. for an RGB image
myimage.imageData[3*(y*myimage.width+x)+0] = 100; //Red
myimage.imageData[3*(y*myimage.width+x)+1] = 100; //Green
myimage.imageData[3*(y*myimage.width+x)+2] = 100; //Blue
Any optimization to avoid to multiply data to obtain index can be done according to the goal you have to achieve.