[OpenCV] Fast acquisition of ROI rectangular area of IplImage type image

I have always used the OpenCV C++ interface, but in some environments that can only use pure c, I have to use the c interface. IplImage is the most basic data structure of the c interface image data. The process of obtaining its ROI area is as follows, first through cvSetImageROI (IplImage* src, CvRect rect) Set the ROI area, then copy some image data, and finally reset the ROI area through cvResetImageROI (IplImage* src).

For the convenience of use, it is encapsulated here, as shown below:

IplImage* get_ipl_roi(IplImage* src, CvRect rect)
{
	cvSetImageROI(src, rect);
	IplImage* tmp = cvCreateImage(cvSize(src->roi->width, src->roi->height), src->depth, src->nChannels);
	cvCopy(src, tmp, NULL);
	cvResetImageROI(src);

	return tmp;
}

The test code is as follows:

intmain()
{
	IplImage *src = cvLoadImage("Hepburn.png", 0);
	CvRect rect = cvRect (60, 60, 130, 130);

	IplImage *roi = get_ipl_roi(src, rect);

	cvNamedWindow("ROI", CV_WINDOW_AUTOSIZE);
	cvShowImage("ROI", king);
	cvWaitKey(0);
	cvReleaseImage(&roi);
	cvDestroyWindow("ROI");
}


The result is as follows:

The original image


KING


Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326642582&siteId=291194637