How to access the elements of single channel IplImage in Opencv

前端 未结 3 1082
無奈伤痛
無奈伤痛 2021-01-20 19:51

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

相关标签:
3条回答
  • 2021-01-20 20:31

    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)
    
    0 讨论(0)
  • 2021-01-20 20:46

    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

    0 讨论(0)
  • 2021-01-20 20:48

    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.

    0 讨论(0)
提交回复
热议问题