opencv ROI进行图像裁剪 C/C++ 学习日记 新手讲解

//裁剪图片
#include 
#include 
#include "opencv2/opencv.hpp"
void CutIplImage(IplImage* src, IplImage* dst, int x, int y)
{
	CvSize size = cvSize(dst->width, dst->height);//区域大小
	cvSetImageROI(src, cvRect(x, y, size.width, size.height));//设置源图像ROI
	cvCopy(src, dst); //复制图像
	cvResetImageROI(src);//源图像用完后,清空ROI
}

这是一个使用opencv裁剪图片的函数。
IplImage是OpenCV中的数据结构,用来表示图像。(详见附录)
src是原图像 ,dst为裁剪后的图像,x, y 为ROI左上角坐标。

对IplImage用cvSetImageROI函数设置了ROI后,
对图像的处理都将仅限于ROI区域,相当于该图像变为了ROI中的图像,
使用ROI需要cvResetImageROI清除ROI。

分解

CvSize size = cvSize(dst->width, dst->height);//区域大小

CvSize是存储图像长和宽的数据结构,cvSize函数对这一结构进行赋值。

cvSetImageROI(src, cvRect(x, y, size.width, size.height));//设置源图像ROI

基于给定的矩形设置图像的ROI(详见附录)

cvCopy(src, dst); //复制图像

将 src 复制到 dst

cvResetImageROI(src);//源图像用完后,清空ROI

cvResetImageROI与cvSetImageROI相对应
清空之前设置的ROI

附录

ROI(region of interest),感兴趣区域。机器视觉、图像处理中,从被处理的图像以方框、圆、椭圆、不规则多边形等方式勾勒出需要处理的区域,称为感兴趣区域。

typedef struct _IplImage
{
    int  nSize;             /* sizeof(IplImage) */
    int  ID;                /* version (=0)*/
    int  nChannels;         /* Most of OpenCV functions support 1,2,3 or 4 channels */
    int  alphaChannel;      /* Ignored by OpenCV */
    int  depth;             /* Pixel depth in bits: IPL_DEPTH_8U, IPL_DEPTH_8S, IPL_DEPTH_16S,
                               IPL_DEPTH_32S, IPL_DEPTH_32F and IPL_DEPTH_64F are supported.  */
    char colorModel[4];     /* Ignored by OpenCV */
    char channelSeq[4];     /* ditto */
    int  dataOrder;         /* 0 - interleaved color channels, 1 - separate color channels.
                               cvCreateImage can only create interleaved images */
    int  origin;            /* 0 - top-left origin,
                               1 - bottom-left origin (Windows bitmaps style).  */
    int  align;             /* Alignment of image rows (4 or 8).
                               OpenCV ignores it and uses widthStep instead.    */
    int  width;             /* Image width in pixels.                           */
    int  height;            /* Image height in pixels.                          */
    struct _IplROI *roi;    /* Image ROI. If NULL, the whole image is selected. */
    struct _IplImage *maskROI;      /* Must be NULL. */
    void  *imageId;                 /* "           " */
    struct _IplTileInfo *tileInfo;  /* "           " */
    int  imageSize;         /* Image data size in bytes
                               (==image->height*image->widthStep
                               in case of interleaved data)*/
    char *imageData;        /* Pointer to aligned image data.         */
    int  widthStep;         /* Size of aligned image row in bytes.    */
    int  BorderMode[4];     /* Ignored by OpenCV.                     */
    int  BorderConst[4];    /* Ditto.                                 */
    char *imageDataOrigin;  /* Pointer to very origin of image data
                               (not necessarily aligned) -
                               needed for correct deallocation */
}
IplImage;
typedef struct CvSize
{
    int width;
    int height;
}
CvSize;

CV_INLINE  CvSize  cvSize( int width, int height )
{
    CvSize s;
    s.width = width;
    s.height = height;
    
    return s;
}

希望得以指正和交流,谢谢。

你可能感兴趣的:(opencv ROI进行图像裁剪 C/C++ 学习日记 新手讲解)