Crop and save ROI as a new image in OpenCV 2.4.2 using cv :: Mat

While working on face detection and recognition, and after successfully detecting a face, I just want to crop the face and save it somewhere in the drive to give it a recognition code. I can hardly manage to maintain the region of interest as a new image. I have codes on the Internet, but it was written in a previous version of OpenCV, which uses IplImage* . I am using OpenCV 2.4.2 which uses cv::Mat .
Heeeelp !!!
I will send my codes (face recognition and recognition as such) if you want to.

 #include <cv.h> #include <highgui.h> #include <math.h> // alphablend <imageA> <image B> <x> <y> <width> <height> // <alpha> <beta> IplImage* crop( IplImage* src, CvRect roi) { // Must have dimensions of output image IplImage* cropped = cvCreateImage( cvSize(roi.width,roi.height), src->depth, src->nChannels ); // Say what the source region is cvSetImageROI( src, roi ); // Do the copy cvCopy( src, cropped ); cvResetImageROI( src ); cvNamedWindow( "check", 1 ); cvShowImage( "check", cropped ); cvSaveImage ("style.jpg" , cropped); return cropped; } int main(int argc, char** argv) { IplImage *src1, *src2; CvRect myRect; // IplImage* cropped ; src1=cvLoadImage(argv[1],1); src2=cvLoadImage(argv[2],1); { int x = atoi(argv[3]); int y = atoi(argv[4]); int width = atoi(argv[5]); int height = atoi(argv[6]); double alpha = (double)atof(argv[7]); double beta = (double)atof(argv[8]); cvSetImageROI(src1, cvRect(x,y,width,height)); cvSetImageROI(src2, cvRect(100,200,width,height)); myRect = cvRect(x,y,width,height) ; cvAddWeighted(src1, alpha, src2, beta,0.0,src1); cvResetImageROI(src1); crop (src1 , myRect); cvNamedWindow( "Alpha_blend", 1 ); cvShowImage( "Alpha_blend", src1 ); cvWaitKey(0); } return 0; } 

Thanks. World

+8
opencv face-detection roi crop
source share
2 answers

Using cv::Mat objects will make your code much easier. Assuming the detected face is in a rectangle called faceRect type cv::Rect , all you have to type to get the cropped version:

 cv::Mat originalImage; cv::Rect faceRect; cv::Mat croppedFaceImage; croppedFaceImage = originalImage(faceRect).clone(); 

Or alternatively:

 originalImage(faceRect).copyTo(croppedImage); 

This creates a temporary cv::Mat object (without copying data) from the rectangle you provided. Then the real data is copied to your new object using the clone or copy method.

+23
source share

To crop a region, ROI (Area of โ€‹โ€‹Interest) is used. Opencv2 makes the job pretty easy. You can check the link: http://life2coding.blogspot.com/search/label/cropping%20of%20image

-one
source share

All Articles