Matlab: How to properly use the Java ImageIO class to get a BufferedIMage -


i'm trying mock-up in matlab on image data in database. image data java, output base64 encoded byte array. i'm not familiar [java image] format. however, wrote following matlab code based on java written working me. follow same basic outline, , java code able read image fine. matlab code looks this:

function [ result ] = querydb( thequery )   conn   = database( ... ); % connect database   result = fetch( exec( conn, thequery ) );   result = result.data;    close( conn ); end  data = querydb( 'sql query data' ); data = uint8( data{1,1} ); data = org.apache.commons.codec.binary.base64.decodebase64( data ); data = uint8( 127 + data ); % base64 decoder returns signed int8  import javax.imageio.imageio; import java.io.bytearrayinputstream;  datastream    = bytearrayinputstream( data ); bufferedimage = imageio.read( datastream ); 

upon inspection of bufferedimage, empty array of double opposed java bufferedimage instance.

i ran few tests on datastream see if behaved expected; sort of mini unit test along lines of:

for jj = 1:10   kk 1:10     assert( datastream.read() == data(kk) );   end;    datastream.reset(); end; 

it checked out, leads me believe problem imageio or use of it.

unfortunately, none of examples i've found using imageio (and of these other apis) used in quite manner outline here (in matlab is).

this code uses java.io.bytearrayinputstream in same manner -- in sense provided data array of bytes.

this code i'm looking -- convert java image matlab array. unfortunately, cheat taking matlab image, turning java image, turn back.

this code uses imageio, reading file stream. tried writing data out file reading in using java.io.file, same result either way.

so, i'm @ loss.

the approach outlined in question works ... thing have differs in way code above i'm not adding 127 uint8 array:

% imports % sql query data datastream    = bytearrayinputstream( decodebase64( rows.data{1} ) ); bufferedimage = imageio.read( datastream ); [w h] = deal( thewidth, theheight ); imagedata     = uint8( bufferedimage.getdata.getpixels( 0,0,w,h,[] ) ); imagedata     = permute( reshape( imagedata, [3 w h] ), [2 3 1] );  imshow( imagedata ); % voila 

edit incorporating text @kchja linked to, in case link becomes invalid:

subject: how can convert "java image" object matlab image matrix?

problem description: im2java function available converting matlab image java image. convert java image matlab image.

solution: there no built-in function convert java images matlab images. using java api can extract data java image , store matrix, matlab's image representation.

below example of converting matlab image java , matlab image. image used in example part of matlab demos , on matlab path. in example, "getpixels" function of java class "raster" returns rgb values image.

depending upon format of java image, additional work may required. java images can stored in 1 of many formats; "getpixels" function returns pixel data in format. more information, see javadoc page java.awt.image.raster class @ http://java.sun.com/j2se/1.5.0/docs/api/java/awt/image/raster.html

% create java image = imread('office_3.jpg'); javaimage = im2java(i);  % image properties h=javaimage.getheight; w=javaimage.getwidth;  % repackage 3d array (matlab image format) b = uint8(zeros([h,w,3])); pixelsdata = uint8(javaimage.getbufferedimage.getdata.getpixels(0,0,w,h,[])); = 1 : h base = (i-1)*w*3+1; b(i,1:w,:) = deal(reshape(pixelsdata(base:(base+3*w-1)),3,w)'); end  % display image imshow(b); 

the following 2 other ways implement (in more optimized manner):

example 1: (faster execution)

pixelsdata = reshape(typecast(jimage.getdata.getdatastorage, 'uint8'), 4, w, h); imgdata = cat(3, ...         transpose(reshape(pixelsdata(3, :, :), w, h)), ...         transpose(reshape(pixelsdata(2, :, :), w, h)), ...         transpose(reshape(pixelsdata(1, :, :), w, h))); 

example 2:

imgdata = zeros([h,w,3],'uint8'); pixelsdata = reshape(typecast(javaimage.getbufferedimage.getdata.getdatastorage,'uint32'),w,h).'; imgdata(:,:,3) = bitshift(bitand(pixelsdata,256^1-1),-8*0); imgdata(:,:,2) = bitshift(bitand(pixelsdata,256^2-1),-8*1); imgdata(:,:,1) = bitshift(bitand(pixelsdata,256^3-1),-8*2); 

Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -