基于OPENCV的图像裁切

IplImage:

step1:设置感兴趣区域cvSetImageROI

step2:拷贝图像到感兴趣区域

step3:释放感兴趣区域

//图部分替换

rect=cvRect(cpPoint.x,cpPoint.y,src2->width,src2->height);//cpPoint为起点的rect
cvSetImageROI(src,rect);//设置感兴趣区域
cvCopy(src2,src);//将图像src2拷贝到src
cvResetImageROI(src);//释放感兴趣区域

//图裁切

cvSetImageROI(src,rect);
IplImage *img_targetRegion =cvCreateImage(cvSize(rect.width,rect.height),src->depth,src->nChannels);
cvCopy(src,img_targetRegion);
cvResetImageROI(src);

Mat:

//从图中截取

type1:

Mat dst =src(Range(起始行,终止行),Range(起始列,终止列));

type2:

Mat dst;
src(cv::Rect(10,10,30,30)).copyTo(dst);
return dst;

//图部分替换(可用于补边)

Mat matZeros,imageROI,dst;
matZeros=Mat::zeros(src.rows+2,src.cols+2,src.type());
imageROI = matZeros(cv::Rect(1,1,src.cols,src.rows));
src.copyTo(imageROI);
matZeros.copyTo(dst);

return dst;

//补边的另一种写法

CvRect rect = {1,1,2,2};
IplImage *dst=cvCreateImage(cvSize(src->width+2,src->height+2),src->depth,src->nChannels);
//补黑边,因为部分图像不存在黑边
cvCopyMakeBorder(src,dst,cvPoint(1,1),BORDER_REPLICATE);

return dst;

你可能感兴趣的:(OPENCV积累)