Ask Your Question
2

JPEG 2000 compression for 16 bit images does not work

asked 2013-10-25 03:12:04 -0500

ttn gravatar image

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.

edit retag flag offensive close merge delete

Comments

1

now what was the problem again ?

also note, that CV_LOAD_IMAGE_GRAYSCALE will force the output to 8bit gray.

berak gravatar imageberak ( 2013-10-25 03:21:06 -0500 )edit

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).

ttn gravatar imagettn ( 2013-10-31 02:10:42 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2013-10-31 02:26:12 -0500

ttn gravatar image

updated 2013-10-31 02:28:04 -0500

My comment to berak is badly formatted, so I post it here again:

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 imageNew8, newImage16;
newImage16 = imread("c:\tmp\test.jp2", CV_LOAD_IMAGE_ANYDEPTH|CV_LOAD_IMAGE_ANYCOLOR);
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).

The "c:\tmp\test.jp2" is shown in IrfanView as just a black image (can not see the pixel values, but they are checked in the code via newImage16 to be totally wrong in something like half of the picture). That IrfanView can not display the image properly is not a problem for me, I am not sure whether the decoder here is 100% compatible with libjasper.

edit flag offensive delete link more

Comments

I experienced a similar issue today. I read a 16-bit png file, and tried to save that file in jp2 format, without success. The saved image completely different from the read image.

fijoy gravatar imagefijoy ( 2016-04-15 13:36:10 -0500 )edit

it was a bug that fixed

sturkmen gravatar imagesturkmen ( 2016-04-18 07:07:18 -0500 )edit

Thanks sturkmen.

I have tested it in OpenCV 3.2 and verified it is corrected as you wrote.

ttn gravatar imagettn ( 2017-08-16 05:44:13 -0500 )edit
Login/Signup to Answer

Question Tools

Stats

Asked: 2013-10-25 03:12:04 -0500

Seen: 2,333 times

Last updated: Oct 31 '13