# roi (Region of Interest)

I want to use histogram only the center part of my image.I use histogram for illumination images but I wanted to use it only on the center of the image because of time limitations. In addition to this, when I use equalizeHist, it sometimes causes noises.Here is my code;

Mat img1=imread("imageLight100.bmp");
Mat img2=img1.clone();
Rect roi = Rect(100,100,200,200);
Mat roiImg;

roiImg = img1(roi);
cvtColor(roiImg, roiImg, CV_RGB2GRAY);
equalizeHist(roiImg,roiImg);
imshow("roi",roiImg);

cvtColor(img2, img2, CV_RGB2GRAY);
img2.copyTo(roiImg);
imshow("roiImage",img2);
waitKey();

edit retag close merge delete

Sort by » oldest newest most voted

basarkir, can you please rephrase your question? It is hard to understand.

Anyway, when you are taking ROI of the image (as you did in your example), no copy is preformed. There still single image buffer for both img1 and roiImg. They are just pointing to different parts of it. So when you change content of roiImg, content of img1 is changed as well.

1) If you are working with gray images, it makes sense to convert them all prior to making any operations on them (clone and such).

2) If you are working with gray images than read them as gray images when you are using imread. Its default is to read image as 3-channel image, but you may force it to read it as 1-channel image, or you may read the image as is.

Mat color      =imread("imageLight100.bmp",1); // read the image as 3-channel image


3) imread() returns image in BGR format not RGB, so proper flag for cvtColor will be CV_BGR2GRAY.

4) I don't see why you copied img2 to roiImg. This will just erase all content of roiImage and replace it with content of img2 (including width and height).

To put this all together, if you want to make histogram equalization to part of the image it should be writen this way:

Mat img = imread("imageLight100.bmp",0);  // read the image as gray image
Mat roiImg = img(Rect(100,100,200,200));  // take ROI from image
equalizeHist(roiImg,roiImg);              // perform equalization of ROI
imshow("roiImage",roiImg);                // show only equalized ROI
imshow("image",img);                      // show whole image while part of it was equalized
waitKey();

more

Thank you so much Micheal for your answer. I found solution. Here is my working code:

void roi(Mat& img,int x_point,int y_point,int width ,int height)
{
Rect roi = Rect(x_point,y_point,width,height);
Mat roiImg;
roiImg = img(roi);
equalizeHist(roiImg,roiImg);
roiImg.copyTo(img(Rect(x_point, y_point, width, height)));
}


I use histogram for illumination images but I wanted to use it only on the center of the image because of time limitations. My roi function's execute time is 0.2ms. Using the equalizeHist function's execute time is 1.0ms. In addition to this, when I use equalizeHist, it sometimes causes noises. You gave me some useful information about images. I have always used cvtColor, but now I am going to use imread method with (1,0,-1).

more

You are welcome. Also note that that last line of your code is not needed. It just coping memory buffer on itself.

( 2012-07-30 07:09:48 -0600 )edit

Official site

GitHub

Wiki

Documentation