How to save an image with JPEG output in opencv 3.2.0?

asked 2017-06-18 09:49:54 -0500

Digital Design gravatar image

updated 2017-06-18 10:02:44 -0500

LBerger gravatar image

Hello, I am using opencv 3.2.0 and I simply want to read an image and simple save the same image to JPEG output with compression ratio of 75%. There is an example for PNG format: http://docs.opencv.org/trunk/d4/da8/g... . I use the following instructions:

#include <opencv2/core.hpp>
#include <opencv2/opencv.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>

#pragma comment(lib, "opencv_world320.lib")
#pragma comment(lib, "opencv_world320d.lib")

using namespace std;
using namespace cv;

int main()
{
    /************************************************************
    Declaring Variables
    ************************************************************/
    int x, y;
    Mat my_img;
    vector<int> compression_params;
    /************************************************************
    load and dispaly an image
    ************************************************************/
    my_img = imread("E:/Standard_Images/goldhill.jpg", CV_LOAD_IMAGE_COLOR);
    cout << "The image dimensions:" << my_img.rows << "*" << my_img.cols << endl;
    cout << "The number of the channels:" << my_img.channels() << endl;
    cout << "The image depth: " << my_img.depth() << endl;
    namedWindow("Lena", WINDOW_AUTOSIZE);
    imshow("Lena", my_img);
    waitKey(0);
    destroyWindow("Lena");
    /************************************************************
    Compress the image with JPEG and write into a new file
    ************************************************************/
    compression_params.push_back(CV_IMWRITE_JPEG_QUALITY);
    compression_params.push_back(75);
    bool bSuccess = imwrite("E:/Standard_Images/My_Image.jpg", my_img, compression_params);
    if (!bSuccess)
    {
        cout << "Couldn't save the file" << endl;
    }
    else
    {
        namedWindow("The Saved file", CV_WINDOW_AUTOSIZE);
        Mat openImage = imread("example.jpg", CV_LOAD_IMAGE_UNCHANGED);
        imshow("The Saved file", openImage);
    }
    getchar();
    return 1;
}

But it fails to save the output image and the following error is issued: "Exception thrown at 0x00007FFF08DC86C2 (opencv_world320.dll) in ConsoleApplication1.exe: 0xC0000005: Access violation reading location 0x0000020F4BADF000." Do you have any idea about how to save an image with JPEG format? Thanks a lot for your help

edit retag flag offensive close merge delete

Comments

I have never use #pragma comment(lib, . Now I think if you are in debug use only #pragma comment(lib, "opencv_world320d.lib") and in release only #pragma comment(lib, "opencv_world320.lib") but not both.

May be you should learn CCMake

LBerger gravatar imageLBerger ( 2017-06-18 10:05:55 -0500 )edit

Thank you for your help. But if I don't use #pragma comment(lib everything goes wrong and I end up with millions of errors! How can I complie my program without #progma?

Digital Design gravatar imageDigital Design ( 2017-06-18 14:59:26 -0500 )edit

you can read this post : git + cmake and then you can try this samples

LBerger gravatar imageLBerger ( 2017-06-18 15:18:07 -0500 )edit

I am really confused. I couldn't follow the flow of the given link. all the other functions are running properly (resize, crop, ...). Do you think that the main reason of failure to save JPEG file is #progma command?

Digital Design gravatar imageDigital Design ( 2017-06-18 17:58:19 -0500 )edit

Have you got only one pragma comment ? Your program is good (VS 2015 windows 10 opencv 3.2.0-dev) dll called is opencv_world320.dll you should be in release mode

LBerger gravatar imageLBerger ( 2017-06-19 01:02:28 -0500 )edit

please REMOVE those #pragma s and learn, how to add libraries properly to your ide.

then, you must NEVER use both debug and release libs at the same time. be strict about using the correct one only, or you'll burn in 0xC0000005:hell foerever !

berak gravatar imageberak ( 2017-06-19 01:14:19 -0500 )edit

Have you got only one pragma comment ? Your program is good (VS 2015 windows 10 opencv 3.2.0-dev) dll called is opencv_world320.dll you should be in release mode i tried to use each one separately, but the problem still persists...

Digital Design gravatar imageDigital Design ( 2017-06-19 01:40:33 -0500 )edit

please REMOVE those #pragma s and learn, how to add libraries properly to your ide.

then, you must NEVER use both debug and release libs at the same time. be strict about using the correct one only, or you'll burn in 0xC0000005:hell foerever ! I'm a beginner in opencv. Can you tell me more about debug/release mode? Many thanks to LBerger and break for their kind support

Digital Design gravatar imageDigital Design ( 2017-06-19 01:41:54 -0500 )edit

your project has a "linker" section, please add your lib there. opencv_world320d.lib goes into debug, opencv_world320.lib into release

berak gravatar imageberak ( 2017-06-19 01:49:17 -0500 )edit