8

I want to convert byte array to Mat object, but it throws

java.lang.UnsupportedOperationException: Provided data element number (60181) should be multiple of the Mat channels count (3)
    at org.opencv.core.Mat.put(Mat.java:992)

It is my code:

byte[] bytes = FileUtils.readFileToByteArray(new File("aaa.jpg"));

Mat mat = new Mat(576, 720, CvType.CV_8UC3);
//Imgcodecs.imencode(".jpg", mat, new MatOfByte(bytes));
mat.put(0, 0, bytes);

I tried many ways and also googled a lot, but did not find any solution.

Note: I know Imgcodecs.imread("aaa.jpg"); and

BufferedImage img = ImageIO.read(new ByteArrayInputStream(byteArray));
Mat mat = new Mat(img.getHeight(), img.getWidth(), CvType.CV_8UC3);
mat.put(0, 0, ((DataBufferByte) img.getRaster().getDataBuffer()).getData());

But I want to directly convert byte array to Mat without any extra process to speed up the process time.

Thanks in advance!

4 Answers 4

31

I solved the problem like this:

byte[] bytes = FileUtils.readFileToByteArray(new File("aaa.jpg"));
Mat mat = Imgcodecs.imdecode(new MatOfByte(bytes), Imgcodecs.CV_LOAD_IMAGE_UNCHANGED);

Now it works well and much faster than *bytes->BufferedImage->Mat*

Sign up to request clarification or add additional context in comments.

3 Comments

Imgcodecs.CV_LOAD_IMAGE_UNCHANGED macro has been replaced by IMREAD_UNCHANGED
how do you know if the image was decoded successfully?
@Tobiq Check whether the returned mat is empty()?
0

Try it please. I'm using this.

BufferedImage b = ImageIO.read(url);
BufferedImage b1 = new BufferedImage(b.getWidth(), b.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
b1=b;
byte [] pixels = ((DataBufferByte)b1.getRaster().getDataBuffer()).getData();

Mat myPic = new Mat(b1.getHeight(),b1.getWidth(),CvType.CV_8UC3);
myPic.put(0, 0, pixels);

Or

OpenCV imread()

Mat myPic = Highgui.imread(url);

3 Comments

Thanks for your reply, I know this way. But I want to directly convert bytes to Mat, if we first convert it to BufferedImage then to Mat, it is needs much process time. A kind of wasting time.
Mat myPic = Highgui.imread(url); this one also cannot use because I directly receive bytes.
Highgui has been removed from OpenCv 3.0.0 and replaced by Imgcodecs
0
// OpenCV 3.x
Mat mat = Imgcodecs.imdecode(new MatOfByte(bytes), Imgcodecs.CV_LOAD_IMAGE_UNCHANGED);
// OpenCV 2.x
Mat mat = Highgui.imdecode(new MatOfByte(bytes), Highgui.CV_LOAD_IMAGE_UNCHANGED);

Comments

-2

I've tried this kind of solution.

 static Mat ba2Mat(byte[] ba)throws Exception{
    // String base64String=Base64.getEncoder().encodeToString(ba);


      //  byte[] bytearray = Base64.getDecoder().decode(base64String);
        Mat mat = Imgcodecs.imdecode(new MatOfByte(ba), 
        Imgcodecs.CV_LOAD_IMAGE_UNCHANGED);
    return mat; 

}

5 Comments

"I've tried this kind of solution" - successfully? Or did you run into some issue?
What are you returning? An empty Mat? And what is ba? And where did you use the mob in your code? This code is completely wrong, even cannot be compiled. At the end, the question not says about how to convert byte array to BufferedImage, but to Mat object.
Sorry brother, I'm gonna edit code. Actually I extracted wrong code from my project. Check now
@LogRajBhatt Why you don't directly use Imgcodecs.imdecode() on ba as I did in my answer above? There is nothing new in your answer, the best way is which I have done above, no need to do it like you think.
Oh I see there is no meaning of first two lines. I pick up my code from entirely different scene

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.