OpenCV 2:如何节省投资回报率

时间:2011-09-19 02:47:58

标签: opencv

我是OpenCV的新手。目前,尝试加载并保存图像的已定义ROI。 对于OpenCV 1.x,我使用了以下函数...

#include <cv.h>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>  

void SaveROI(const CStringA& inputFile, const CStringA& outputFile)
{
    if (ATLPath::FileExists(inputFile))
    {
        CvRect rect;
        rect.x      = 8;
        rect.y      = 90;
        rect.width  = 26;
        rect.height = 46;

        IplImage* imgInput = cvLoadImage(inputFile.GetString(), 1); 
        IplImage* imgRoi   = cvCloneImage(imgInput);
        cvSetImageROI(imgRoi, rect);
        cvSaveImage(outputFile.GetString(), imgRoi);

        cvReleaseImage(&imgInput);
        cvReleaseImage(&imgRoi);
    }
}

如何使用OpenCV 2或C ++完成此操作。我尝试了以下但没有成功,整个图像都被保存了。

void SaveROICPP(const CStringA& inputFile, const CStringA& outputFile)
{      
    if (ATLPath::FileExists(inputFile))
    {
        cv::Mat imgInput = cv::imread(inputFile.GetString());

        if (imgInput.data != NULL)
        {
            cv::Mat imgRoi = imgInput(cv::Rect(8, 90, 26, 46));

            imgInput.copyTo(imgRoi);                 

            cv::imwrite(outputFile.GetString(), imgRoi);
        }
    }
}

任何帮助或建议?

2 个答案:

答案 0 :(得分:4)

您无需致电copyTo

void SaveROICPP(const CStringA& inputFile, const CStringA& outputFile)
{      
    if (ATLPath::FileExists(inputFile))
    {
        cv::Mat imgInput = cv::imread(inputFile.GetString());

        if (imgInput.data != NULL)
        {
            cv::Mat imgRoi = imgInput(cv::Rect(8, 90, 26, 46));
            cv::imwrite(outputFile.GetString(), imgRoi);
        }
    }
}

在您的版本中copyTo看到imgInput大于imgRoi并重新分配新的全尺寸矩阵以制作副本。 imgRoi已经是子图像,您只需将其传递给任何OpenCV函数。

答案 1 :(得分:3)

以下是一些用于混合,裁剪和保存新图像的经过测试的代码。 您裁剪,然后将该区域保存在新文件中。

#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;
}