3

I have an x,y point coordinate, how would I use this to access a specific point on an IplImage?

Thanks

fdh
  • 5,256
  • 13
  • 58
  • 101

2 Answers2

7

Use CV_IMAGE_ELEM

CV_IMAGE_ELEM( image_header, elemtype, y, x*N+C )

E.g. given an 8-bit 3 channel (such as RGB) IplImage* img, we want (x,y) on the 2nd channel:

CV_IMAGE_ELEM(img, uchar, y, (x * 3) + 1))
Jacob
  • 34,255
  • 14
  • 110
  • 165
  • Thanks This would return the value of that pixel right? or does it return a pointer to that pixel? Because I need the value – fdh Sep 23 '11 at 22:57
  • @FarhadYusufali: Yes, it returns the value. For more info, look at the link I added – Jacob Sep 23 '11 at 23:50
  • @Jacob I am trying to use this in Java for HSV but it isn't working. Could you look a my post with a similar question? I am quite new to image processing. http://stackoverflow.com/questions/42399777/accessing-pixels-in-an-mat-image-opencv-java – cuber Feb 22 '17 at 19:10
3

OR, you can do this. for more matrix operation, see here.

http://note.sonots.com/OpenCV/MatrixOperations.html

     int col, row, z;
     uchar b, g, r;
     for( y = 0; row < img->height; y++ )
     {
       for ( col = 0; col < img->width; col++ )
       {
         //for( z = 0; z < img->nChannels; z++ )
         //{
         //   c = img->imageData[img->widthStep * row + col * img->nChannels + z];
         //}
         b = img->imageData[img->widthStep * row + col * 3]
         g = img->imageData[img->widthStep * row + col * 3 + 1];
         r = img->imageData[img->widthStep * row + col * 3 + 2];
       }
     }
plan9assembler
  • 2,862
  • 1
  • 24
  • 13