MATLAB Answers

how to input to "imread" using uint8 vector that is equivalent to binary data of image file from fread

17 views (last 30 days)
raym
raym on 4 Aug 2020
Edited: raym on 6 Aug 2020
Hi, I am trying to read many 512x512 jpg files that extracted from a .db database as uint8 vector for each file.
The vector is not the pixel intensity matrix, but exactly the binary data of file. So I need to fwrite the vector into a temp jpg file to hard disk, then imread that file as matrix in memory.
Because the number of jpg can be hundreds of thousands, so the frequent io from hard disc make the process very slow.
Is there a way to "recognize" the uint8 vector as if it is on the hard drive, then read it as a image file into imread?
Thanks.

Accepted Answer

Walter Roberson
Walter Roberson on 4 Aug 2020
For jpeg in particular, imread() invokes a function that calls upon a .mex* file to do the reading. It is likely that the file invokes libjpeg .
I believe that there is a route to do what you want by using Java. Java permits defining a Java.io.FileInputStream from memory (Byte array), and ImageIO.read() can be used to read images from FileInputStream .
I do not have any code that puts all this together; I have not used Java much.
  1 Comment
raym
raym on 6 Aug 2020
I have used the java way to do the rgb binary reading, and use imwrite to write jpg. the speed increased from 3 round/s to 15 round/s. The SSD is even faster: 18~19 round/s.

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 4 Aug 2020
Of course imread() will "recognize" the image file on disk as a uint8 jpg image if you got it written out properly. I don't know of anyway to decompress a JPG array that's in binary in memory so writing it out to disk to read back in may be the way you need to do it. If that's too slow, you can use solid state drive instead of a hard drive. Exactly how slow is it? For me, it typically takes less than a second to read in an image from my SSD.
  4 Comments
Image Analyst
Image Analyst on 4 Aug 2020
You can put it in as a suggestion but I think it would rarely be used by others so it probably will never rise to the top of the priority list. In over 40 years of image processing I've never had to do that.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by