The Thing to be careful, when you use cvSetImageROI


cvSetImage is to set region of interest by a rect size.
If you program as follows:

cvSetImageROI(image, cvRect(10,10,300,300) ); //cvRect(Left, Top, Width, Height)

Then, The region set by (10,10,300,300) in the image will be prcessed only by functions.
so..

cvSetImageROI(image, cvRect(10,10,300,300) ); //cvRect(Left, Top, Width, Height)
cvSetZero(image);
cvResetImageROI(image);

The region (10,10,300,300) in the image is only set by zero.
and ROI is released by cvResetImageROI function.

But there is one thing to be careful.

//Origin image size is 1024, 768
cvSetIamgeROI(image, cvRect(10, 10, 300, 300) );
image->width; -> ?
image->height; -> ?

What do you think about the value?
the answer is 1024, 768
so if you program as follows:(This is not correct source!!)
cvSetImageROI(image, cvRect(10,10,300,300));
for(int h=0; h< image->height; ++h)
{
     for(int w=0; w< image->width; ++w)
     {
          unsigned char R,G,B;
          B = (unsigned char)image->imageData[h*image->widthStep+w*3+0];
          G = (unsigned char)image->imageData[h*image->widthStep+w*3+1];
          R = (unsigned char)image->imageData[h*image->widthStep+w*3+2];

          image->imageData[h*image->widthStep+w*3+0] =  (unsigned char)(255-B);
          image->imageData[h*image->widthStep+w*3+1] =  (unsigned char)(255-G);
          image->imageData[h*image->widthStep+w*3+2] =  (unsigned char)(255-R);
     }
}
cvResetImageROI(image);

The color of whole area of image is inverted.
So If you apply invert processing to the ROI, you have to program as follows:(This is correct source)
for(int h=image2->roi->yOffset; h< image2->roi->yOffset+image2->roi->height; ++h)
{
     for(int w=image2->roi->xOffset; w< image2->roi->xOffset+ image2->roi->width; ++w)
     {
          unsigned char R,G,B;
          B = (unsigned char)image2->imageData[h*image2->widthStep+w*3+0];
          G = (unsigned char)image2->imageData[h*image2->widthStep+w*3+1];
          R = (unsigned char)image2->imageData[h*image2->widthStep+w*3+2];

          image->imageData[h*image->widthStep+w*3+0] =  (unsigned char)(255-B);
          image->imageData[h*image->widthStep+w*3+1] =  (unsigned char)(255-G);
          image->imageData[h*image->widthStep+w*3+2] =  (unsigned char)(255-R);
     }
}

Thank you.
Be careful when you use cvSetImageROI~

Comments

Popular posts from this blog

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

OpenCV Stitching example (Stitcher class, Panorama)

AMP dose run on my pc?, AMP(Accelerated Massive Parallelism)

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

Optical Flow sample source code using OpenCV

Real-time N camera stitching Class.

OpenCV meanShiftFiltering example source code ( cpu: pyrMeanShiftFiltering, gpu:meanShiftFiltering, gpu:meanShiftSegmentation )

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

Image warping (using opencv findHomography, warpPerspective)

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