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

 채택된 답변

Guillaume
Guillaume 2014년 10월 5일

1 개 추천

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

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

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
Image Analyst 2014년 10월 4일
편집: Image Analyst 2014년 10월 4일

0 개 추천

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

kanwal
kanwal 2014년 10월 5일
the resultant image is a straight line only nothing more..
kanwal
kanwal 2014년 10월 5일
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 2014년 10월 5일
편집: Guillaume 2014년 10월 5일
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 2014년 10월 5일
format is jpg
Can you show the first few bytes d?
d(1:40)
kanwal
kanwal 2014년 10월 5일
-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 2014년 10월 5일
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 2014년 10월 6일
yeah u r right it was png. but its not decoding

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Convert Image Type에 대해 자세히 알아보기

질문:

2014년 10월 4일

댓글:

2022년 4월 14일

Community Treasure Hunt

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

Start Hunting!

Translated by