Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

This problem is solved on stack overflow by MIKI. link text

This problem is solved on stack overflow by MIKI. link text 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;

}

click to hide/show revision 3
No.3 Revision

This problem is solved on stack overflow by MIKI.

image description

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

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

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

}