图像切割,使用ROI

    xiaoxiao2021-03-25  120

    一.ROI介绍

    在OpenCV中我们能够非常方便地获取指定ROI区域的子图像。如果你对图像设置了ROI,那么,Opencv的大多数函数只在该ROI区域内运算(只处理该ROI区域),如果没设ROI的话,就会出来整幅图像。 ROI非常有用,例如我们想把图像中的人脸扣出来,进行人脸识别。需要注意的时候,ROI一定在图像内部,而不能超出图像的范围。

    对图像设定ROI的函数是: cvSetImageROI(IplImage* src,CvRect rect); src表示的是源图像,rect只的是ROI区域。 如果要取消ROI区域,那么使用函数: cvResetImageROI(IplImage* src); 这个函数,就把src上的ROI区域取消掉。

    图像的剪切有多种措施,其中利用ROI的措施

    第一步:将必需剪切的图像图像不局部设置为ROI

    cvSetImageROI(src , cvRect(x,y,width,height));

    第二步:修建一个与必需剪切的图像局部同样大小的新图像

    cvCreateImage(cvSize(width,height),IPL_DEPTH,nchannels);

    第三步:将源图像复制到修建的图像中

    cvCopy(src,dst,0);

    第四步:释放ROI区域

    cvResetIamgeROI(src);

    缩放可用cvResize();

    二.图像分割

    [cpp] view plain copy // Cut_image.cpp : 定义控制台应用程序的入口点。  //  /***********************************************************************  * OpenCV 2.4.4 测试例程  * 杜健健 提供  ***********************************************************************/  #include "stdafx.h"  #include <opencv2/opencv.hpp>     #include <opencv2/imgproc/imgproc.hpp>  #include <opencv2/highgui/highgui.hpp>  #include <iostream>  #include <vector>  using namespace std;  using namespace cv;      //剪切图片为m * n 块  void Cut_img(Mat src_img,int m,int n,Vector<Mat> ceil_img){        int t = m * n;      int height = src_img.rows;      int width  = src_img.cols;        int ceil_height = height/m;      int ceil_width  = width/n;         Mat roi_img,tmp_img;        Point p1,p2;      for(int i = 0;i<m;i++)          for(int j = 0;j<n;j++){              //p1 =               Rect rect(i+j*ceil_width,j+i*ceil_height,ceil_width,ceil_height);                src_img(rect).copyTo(roi_img);                ceil_img.push_back(roi_img);              imshow("roi_img",roi_img);              //getchar();              waitKey(0);          //rectangle(i+j*ceil_width,j+i*ceil_height,);          }     }    void show_images(Vector<Mat> imgs,int n){              //do something  }    int _tmain(int argc, _TCHAR* argv[])  {      Mat img = imread("D://input//lena.jpg",1);      imshow("src img",img);      int m = 3;      int n = 3;      Vector<Mat> ceil_img = m*n;        Cut_img(img,m,n,ceil_img);        waitKey();      return 0;  }  

    三.示例结构

    原图:

    切割后的图:

    四.总结

    根据切割的块数,计算小图片尺寸大小,存放到vector容器里,要的时候再读取出来就可以了。没用到opencv的ROI,但也可以使用。

    五.参考

    http://www.opencvchina.com/thread-228-1-1.html

    转自:http://blog.csdn.net/dujian996099665/article/details/8897077

    转载请注明原文地址: https://ju.6miu.com/read-21797.html

    最新回复(0)