OpenCV实现对某图的裁剪输出

C++: Mat::Mat(const Mat& m, const Range& rowRange, const Range& colRange=Range::all() )

第一个参数表示Mat文件图像,其实就是Mat类具体的一个对象。

第二个参数表示行的变换范围。

第三个参数表示列的变换范围。

Range也是OpenCV中的一个类。

要想取范围,可以先构造一个对象R1;

cv::Range R1;

R1.start=int X;(X为起点)

R1.end=int y;(Y为终点)

如果想取整列,只需在构造函数中输入Range::all()

所以可以输入一幅原图,靠范围实现图片的裁剪输出。

————————————–分割线 ————————————–

 编辑推荐

Ubuntu Linux下安装OpenCV2.4.1所需包 http://www.linuxidc.com/Linux/2012-08/68184.htm

Ubuntu 12.04 安装 OpenCV2.4.2 http://www.linuxidc.com/Linux/2012-09/70158.htm

CentOS下OpenCV无法读取视频文件 http://www.linuxidc.com/Linux/2011-07/39295.htm

Ubuntu 12.04下安装OpenCV 2.4.5总结 http://www.linuxidc.com/Linux/2013-06/86704.htm

Ubuntu 10.04中安装OpenCv2.1九步曲 http://www.linuxidc.com/Linux/2010-09/28678.htm

基于QT和OpenCV的人脸识别系统 http://www.linuxidc.com/Linux/2011-11/47806.htm

————————————–分割线 ————————————–

#include  
   
using namespace std; 
using namespace cv; 
   
int main(int argc, char* argv[]) 

    const char* imagename = “hehe.jpg”; 
   
    //从文件中读入图像 
    Mat img = imread(imagename); 
   
    //如果读入图像失败 
    if(img.empty()) 
    { 
        fprintf(stderr, “Can not load image %s\n”, imagename); 
        return -1; 
    } 
   
    //显示图像 
    imshow(“image”, img);

 int width = img.size().width;
 int height = img.size().height;
 Range R1; 
 R1.start=50;
 R1.end =width-50;
 //Range R2;
 //R2.start=50;
 //R2.end=height-50;

 Mat mask = Mat::Mat(img,R1 ,Range::all()) ;
   
 imshow(“Mask”,mask);
   
    //此函数等待按键,按键盘任意键就返回 
    waitKey(); 
   
    return 0;  <