1. 程式人生 > >OpenCV擷取影象ROI並保存於指定路徑

OpenCV擷取影象ROI並保存於指定路徑

<span style="font-size:10px;"><span style="font-size:12px;">#include <cv.h>
#include<highgui.h>

int main()
{
	//Loading the original image and declaring the variables
	IplImage* src=cvLoadImage("D:\\Study Documents\\opencv_source\\images\\lena.jpg");
	CvSize size=cvSize(100,120);
	IplImage* roi=cvCreateImage(size,src->depth,src->nChannels);
	CvRect box=cvRect(120,100,size.width,size.height);
	
	//Setting the ROI and copying it
	cvSetImageROI(src,box);
	cvCopy(src,roi);
	cvSaveImage("D:\\Study Documents\\opencv_source\\images\\lenaROI.jpg",roi);
	cvResetImageROI(src);
	
	//Defining windows
	cvNamedWindow("ShowSRC");
	cvNamedWindow("ShowROI");
	
	//Showing images
	cvShowImage("ShowSRC",src);
	cvShowImage("ShowROI",roi);

	cvWaitKey(0);
	
	//Releasing resources
	cvReleaseImage(&src);
	cvReleaseImage(&roi);
	cvDestroyAllWindows();
	
	return 0;	
}</span></span>