使用cv :: Mat在OpenCV 2.4.2中裁剪并保存ROI作为新图像 [英] crop and Save ROI as new image in OpenCV 2.4.2 using cv::Mat

查看:132
本文介绍了使用cv :: Mat在OpenCV 2.4.2中裁剪并保存ROI作为新图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

致力于人脸检测和识别,并在成功检测到人脸之后,我只想裁剪人脸并将其保存在驱动器中的某个位置,以供识别码使用.我很难将感兴趣的区域保存为新图像.我在线上有一些代码,但是它是在使用IplImage*的早期版本的OpenCV中编写的.我正在使用使用cv::Mat的OpenCV 2.4.2.
Heeeelp !!!
如果您愿意,我会发布我的代码(人脸检测和识别本身).

Working on Face Detection and Recognition, and after successfully detecting a face, I just want to crop the face and save it somewhere in the drive to give it for the recognition code. I am having hard time doing the saving the Region of Interest as a new image. I have got some codes online but it is written in the previous version of OpenCV that uses IplImage*. I am using OpenCV 2.4.2 that uses cv::Mat.
Heeeelp!!!
I will post my codes(Face detection and Recognition per se) if you guys want it.

#include <cv.h>
#include <highgui.h>
#include <math.h>

// alphablend <imageA> <image B> <x> <y> <width> <height>
// <alpha> <beta>

IplImage* crop( IplImage* src,  CvRect roi)
{

  // Must have dimensions of output image
  IplImage* cropped = cvCreateImage( cvSize(roi.width,roi.height), src->depth, src->nChannels );

  // Say what the source region is
  cvSetImageROI( src, roi );

  // Do the copy
  cvCopy( src, cropped );
  cvResetImageROI( src );

  cvNamedWindow( "check", 1 );
  cvShowImage( "check", cropped );
  cvSaveImage ("style.jpg" , cropped);

  return cropped;
}

int main(int argc, char** argv)
{
  IplImage *src1, *src2;

  CvRect myRect;

  // IplImage* cropped ;
  src1=cvLoadImage(argv[1],1);
  src2=cvLoadImage(argv[2],1);

  {
    int x = atoi(argv[3]);
    int y = atoi(argv[4]);
    int width = atoi(argv[5]);
    int height = atoi(argv[6]);

    double alpha = (double)atof(argv[7]);
    double beta = (double)atof(argv[8]);

    cvSetImageROI(src1, cvRect(x,y,width,height));
    cvSetImageROI(src2, cvRect(100,200,width,height));

    myRect = cvRect(x,y,width,height) ;

    cvAddWeighted(src1, alpha, src2, beta,0.0,src1);

    cvResetImageROI(src1);
    crop (src1 , myRect);

    cvNamedWindow( "Alpha_blend", 1 );
    cvShowImage( "Alpha_blend", src1 );

    cvWaitKey(0);
  }

  return 0;
}

谢谢.和平

推荐答案

使用cv::Mat对象将使您的代码实质上更简单.假设检测到的人脸位于类型为cv::Rect的名为faceRect的矩形中,则只需键入以下内容即可获得裁剪的版本:

Using cv::Mat objects will make your code substantially simpler. Assuming the detected face lies in a rectangle called faceRect of type cv::Rect, all you have to type to get a cropped version is:

cv::Mat originalImage;
cv::Rect faceRect;
cv::Mat croppedFaceImage;

croppedFaceImage = originalImage(faceRect).clone();

或者:

originalImage(faceRect).copyTo(croppedImage);

这将从您提供的矩形中创建一个临时的cv::Mat对象(不复制数据).然后,通过克隆或复制方法将真实数据复制到新对象.

This creates a temporary cv::Matobject (without copying the data) from the rectangle that you provide. Then, the real data is copied to your new object via the clone or copy method.

这篇关于使用cv :: Mat在OpenCV 2.4.2中裁剪并保存ROI作为新图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆