Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

specify the compression ratio of JPEG 2000

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;
compression_params.push_back(CV_IMWRITE_JPEG_QUALITY);
compression_params.push_back(1);

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; getch(); return 0; }

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

    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 ( https://docs.opencv.org/3.0-beta/modules/imgcodecs/doc/reading_and_writing_images.html ) 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?

click to hide/show revision 2
None

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

berak gravatar image

specify the compression ratio of JPEG 2000

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;
 compression_params.push_back(CV_IMWRITE_JPEG_QUALITY);
 compression_params.push_back(1);

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; getch(); return 0; }

}

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

     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 ( https://docs.opencv.org/3.0-beta/modules/imgcodecs/doc/reading_and_writing_images.html ) 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?