Ask Your Question
1

unable to properly read 16bit tiff image

asked 2013-08-22 04:06:59 -0600

zenified gravatar image

Hi, I am currently trying to use opencv to process a 16 bit tiff image. However the image matrix returned by imread seem to be filled with '255' values. I have tried various flag combination but it all does not seem to work. I tried reading a 16 bit EXR file and it was alright. As most of the values of the image is in levels higher then 255, it will seems as if the image was down converted into an 8 bit with all the level clipped.

Many Thanks

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
4

answered 2013-08-25 06:58:19 -0600

Just as an addition to my remark, these are actually the codes for formatting an imread:

/* 8 bit, color or gray - deprecated, use CV_LOAD_IMAGE_ANYCOLOR */
#define CV_LOAD_IMAGE_UNCHANGED  -1
/* 8 bit, gray */
#define CV_LOAD_IMAGE_GRAYSCALE   0
/* 8 bit unless combined with CV_LOAD_IMAGE_ANYDEPTH, color */
#define CV_LOAD_IMAGE_COLOR       1
/* any depth, if specified on its own gray */
#define CV_LOAD_IMAGE_ANYDEPTH    2
/* by itself equivalent to CV_LOAD_IMAGE_UNCHANGED
   but can be modified with CV_LOAD_IMAGE_ANYDEPTH */
#define CV_LOAD_IMAGE_ANYCOLOR    4

So you can see that the imread with the color option, creates an 8 bit 3 channel image.

edit flag offensive delete link more
1

answered 2013-08-22 04:27:51 -0600

Michael Burdinov gravatar image

updated 2013-08-25 01:42:33 -0600

  1. Have you tried flag CV_LOAD_IMAGE_ANYDEPTH in imread function? As far as I know it should be present in order to read 16-bit image properly (although I didn't tried it myself so I am not sure that it actually does what it should).

  2. Are you sure that when you accessing values you are not casting the them to unsigned char by mistake?

Edit:

Ok, to narrow the search for the problem try following code. I checked that it works properly on my computer:

Mat test1(1000, 1000, CV_16U, Scalar(400));
imwrite("test.tiff", test1);
Mat test2 = imread("stam.tiff", CV_LOAD_IMAGE_ANYDEPTH);
cout << test1.depth() << " " << test2.depth() << endl;
cout << test2.at<unsigned short>(0,0) << endl;
edit flag offensive delete link more

Comments

Yup I did used CV_LOAD_IMAGE_ANYDEPTH in imread function. But it is still stored as unsigned char.

Here is the imread code i used

imgRGB = imread(src, CV_LOAD_IMAGE_ANYDEPTH | CV_LOAD_IMAGE_COLOR);
zenified gravatar imagezenified ( 2013-08-22 21:28:48 -0600 )edit
1

remove the CV_LOAD_IMAGE_COLOR, it makes your data of format CV_8UC3

StevenPuttemans gravatar imageStevenPuttemans ( 2013-08-25 04:53:16 -0600 )edit

Steven, thanks for pointing that out. For some reason I though that I checked with CV_LOAD_IMAGE_COLOR option and it also was ok....

Michael Burdinov gravatar imageMichael Burdinov ( 2013-08-25 05:50:37 -0600 )edit

Just to check.. in line 3 the file name should be "test.tiff" right. If that is correct the output i gotten is >2 2 >400 Is this the output i am suppose to be expecting? Seems like it was able to store and output value higher then 8bit

zenified gravatar imagezenified ( 2013-08-25 20:26:10 -0600 )edit

ok this is strange. Your code works for the tiff file you created. But when I replaced it with my image file the last line give me an exception at memory location

zenified gravatar imagezenified ( 2013-08-25 20:59:08 -0600 )edit

Actually if depth returns 2 then it is a 16 bit grayscale image? Not a color one?

StevenPuttemans gravatar imageStevenPuttemans ( 2013-08-26 00:50:19 -0600 )edit
1

it 2 for the test image generated from Michael's code.

I found out the reason why there was an exception. The image loaded by opencv is stored in a 32F format, changing the type case to float fixed the problem.

My issue was that I have been reading the image matrix manually using the .data() operation. Which is why the matrix is always been processed as a 8 bit image.

Thanks you Michael for the your code, it helped me realized my mistake. Steven thank you for codes for formatting an imread too. I shall go and retry it on the Tiff image

zenified gravatar imagezenified ( 2013-08-29 00:09:58 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2013-08-22 04:06:59 -0600

Seen: 21,264 times

Last updated: Aug 25 '13