OpenCV Mat Data point access and memcpy to another Mat, example source code

This is example source code.

Mat inImg = imread("Penguins.jpg",0);
//Data point copy
unsigned char * pData =;
int width = inImg.rows;
int height = inImg.cols;
//gray color value reverse
for(int i=0; i < width; ++i)
  for(int j=0; j < height; ++j)
   pData[j*width+i] = 255-pData[j*width+i];
//create another Mat same size, type with inImg
Mat outImg(width, height, CV_8UC1);
//data copy using memcpy function
memcpy(, pData, sizeof(unsigned char)*width*height);
//processing and copy check
imshow("Test", inImg);

imshow("Test2", outImg);


Thank you.


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)

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

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

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