Ask Your Question
0

how to convert image in rectangle into gray??

asked 2015-11-28 16:50:08 -0600

Akki gravatar image

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.

image description

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 flag offensive close merge delete

Comments

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);
can gravatar imagecan ( 2015-11-29 02:10:53 -0600 )edit

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.

Akki gravatar imageAkki ( 2015-11-29 09:14:25 -0600 )edit

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

Akki gravatar imageAkki ( 2015-11-30 03:20:30 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2015-11-30 09:46:39 -0600

Akki gravatar image

updated 2015-11-30 10:06:39 -0600

LorenaGdL gravatar image

This problem is solved on stack overflow by MIKI.

image description

#include <opencv2\opencv.hpp>
using namespace cv;

int main()
{
    // Your image
    Mat3b img = imread("path_to_image");

    // Your rects
    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;
}
edit flag offensive delete link more

Comments

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

LorenaGdL gravatar imageLorenaGdL ( 2015-11-30 10:08:44 -0600 )edit

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.

Akki gravatar imageAkki ( 2015-11-30 10:26:51 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-11-28 16:50:08 -0600

Seen: 1,333 times

Last updated: Nov 30 '15