OpenCv裁剪问题

时间:2014-09-23 09:07:27

标签: c++ image opencv

我是openCV的新手。我正在尝试裁剪图像,但我写的代码并没有裁剪它。请帮帮我。我得到了我感兴趣的区域,但是当我尝试复制它时,它复制了整个图像,而不仅仅是感兴趣的区域

#include<iostream>
using namespace std;
#include<vector>
#include<iostream>
#include<opencv2\opencv.hpp>


CvMemStorage * st = 0;
CvHaarClassifierCascade * hcc= 0;
char * path =  "C:/Users/Gardezi/Documents/Visual Studio 2012/Projects/AIMRL/AIMRL/haarcascade_frontalface_alt.xml";



bool startDetection(IplImage * img)
{   
int i ;
cvCreateImage(cvGetSize(img) , img->depth , img->nChannels );
CvPoint pt1 , pt2;
IplImage * f ;
if (hcc)
{
        CvSeq * face = cvHaarDetectObjects(img , hcc , st , 1.1  , 2 , CV_HAAR_DO_CANNY_PRUNING , cvSize(40  , 40 ) );


    //Face to Data Base 

    for (i = 0 ;i <  (face? face->total : 0 ) ; i++)
    {
        CvRect * r = (CvRect*)cvGetSeqElem(face , i );
        pt1.x = r->x; 
        pt1.y = r->y;
        pt2.x = r->x + r->width;
        pt2.y = r->y + r->height;

    cvRectangle(img , pt1 , pt2 , CV_RGB(255, 0 , 0) , 3 , 8 , 0); 

        cvSetImageROI(img , cvRect(pt1.x , pt1.y , pt2.x , pt2.y));


        f =  cvCreateImage( cvGetSize( img) , img->depth , img->nChannels );

        cvCopy(img , f , NULL );

    }


}

cvShowImage("Result" , f );
cvWaitKey(0);
return true;
}



void main()
{
IplImage * img;
hcc = (CvHaarClassifierCascade *)cvLoad(path , 0 , 0 , 0 );
img = cvLoadImage("download.jpg" , 0);
st = cvCreateMemStorage(0);
startDetection(img);
}

1 个答案:

答案 0 :(得分:2)

如果您不熟悉Opencv,请不要使用IplImage,而是使用cv::Mat。使用运算符括号进行裁剪是微不足道的,如果需要在新映像中复制感兴趣的区域,则调用clone()。

cv::Rect roi;
cv::Mat image;
[...] //Initialization of roi and image
cv::Mat roiOfImage = image(roi);
cv::Mat deepCopyOfRoiImage = image(roi).clone();