1、Mat
Mat TempImg; //裁取出的区域存储为Mat int x_begin, y_begin, width, height; //裁取区域的坐标及大小 int srcWidth, srcHeight; //存储原图宽、高 src = imread("test.jpg", 1); cout<<" image read error!"<<endl; srcWidth = src.cols; //获取原图宽、高 if(width < 1 || height < 1 || width > srcWidth || height > srcHeight){ LOOGE<<"[Rect error: srcWidth = "<<srcWidth<<", srcHeight = "<<srcHeight<<", x_begin = " <<x_begin<<", y_begin = "<<y_begin<<", width = "<<width<<", height = "<<height<<" ]"; if(x_begin + width > srcWidth) width = srcWidth - x_begin; if(y_begin + height > srcHeight) height = srcHeight - y_begin; 2、IplImage
int x_begin, y_begin, width, height; //裁取区域的坐标及大小 int srcWidth, srcHeight; //原图宽、高 IplImage* src = cvLoadImage("test.jpg", 1); cout<<" image read error!"<<endl; srcWidth = image->width; //获取原图宽、高 srcHeight = image->height; if(width < 1 || height < 1 || width > srcWidth || height > srcHeight){ LOOGE<<"[Rect error: srcWidth = "<<srcWidth<<", srcHeight = "<<srcHeight<<", x_begin = " <<x_begin<<", y_begin = "<<y_begin<<", width = "<<width<<", height = "<<height<<" ]"; if(x_begin + width > srcWidth) width = srcWidth - x_begin; if(y_begin + height > srcHeight) height = srcHeight - y_begin; IplImage* image_src = cvCloneImage(src); //备份原图 cvSetImageROI(image_src, cvRect(x_begin, y_begin, width, height)); //设置待裁取ROI IplImage* dst = cvCreateImage(cvSize(width, height), src->depth, src->nChannels); //创建裁取区域大小的IplImage* cvCopy(image_src, dst); //将ROI区域拷贝至dst cvResetImageROI(image_src); cvReleaseImage(&image_src);
//区域裁取 image_src = src.clone(); //备份原图 imageROI = image_src(Rect(x_begin, y_begin, width, height)); //设置待裁取ROI imageROI.convertTo(TempImg, TempImg.type()); //将ROI区域拷贝至ds
|