Attention! This forum will be made read-only by Dec-20. Please migrate to Most of existing active users should've received invitation by e-mail.
Ask Your Question

specify the compression ratio of JPEG 2000

asked 2018-12-26 01:18:29 -0500

Digital Design gravatar image

updated 2018-12-26 01:30:49 -0500

berak gravatar image

Hi all, I am trying to compress an RGB image with JPEG 2000 in opencv c++.

    Mat x= imread("/home/amir/Downloads/aaaa.jpg", CV_LOAD_IMAGE_COLOR);
    vector<int> compression_params;
int n= 1;
    while (n!= 0)
        cout<< "Enter the compression ratio: ";
        cin>> n;
        compression_params[1]= n;
        cout<< "well, the compression ratio= "<< int(compression_params[1])<< endl;
        bool save_JPEG = imwrite(jpeg_st, x, compression_params);
        //bool save_JPEG = imwrite(jpeg_st, x);
        if (!save_JPEG) 
            cout << "Couldn't save the file" << endl;
            cout << "Press any ket to halt the program" << endl;
            return 0;

        temp= imread(jpeg_st, CV_LOAD_IMAGE_ANYCOLOR);
        namedWindow("The compressed image", WINDOW_FULLSCREEN );
        imshow("The compressed image", temp);
        namedWindow("The host image", WINDOW_FULLSCREEN );
        imshow("The host image", temp);

        temp.convertTo(temp, CV_64FC3);
        x.convertTo(x, CV_64FC3);
        cout<< GetPSNR2d(x, temp)<< endl;

This is a psuedo-code for reading the compression ratio of the JPEG2000. Then it shows compressed image and displays the PSNR of the compressed image. Surprisingly, DESPITE THE COMPRESSION RATIO (even for the compression ratio of 20), the PSNR always remains infinite and no artifact is observed in the compressed image!!! how can it be possible?????????????????????????????????????!!!!!!!!!!!!!!!!!!!!! Conversely, in Matlab the PSNR depends on the compression ratio but it seems like the opencv is simply ignoring the compression ratio. In opencv documentation ( ) it says that: "On Linux, BSD flavors and other Unix-like open-source operating systems, OpenCV looks for codecs supplied with an OS image. Install the relevant packages (do not forget the development files, for example, “libjpeg-dev”, in Debian and Ubuntu*) to get the codec support or turn on the OPENCV_BUILD_3RDPARTY_LIBS flag in CMake." Does anyone have experience of using JPEG2000 in opencv c++ by Linux Mint and cmake? How will you set OPENCV_BUILD_3RDPARTY_LIBS in cmake?

edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted

answered 2018-12-26 01:29:37 -0500

berak gravatar image

updated 2018-12-26 01:35:19 -0500

but it seems like the opencv is simply ignoring the compression ratio

yes. there are no compression flags for jpeg2000, only for jpg.

building the 3rdparty libs from src won't change anything here, but the resp. cmake flags would be:


, etc.

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower


Asked: 2018-12-26 01:18:29 -0500

Seen: 479 times

Last updated: Dec 26 '18