OpenCV Pixel Access, at, ptr, data, iteration (example)

I once wrote the following article.
http://study.marearts.com/2014/04/opencv-study-mat-point-access-method.html
This article is a sample code.

..
//color case
 Mat img = imread("ss.jpg");
 namedWindow("img", 0);

 
 //using at
 for (int i = img.rows / 10*3; i < img.rows / 10*4; ++i)
 {
  for (int j = 0; j < img.cols; ++j)
  {
   //Vec3b means 'uchar 3ch'
   unsigned char b = img.at< cv::Vec3b>(i, j)[0];
   unsigned char g = img.at< cv::Vec3b>(i, j)[1];
   unsigned char r = img.at< cv::Vec3b>(i, j)[2];

   //printf("%d %d %d\n", b, g, r);

   img.at< cv::Vec3b>(i, j)[0] = unsigned char(255 - b); //b
   img.at< cv::Vec3b>(i, j)[1] = unsigned char(255 - g); //g
   img.at< cv::Vec3b>(i, j)[2] = unsigned char(255 - r); //r
  }
 }
 


 //using ptr
 for (int i = img.rows / 10 * 6; i < img.rows / 10 * 7; i++) {

  cv::Vec3b* ptr = img.ptr< cv::Vec3b >(i);

  for (int j = 0; j < img.cols; j++) {

   cv::Vec3b bgr = ptr[j];
   unsigned char b = (bgr[0]);
   unsigned char g = (ptr[j][1]); //note!!
   unsigned char r = (bgr[2]);

   ptr[j] = cv::Vec3b(255 - b, 255 - g, 255 - r);

  }
 }

 
 
 
 //using data
 for (int i = img.rows / 10 * 8; i < img.rows / 10 * 9; i++) {
  for (int j = 0; j < img.cols; j++) {
   unsigned char r, g, b;
   b = img.data[i * img.step + j * img.elemSize() + 0];
   g = img.data[i * img.step + j * img.elemSize() + 1];
   r = img.data[i * img.step + j * img.elemSize() + 2];

   img.data[i * img.step + j * img.elemSize() + 0] = unsigned char(255 - b);
   img.data[i * img.step + j * img.elemSize() + 1] = unsigned char(255 - g);
   img.data[i * img.step + j * img.elemSize() + 2] = unsigned char(255 - r);

  }
 }



 //using iteration
 cv::MatIterator_< cv::Vec3b> itd = img.begin< cv::Vec3b>(), itd_end = img.end< cv::Vec3b>();
 for (int i = 0; itd != itd_end; ++itd, ++i) {
  cv::Vec3b bgr = (*itd);

  (*itd)[0] = 255 - bgr[0];
  (*itd)[1] = 255 - bgr[1];
  (*itd)[2] = 255 - bgr[2];
 }
 

 imshow("img",img);
 waitKey(0);



..
input


output

Comments

Popular posts from this blog

OpenCV Stitching example (Stitcher class, Panorama)

(OpenCV Study) Background subtractor MOG, MOG2, GMG example source code (BackgroundSubtractorMOG, BackgroundSubtractorMOG2, BackgroundSubtractorGMG)

Example source code of extract HOG feature from images, save descriptor values to xml file, using opencv (using HOGDescriptor )

Real-time N camera stitching Class.

8 point algorithm (Matlab source code) / The method to get the Fundamental Matrix and the Essential matrix

Optical Flow sample source code using OpenCV

Video Stabilization example source code, (using cvFindHomography, cvWarpPerspective functions in openCV)

yuv422(YUYV) to RGB and RGB to yuv422(YUYV), (Using OpenCV and TBB)

(C, C++) TinyXML , xml read & write

(OpenCV Study) calcOpticalFlowFarneback example source code ( dense optical flow )