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

 Akzeptierte Antwort

Guillaume
Guillaume am 5 Okt. 2014

1 Stimme

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 Kommentare

kanwal
kanwal am 5 Okt. 2014
giving an error No constructor 'java.io.ByteArrayInputStream' with matching signature found.
Guillaume
Guillaume am 5 Okt. 2014
Bearbeitet: Guillaume am 5 Okt. 2014
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')));
kanwal
kanwal am 6 Okt. 2014
plz help me out this is v important 4 my project
Guillaume
Guillaume am 6 Okt. 2014
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 am 6 Okt. 2014
Bearbeitet: kanwal am 6 Okt. 2014
jimage= [ ] an empty array
kanwal
kanwal am 6 Okt. 2014
i have attached the file this is a corrected (jpg file) not png.
Guillaume
Guillaume am 6 Okt. 2014
Bearbeitet: Guillaume am 6 Okt. 2014
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)
kanwal
kanwal am 6 Okt. 2014
error:Attempt to reference field of non-structure array.
Guillaume
Guillaume am 6 Okt. 2014
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 am 7 Okt. 2014
Bearbeitet: kanwal am 7 Okt. 2014
i m getting the error at height= jimage.getHeight;
kanwal
kanwal am 7 Okt. 2014
thank u so much its working.
Thank you allot, Its work for me too

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Image Analyst
Image Analyst am 4 Okt. 2014
Bearbeitet: Image Analyst am 4 Okt. 2014

0 Stimmen

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 Kommentare

kanwal
kanwal am 5 Okt. 2014
the resultant image is a straight line only nothing more..
kanwal
kanwal am 5 Okt. 2014
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 am 5 Okt. 2014
Bearbeitet: Guillaume am 5 Okt. 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))?
kanwal
kanwal am 5 Okt. 2014
format is jpg
Can you show the first few bytes d?
d(1:40)
kanwal
kanwal am 5 Okt. 2014
-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
Guillaume
Guillaume am 5 Okt. 2014
Your image is a png image, not a jpeg. The code I posted in my answer, with the typecast fix, should decode it.
kanwal
kanwal am 6 Okt. 2014
yeah u r right it was png. but its not decoding

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Convert Image Type finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by