I am using this code in MATLAB R2015a:
javaaddpath('javacv.jar');
import org.bytedeco.javacv.*;
grabber = FrameGrabber.createDefault(0);
grabber.start();
img = grabber.grab();
And I got img
variable that is org.bytedeco.javacv.Frame
class.
How I convert this class to matlab image?
I have half way through:
I Convert from org.bytedeco.javacv.Frame
to org.bytedeco.javacpp.opencv_core$Mat
in this code:
javaaddpath('javacv.jar');
import org.bytedeco.javacv.*;
import org.bytedeco.javacpp.*;
import org.bytedeco.javacpp.opencv_highgui.*;
grabber = FrameGrabber.createDefault(0);
grabber.start();
img = grabber.grab();
buff = img.image(1);
bytePointer = BytePointer(buff);
cvImage = javaObject('org.bytedeco.javacpp.opencv_core$Mat',img.imageHeight,img.imageWidth,opencv_core.CV_8UC3);
cvImage = cvImage.data(bytePointer);
imshow('tal',cvImage);
I can able to see correctly the image from imshow function.
Still I want to convert from org.bytedeco.javacpp.opencv_core$Ma
to matlab image.
How I can do it?
Able to convert it in inefficient way,
Put the code here, hopefully someone will convert it to efficient way code,
and publish here as an answer.
javaaddpath('javacv.jar');
import org.bytedeco.javacv.*;
import org.bytedeco.javacpp.*;
grabber = FrameGrabber.createDefault(0)
grabber.start();
img = grabber.grab();
buff = img.image(1);
w = img.imageWidth;
h = img.imageHeight;
%// from here is inefficient code
charBuff = buff.asCharBuffer;
n = charBuff.length;
data = repmat(uint16(0),n,1);
for i=0:n-1
data(i+1) = charBuff.get(i);
end
I = typecast(data, 'uint8');
I = cat(3, ...
reshape(I(3:3:end),[w h])', ...
reshape(I(2:3:end),[w h])', ...
reshape(I(1:3:end),[w h])' ...
);
imshow(I);
Faster way, still inefficient.
I think there is no more efficient way to do it, unless you write your own java code, that give you that byte array, because matlab give only duplicate of primitive array on function call, and not the array it self. the function can modified the array, but matlab do not duplicate the output when it done.
Read the "before last" comment in Problem on defining Java int array in Matlab
javaaddpath('javacv.jar');
import org.bytedeco.javacv.*;
import org.bytedeco.javacpp.*;
grabber = FrameGrabber.createDefault(0)
grabber.start();
img = grabber.grab();
buff = img.image(1);
w = img.imageWidth;
h = img.imageHeight;
%// from here is inefficient code
doubleBuff = buff.asDoubleBuffer;
n = doubleBuff.remaining;
data = zeros(n,1);
for i=1:n
data(i) = doubleBuff.get();
end
I = typecast(data, 'uint8');
I = cat(3, ...
reshape(I(3:3:end),[w h])', ...
reshape(I(2:3:end),[w h])', ...
reshape(I(1:3:end),[w h])' ...
);
imshow(I);
Faster way, still inefficient.
I think there is no more efficient way to do it, unless you write your own java code, that give you that byte array, because matlab give only duplicate of primitive array on function call, and not the array it self. the function can modified the array, but matlab do not duplicate the output when it done.
Read the "before last" comment in Problem on defining Java int array in Matlab
javaaddpath('javacv.jar');
import org.bytedeco.javacv.*;
import org.bytedeco.javacpp.*;
grabber = FrameGrabber.createDefault(0)
grabber.start();
img = grabber.grab();
buff = img.image(1);
w = img.imageWidth;
h = img.imageHeight;
%// from here is inefficient code
doubleBuff = buff.asDoubleBuffer;
n = doubleBuff.remaining;
data = zeros(n,1);
for i=1:n
data(i) = doubleBuff.get();
end
I = typecast(data, 'uint8');
I = cat(3, ...
reshape(I(3:3:end),[w h])', ...
reshape(I(2:3:end),[w h])', ...
reshape(I(1:3:end),[w h])' ...
);
imshow(I);