Ask Your Question

imdecode() with any depth

asked 2016-04-14 09:46:43 -0500

fijoy gravatar image

updated 2016-04-14 09:47:45 -0500

Hi all,

OpenCV documentation specifies that I can invoke imdecode() with the same flags that we use in imread(). However, I am required to pass a Mat object to imdecode(), and this Mat object has a specific type. So, if I invoke imdecode() with IMREAD_UNCHANGED flag, will it reallocate the Mat object to have the same bit depth and number of channels as the input buffer? Or, will it ignore the IMREAD_UNCHANGED flag and convert the data to have the same type as the Mat object?

Thanks, Fijoy

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2016-04-14 10:06:12 -0500

berak gravatar image

updated 2016-04-14 10:11:26 -0500

imho, you're confusing the type of the input buffer (which will be a Mat or a vector of uchar bytes) with the type of the final image.

think of a file on disk. though it's bytes all the way down, you can decompress it, and rearrange the image pixels to various other formats, bgr, 16bit, float, whatever (this information is stored in the image header, preceeding the pixels) . this is, what's meant here. if your bytes (like on disk) represent a bgr image, should it get converted to grayscale or not ? should an alpha channel get discarded or not ? like that.

edit flag offensive delete link more


So, let's say I read an image file into a variable buf of type char* using fread(), create a Mat object M by calling Mat(L, 1, CV_8UC1, buf) where L is the #bytes I read from file, and invoke imdecode(M, IMREAD_UNCHANGED). If the image I read is of depth 16 bit and has 3 channels, will imdecode() return a Mat object of type CV_16UC3?

fijoy gravatar imagefijoy ( 2016-04-14 10:20:06 -0500 )edit

CV_16U3C for 16 bit, but hopefully, yes ;)

berak gravatar imageberak ( 2016-04-14 10:30:00 -0500 )edit

Question Tools

1 follower


Asked: 2016-04-14 09:46:43 -0500

Seen: 750 times

Last updated: Apr 14 '16