JPEG 2000 compression for 16 bit images does not work
Hi,
according to the OpenCV documentation, I should be able to save a 16 bit (unsigned) image to a JPEG 2000 image, and then later read it again from the file.
The following code snippet shows the problem (I start by loading a simple 8 bit JPG image as I asume not every body reading this have a 16 bit image available - also I am working on gray level images only):
Mat image8 = imread("c:\\tmp\\about.jpg", CV_LOAD_IMAGE_GRAYSCALE);
Mat image16;
image8.convertTo(image16, CV_16UC1);
imwrite("c:\\tmp\\test16.jp2", image16);
Mat newImage16 = imread("c:\\tmp\\test16.jp2", CV_LOAD_IMAGE_GRAYSCALE), imageNew8;
newImage16.convertTo(imageNew8, CV_8UC1);
namedWindow("About", 0);
imshow("About", imageNew8);
waitKey(0);
destroyWindow("About");
The same code snippet works if I only use 8 bit JPEG 2000 compression (changing only one line):
Mat image8 = imread("c:\\tmp\\about.jpg", CV_LOAD_IMAGE_GRAYSCALE);
Mat image16;
image8.convertTo(image16, CV_8UC1);
imwrite("c:\\tmp\\test16.jp2", image16);
Mat newImage16 = imread("c:\\tmp\\test16.jp2", CV_LOAD_IMAGE_GRAYSCALE), imageNew8;
newImage16.convertTo(imageNew8, CV_8UC1);
namedWindow("About", 0);
imshow("About", imageNew8);
waitKey(0);
destroyWindow("About");
I have posted this about a year ago in another forum, using at that time OpenCV 2.3.1 on a 32 bit Window system using Microsoft Visual Studio 2010.
I am now working at a 64 bit Window system using Visual Studio 2012 and made my own build of OpenCV 2.4.6.1 using among other stuff the GPU module (CUDA).
I have the same problem with JPEG2000 again. Am I doing something wrong or is this a bug in OpenCV I should report?
PS: My image is a small one a 640x480 pixels only.
Thansk in advance, Torben.
now what was the problem again ?
also note, that CV_LOAD_IMAGE_GRAYSCALE will force the output to 8bit gray.
Hi berak,
sorry I did not see your post until now (was just looking in the overview for answers). You are right, the grayscale image is always loaded as an 8 bit image (I didnt realse that until you mentioned it). But it is still not working even if I correct for that:
Mat image8, image16; image8 = imread("c:\tmp\test.jpg", CV_LOAD_IMAGE_GRAYSCALE); image8.convertTo(image16, CV_16UC1, 255);
bool result = imwrite("c:\tmp\test.jp2", image16); Mat newImage16 = imread("c:\tmp\test.jp2", CV_LOAD_IMAGE_ANYDEPTH|CV_LOAD_IMAGE_ANYCOLOR);
Mat imageNew8; newImage16.convertTo(imageNew8, CV_8UC1, 1.0/255);
results in a totally distorted image (both newImage16 and newImage8, the result variable indicates that the write went well).