openCv裁剪图像

Joh*_*ann 11 c++ opencv crop iplimage

我遇到了openCv IplImage裁剪的问题.假设tmp和img都是IplImage*.使用代码:

printf("Orig dimensions: %dx%d\n", img->width, img->height);
cvSetImageROI(img, cvRect(0, 0,500,500));
tmp = cvCreateImage(cvGetSize(img),img->depth,img->nChannels);
cvCopy(img, tmp, NULL);
cvResetImageROI(img);
img = cvCloneImage(tmp);
printf("Orig dimensions after crop: %dx%d\n", tmp->width, tmp->height);
Run Code Online (Sandbox Code Playgroud)

当我使用上面的cvRect时,我会得到一个像预期的那样裁剪尺寸为500 x500的图像,但是当我使用rect(400,400,500,500)时,我会得到尺寸为500 X 320的图像.

Sma*_*ash 33

cvRect被定义为( int x, int y, int width, int height ),而不是(int left, int top, int right, int bottom).因此,您从该点开始选择500x500区域(x,y) = (400,400).我猜您的图像高度是720;).

  • 一个值得接受的答案; 来自我的谷歌搜索的顶部.有时SO比官方文档要快得多. (4认同)

小智 6

使用OpenCV裁剪图像

Mat image=imread("image.png",1);

int startX=200,startY=200,width=100,height=100

Mat ROI(image, Rect(startX,startY,width,height));

Mat croppedImage;

// Copy the data into new matrix
ROI.copyTo(croppedImage);

imwrite("newImage.png",croppedImage);
Run Code Online (Sandbox Code Playgroud)