How to access elements of single channel IplImage in Opencv
How can I access the IplImage Elements (one channel and IPL_DEPTH_8U depth).
I want to change the pixel value at a specific (x, y) position of an image.
opencv provides CV_IMAGE_ELEM method to access IplImage elements, this is a macro,
define CV_IMAGE_ELEM( image, elemtype, row, col ) \
(((elemtype*)((image)->imageData + (image)->widthStep*(row)))[(col)])
Second parameter - type
Pixels are stored inside the imageData array. So, since your image is the only channel, you just need to do the following:
myimage.imageData[y*myimage.width+x] = 100;
This provides the correct offset from the beginning of the buffer in imageData and is more readable than any other pointer algebra operation.
In the images of N-channels, it is enough to multiply the array offset by N and add the number of channels for reading:
i.e. for 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 multiplying the data to get the index can be done according to the goal you need to achieve.
A quick way to get the pixel value is to use a macro.
CV_IMAGE_ELEM( image_header, elemtype, y, x_Nc )
And in your case, the image is the only channel. So you can get the value of pixel i, j by
CV_IMAGE_ELEM(image, unsigned char, i, j)