处理opencv后将图像保存到文件

时间:2014-02-03 14:22:11

标签: c++ opencv

我正在尝试在处理完图像后保存图像但不幸的是我得到了一个例外。它正在运行除imwrite命令(代码上的最后一个命令)之外的所有行,并且它抛出了这个异常:

Unhandled exception at at 0x000007FEFD0D940D in histogram.exe: 
   Microsoft C++ exception: cv::Exception at memory location 0x00000000001DF720.

如何解决此错误以及导致错误的原因?

我正在使用的代码:

int main(int argc, char *argv[])       
{ 
    ///Loading image to IplImage 
    //IplImage *img=cvLoadImage(argv[1]);
    IplImage *img;
    img = cvLoadImage("phidza.JPG",1);
    cvShowImage("Ipl",img); 
    ///converting IplImage to cv::Mat
    Mat image=cvarrToMat(img); 
    imshow("Mat",image); 
    //std::cout<<"size: " << image.size() .height<< " , "
        // << image.size().width << std::endl ;
    if (image.empty())
    {
        cout << "Image cannot be loaded..!!" << endl;
        return -1;
    }

    cvtColor(image, image, CV_BGR2GRAY); //change the color image to grayscale image

    Mat img_hist_equalized;
    equalizeHist(image, img_hist_equalized); //equalize the histogram

    //create windows
    //namedWindow("Original Image", CV_WINDOW_AUTOSIZE);
    namedWindow("output", CV_WINDOW_AUTOSIZE);

    //show the image
    //imshow("Original Image", image);
    imshow("output", img_hist_equalized);
    waitKey(0); //wait for key press
    imwrite("../output.jpg", img_hist_equalized); // save image
    //cvSaveImage("output.jpg", img);

    destroyAllWindows(); //destroy all open windows

    return 0;
} 

1 个答案:

答案 0 :(得分:0)

此代码段对我来说很好,最后结果图像已保存。在您的情况下,二进制文件可能已损坏,因此最好重建opencv库。