Java Byte array to uint8 array

조회 수: 16 (최근 30일)
Jason Klebes
Jason Klebes 2023년 3월 1일
댓글: Jason Klebes 2023년 3월 3일
I call java functions which return what is supposed to be a uint8 image with values from 0 to 255.
import java.ij.*;
I = ij.ImagePlus('filepath'); %read an 8bit image from file to java.ij.ImagePlus object
Idata= Ip.getImageStack().getImageArray(); %getImageArray() an array of Java Byte s
Idata % a matlab array of int8 values
In Java, it's a ByteArray with 8-bit entries. On returning the data to matlab, the Byte type gets automatically converted to int8, not uint8.
https://uk.mathworks.com/help/mps/java/conversion-of-java-types-to-matlab-types.html
The numbers are in range -127 to 128, not 0 to 255.
How can I force the Java Byte to be interpreted as uint8, OR convert matlab int8 to uint8 afterwards?

채택된 답변

Jan
Jan 2023년 3월 1일
siz = size(Idata);
Idata = reshape(typecast(Idata(:), 'uint8'), siz);
  댓글 수: 3
Jan
Jan 2023년 3월 2일
Why arrayfun? Does this indirection have a benefit?
Jason Klebes
Jason Klebes 2023년 3월 3일
I thought to save on a reshape operation and a line, but actually your vectorized version is much faster.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Call Java from MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by