How to access image Data from a RGB image (3channel image) in opencv

前端 未结 5 723
予麋鹿
予麋鹿 2021-01-21 14:55

I am trying to take the imageData of image in this where w= width of image and h = height of image

for (int i = x; i < x+h; i++) //height of frame pixels
{
          


        
5条回答
  •  花落未央
    2021-01-21 15:39

    There are quite a few methods to do this (the link provided by jeff7 is very useful).

    My preferred method to access image data is the cvPtr2D method. You'll want something like:

    for(int x = 0; x < width; ++x)
    {
        for(int y = 0; y < height; ++y)
        {
            uchar* ptr = cvPtr2D(img, y, x, NULL);
            // blue channel can now be accessed with ptr[0]
            // green channel can now be accessed with ptr[1]
            // red channel can now be accessed with ptr[2]
        }
    }
    

    (img is an IplImage* in the above code)

    Not sure if this is the most efficient way of doing this etc. but I find it the easiest and simplest way of doing it.

    You can find documentation for this method here.

提交回复
热议问题