How to convert bytes arrays type of double to image?
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I am currently working on online SfM and I need to send stream of Images from Android phone to Matlab. I've managed to send bytes of data to Matlab from Android through TCP/IP Socket programming but I am unable to convert it to image. I expect that the image would be saved in the directory or if it's possible to be directly consumed by the SfM pipeline as described on this link: Structure From Motion From Two Views . I tried to decode it with java as described here: how to convert a cell array into an image?, but it didn't work as well. I got an error "Struct contents reference from a non-struct array object." on "height = jimage.getHeight;". Do you know how to fix this problem? Or do you know how to do this correctly in Matlab?
This is the code:
t = tcpip('0.0.0.0', 8889, 'NetworkRole', 'Server');
set(t,'InputBufferSize', 3000000);
set(t,'Timeout', 30);
disp('Waiting Connection...');
fopen(t);
while(1)
if (t.BytesAvailable > 0)
disp('Connection succeed.');
rawData = fread(t,t.BytesAvailable);
whos rawData;
jimage = javax.imageio.ImageIO.read(java.io.ByteArrayInputStream(rawData));
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);
end
end
fclose(t);
This is the sample of data that I get from "whos rawData":
Size: 390964x1
Bytes: 3127712
Class: double
0 commentaires
Réponses (1)
Walter Roberson
le 7 Nov 2016
Try
rawData = fread(t,t.BytesAvailable, '*uint8');
4 commentaires
Walter Roberson
le 14 Fév 2017
If you mean from image data, then https://www.mathworks.com/matlabcentral/answers/100155-how-can-i-convert-a-java-image-object-into-a-matlab-image-matrix suggests im2java
If you mean that you have a data stream, then exactly what is in the data stream? For example is it a just the result of reading a .jpg as a stream of bytes and sending it over the serial port?
Voir également
Catégories
En savoir plus sur Convert Image Type dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!