如何在OpenCV中裁剪CvMat?

我有一个图像转换成CvMatmatrix说CVMat source 。 一旦我从source获得一个感兴趣的区域,我希望algorithm的其余部分只应用到感兴趣的区域。 为此,我想我必须以某种方式裁剪我无法做到的sourcematrix。 有没有一种方法或函数可以裁剪一个CvMatmatrix并返回另一个裁剪的CvMatmatrix? 谢谢。

OpenCV有您感兴趣的区域function,您可能会觉得有用。 如果你使用cv::Mat那么你可以使用类似下面的东西。

 // You mention that you start with a CVMat* imagesource CVMat * imagesource; // Transform it into the C++ cv::Mat format cv::Mat image(imagesource); // Setup a rectangle to define your region of interest cv::Rect myROI(10, 10, 100, 100); // Crop the full image to that image contained by the rectangle myROI // Note that this doesn't copy the data cv::Mat croppedImage = image(myROI); 

提取子图像的文档

我知道这个问题已经解决了,但有一个非常简单的方法来裁剪。 你可以在一行中做到这一点 –

 Mat cropedImage = fullImage(Rect(X,Y,Width,Height)); 

为了得到更好的结果和对不同types的matrix的鲁棒性,除了第一个答案之外,您可以复制数据:

 cv::Mat source = getYourSource(); // Setup a rectangle to define your region of interest cv::Rect myROI(10, 10, 100, 100); // Crop the full image to that image contained by the rectangle myROI // Note that this doesn't copy the data cv::Mat croppedRef(source, myROI); cv::Mat cropped; // Copy the data into new matrix croppedRef.copyTo(cropped); 

要创build我们想要的作物的副本,我们可以执行以下操作,

 // Read img cv::Mat img = cv::imread("imgFileName"); cv::Mat croppedImg; // This line picks out the rectangle from the image // and copies to a new Mat img(cv::Rect(xMin,yMin,xMax-xMin,yMax-yMin)).copyTo(croppedImg); // Display diff cv::imshow( "Original Image", img ); cv::imshow( "Cropped Image", croppedImg); cv::waitKey(); 

我明白这个问题已经得到解答,但也许这对某个人有用。

如果你想将数据复制到一个单独的cv :: Mat对象中,可以使用类似下面的函数:

 void ExtractROI(Mat& inImage, Mat& outImage, Rect roi){ /* Create the image */ outImage = Mat(roi.height, roi.width, inImage.type(), Scalar(0)); /* Populate the image */ for (int i = roi.y; i < (roi.y+roi.height); i++){ uchar* inP = inImage.ptr<uchar>(i); uchar* outP = outImage.ptr<uchar>(i-roi.y); for (int j = roi.x; j < (roi.x+roi.width); j++){ outP[j-roi.x] = inP[j]; } } } 

重要的是要注意,这只能在单通道图像上正常工作。

你可以使用opencv函数轻松地裁剪一个Mat。

 setMouseCallback("Original",mouse_call); 

下面给出mouse_call

  void mouse_call(int event,int x,int y,int,void*) { if(event==EVENT_LBUTTONDOWN) { leftDown=true; cor1.x=x; cor1.y=y; cout <<"Corner 1: "<<cor1<<endl; } if(event==EVENT_LBUTTONUP) { if(abs(x-cor1.x)>20&&abs(y-cor1.y)>20) //checking whether the region is too small { leftup=true; cor2.x=x; cor2.y=y; cout<<"Corner 2: "<<cor2<<endl; } else { cout<<"Select a region more than 20 pixels"<<endl; } } if(leftDown==true&&leftup==false) //when the left button is down { Point pt; pt.x=x; pt.y=y; Mat temp_img=img.clone(); rectangle(temp_img,cor1,pt,Scalar(0,0,255)); //drawing a rectangle continuously imshow("Original",temp_img); } if(leftDown==true&&leftup==true) //when the selection is done { box.width=abs(cor1.x-cor2.x); box.height=abs(cor1.y-cor2.y); box.x=min(cor1.x,cor2.x); box.y=min(cor1.y,cor2.y); Mat crop(img,box); //Selecting a ROI(region of interest) from the original pic namedWindow("Cropped Image"); imshow("Cropped Image",crop); //showing the cropped image leftDown=false; leftup=false; } } 

有关详细信息,请访问链接使用鼠标裁剪图像