lh2705
lh2705

Reputation: 15

output cvRect as an image

I just want to double check that the ROI that I've set is the correct ROI. Hence, I want to be able to output the ROI as an image. The ROI uses cvRect as the 2nd argument and I don't really know how to convert that region into an image. I want to be able to use the values in cvRect to determine the final image. Is this possible?

  cvSetImageROI(frame,cvRect((*pt[0]).x,(*pt[0]).y,abs(ROIwidth),abs(ROIheight)));

Upvotes: 1

Views: 933

Answers (2)

Eric
Eric

Reputation: 2341

With opencv c++ API which I recommand to use for beginner!

cv::Mat img; // my pre-loaded image
cv::Rect roi; // roi position (x,y,width,height)
cv::Mat img_roi(img, roi); // only create header = no copy!
cv::imwrite("my_roi.png",img_roi); // save

Note that conversion from IplImage to cv::Mat is direct using :

IplImage* img = cvLoadImage("my_img.jpg", 1);
cv::Mat new_img(img); // convert IplImage* -> Mat

Upvotes: 1

sgarizvi
sgarizvi

Reputation: 16796

You can copy the ROI into a temporary buffer, and save that buffer.

IplImage* roi = cvCreateImage(cvSize(abs(ROIwidth),abs(ROIheight)),frame->depth, frame->nChannels);

cvSetImageROI(frame,cvRect((*pt[0]).x,(*pt[0]).y,abs(ROIwidth),abs(ROIheight)));

cvCopy(frame,roi);

cvResetImageROI(frame);

cvSaveImage("roi.jpg",roi);

cvReleaseImage(&roi);

Upvotes: 3

Related Questions