Ask Your Question

roi (Region of Interest)

asked 2012-07-30 02:55:10 -0500

basarkir gravatar image

updated 2015-08-26 11:24:35 -0500

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);

cvtColor(img2, img2, CV_RGB2GRAY);
edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted

answered 2012-07-30 06:23:00 -0500

Michael Burdinov gravatar image

updated 2012-07-30 06:25:47 -0500

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.

There also few remarks about your code:

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
Mat gray       =imread("imageLight100.bmp",0); // read the image as 1-channel image
Mat grayOrColor=imread("imageLight100.bmp",-1); // read the image as is

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
edit flag offensive delete link more

answered 2012-07-30 06:44:06 -0500

basarkir gravatar image

updated 2012-07-31 15:05:57 -0500

Kirill Kornyakov gravatar image

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);
    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).

edit flag offensive delete link more


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

Michael Burdinov gravatar imageMichael Burdinov ( 2012-07-30 07:09:48 -0500 )edit
Login/Signup to Answer

Question Tools


Asked: 2012-07-30 02:55:10 -0500

Seen: 13,419 times

Last updated: Aug 01 '12