how to convert a cell array into an image?

I fetched an image from sql database but its returning format is like i=[75839 int8] how can i convert it into image plz help me out

 Accepted Answer

If the bytes you get are truly a jpg image, you may be able to decode it with java:
jimage = javax.imageio.ImageIO.read(java.io.ByteArrayInputStream(d));
height = jimage.getHeight;
width = jimage.getWidth;
pixels = reshape(typecast(jimage.getData.getDataStorage, 'uint8'), [3,width,height]);
img = cat(3, ...
transpose(reshape(pixels(3,:,:), [width,height])), ...
transpose(reshape(pixels(2,:,:), [width,height])), ...
transpose(reshape(pixels(1,:,:), [width,height])));

12 Comments

giving an error No constructor 'java.io.ByteArrayInputStream' with matching signature found.
Yes, I missed the fact that the data came as int8. I automatically assumed it was uint8 as int8 doesn't make much sense. I don't know if you can fetch the data directly as uint8, but if you can't, just change the first line to:
jimage = javax.imageio.ImageIO.read(java.io.ByteArrayInputStream(typecast(d, 'uint8')));
plz help me out this is v important 4 my project
I assume that jimage is empty then. For some reason, the image decoding failed without raising an exception.
Can you save your d in a mat file and attach that so I can have a look. The fragment you've posted is the valid beginning of a png image but possibly there's something wrong later on.
Another option would be to save your d as a binary file (with fopen / fwrite / fclose) and then read it back with imread.
kanwal
kanwal on 6 Oct 2014
Edited: kanwal on 6 Oct 2014
jimage= [ ] an empty array
i have attached the file this is a corrected (jpg file) not png.
This is not the same d as you posted earlier, but no matter, the code I gave you works regardless of the type of the image (as long as it's recognised by java, png and jpeg are ok).
In any case, I had no issue seeing your image with the code I've posted. This is exacty what I typed:
d=d{1};
jimage = javax.imageio.ImageIO.read(java.io.ByteArrayInputStream(typecast(d, 'uint8')));
height = jimage.getHeight;
width = jimage.getWidth;
pixels = reshape(typecast(jimage.getData.getDataStorage, 'uint8'), [3,width,height]);
img = cat(3, ...
transpose(reshape(pixels(3,:,:), [width,height])), ...
transpose(reshape(pixels(2,:,:), [width,height])), ...
transpose(reshape(pixels(1,:,:), [width,height])));
imshow(img)
error:Attempt to reference field of non-structure array.
On which line do you get this error?
As I said, after importing the d.mat you posted into matlab, just running the code above works and displays the image of a yellow packet with 'Bonus tristar' written on it.
kanwal
kanwal on 7 Oct 2014
Edited: kanwal on 7 Oct 2014
i m getting the error at height= jimage.getHeight;
thank u so much its working.
Thank you allot, Its work for me too

Sign in to comment.

More Answers (1)

You need to take the (badly-named) i and reshape it into a 2 or 3D array, but you need to know the number of rows and columns.
cellContents = cell2mat(i); % Convert from cell to double.
grayImage = reshape(cellContents, [rows, columns]);
imshow(grayImage, []);

8 Comments

the resultant image is a straight line only nothing more..
I m doing like this setdbprefs('datareturnformat','cellarray'); conn=database('ssqw','',''); d=fetch(conn,'select picture from shampoo') d = [75389x1 int8] now i need to convert it into an image
Guillaume
Guillaume on 5 Oct 2014
Edited: Guillaume on 5 Oct 2014
AS IA said, you will have to reshape that d into a 2D image, so you need to know the height and width of the image.
Now since, 75389 is a prime number, there's no way that it can be reshaped into a rectangle, so most likely, there is a header to the image. Do you know what that header is (or what the format of the image is)? If not, can you post the first few value of d (for example d(1:40))?
format is jpg
Can you show the first few bytes d?
d(1:40)
-119 80 78 71 13 10 26 10 0 0 0 13 73 72 68 82 0 0 0 -31 0 0 0 -31 8 6 0 0 0 62 -77 -46 122 0 0 0 1 115 82 71 66 0 -82 -50 28 -23 0 0 0 4 0
Your image is a png image, not a jpeg. The code I posted in my answer, with the typecast fix, should decode it.
yeah u r right it was png. but its not decoding

Sign in to comment.

Categories

Find more on Convert Image Type in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!