# how to convert image in rectangle into gray??

Hello,

I am looking for logic or suggestions regarding to access the or do various operations with selected rectangles from the image. for example in given image I am able to draw rectangle by using rectangle function but I want to convert the selected rectangle parts into gray scale or to find the non zero element values for it. for (int i = 612; i >= 0; i = i-204)
{

cout << "i" << i << endl;

rectangle( roi1, Rect(i, 0, 161, 643),  (255, 255,255), 2, 8, 0 );//add this line to draw contour on main screen

}


imshow( "Display window", roi1 );

Is there any suggestions how can I do so?

Thank you very much in advance.

Regards.

edit retag close merge delete

you can crop the region of interest from the original image with

Mat myRoi = myOriginalImage(myRectangle).clone();


and then convert it to grayscale as usual with

cvtColor(myRoi,myRoi,CV_BGR2GRAY);


Thank you very much for suggestion. I tried with following code, But still I am not getting the result. Mat OI = imread("C:/Users/ankit/Desktop/New folder/2/cam2.png", 1);

Mat roi2 = OI.clone();
for (int i = 612; i >= 0; i = i-204) { cout << "i" << i << endl; Mat roi2 = OI(Rect(i, 0, 161, 643)); rectangle( OI, Rect(i, 0, 161, 643), 1, 2, 8, 0 );

cvtColor(roi2, roi2, CV_BGR2GRAY );
}


imshow("Display window",roi2);

if possible then please provide me simple code or suggestion again.

Thank you very much once again.

hello, I want to perform operation in the same image. Thank you very much.

Sort by » oldest newest most voted

This problem is solved on stack overflow by MIKI. #include <opencv2\opencv.hpp>
using namespace cv;

int main()
{

vector<Rect> rois{ Rect(50, 50, 200, 300), Rect(400, 100, 200, 380) };

for (int i = 0; i < rois.size(); ++i)
{
// Convert each roi to grayscale
Mat crop = img(rois[i]).clone();      // Crop is color CV_8UC3
cvtColor(crop, crop, COLOR_BGR2GRAY); // Now crop is grayscale CV_8UC1
cvtColor(crop, crop, COLOR_GRAY2BGR); // Now crop is grayscale, CV_8UC3
crop.copyTo(img(rois[i]));            // Copy grayscale CV_8UC3 into color image
}

// Show results
imshow("Result", img);
waitKey();

return 0;
}

more

1

Though perfectly valid, I'm curious: why do you want to do the operation in place? If you need to further proccess each of your rectangles, it makes more sense to store a vector of ROIs, and then do whatever you want with each of them

Thank you very much for your concern. I am in first step of tube detection. my goal is to capture frame, set ROI for each tubes and than compare with reference frame. As I said I am still learning Image processing and I have got this idea first in my mind so I thought to go with it. but still I have to perform various processes with ROI. As you said, so may be I will try to store a vector of ROIs and perform operation as per requirement. So this is what I can explain. if you need more information I would like to discuss, even its help me too to improve my knowledge.

Official site

GitHub

Wiki

Documentation