Here is a crude way to set captured image ROI to grayscale:
cv::VideoCapture capture;
cv::Mat frame, grayFrame, gray3;
if(!capture.open(-1))
{
cout<<"Capture Not Opened"<<endl; return;
}
int width = capture.get(CV_CAP_PROP_FRAME_WIDTH);
int height = capture.get(CV_CAP_PROP_FRAME_HEIGHT);
cv::Rect roi(20,20,400,400); //The ROI to convert to gray
cv::Mat mask = cv::Mat::zeros(height,width,CV_8U);
for(int i = roi.y; i<roi.y + roi.height - 1; i++)
for(int j= roi.x; j<roi.x + roi.width - 1; j++)
mask.at<uchar>(i,j) = 1;
do
{
capture>>frame;
if(frame.empty()) break;
cv::cvtColor(frame,grayFrame,cv::COLOR_BGR2GRAY);
cv::cvtColor(grayFrame, gray3, cv::COLOR_GRAY2BGR);
frame.setTo(cv::Scalar::all(0),mask);
cv::add(frame,gray3,frame,mask);
cv::imshow("Image",frame);
cv::waitKey(10);
}
while (true);
I couldn't find a simpler way to set image values using masked operation, so the alternate is to set the ROI to zero, and add the masked values to it.
IplImage* frame=0, gray, temp; frame = cvQueryFrame(cam); temp = cvCloneImage(frame); cvCopy(frame,temp); if(drawing_box || box_drew){ draw_box(temp,box); } if(grayScaleOn && box_drew){ gray = cvCreateImage(cvSize(box.width,box.height), 8, 1); cvSetImageROI(temp, box); cvCvtColor(temp,gray,CV_BGR2GRAY); cvResetImageROI(temp); cvShowImage("Cam_2_Gray", temp); }
– kevin labille Mar 21 '13 at 14:15