Ask Your Question
4

Mat to byte array

asked Nov 30 '12

pranith12 gravatar image

I am trying to convert a Mat object to a byte array to transfer over a network. How do I achieve this? I am trying to do this in Java.

Preview: (hide)

4 answers

Sort by » oldest newest most voted
5

answered Dec 2 '12

Michael Burdinov gravatar image

If your need is to convert the data of the image, you don't need to do much. If function image.isContinuous() returns true (which is usually the case) then it is already byte array. You can access it by calling image.data. If image.isContinuous() returns false (for example you worked with ROI of image), you will need to move the data. Easiest way will be by calling image.clone().

As for the header of image, you don't need to send all of its fields. Value of reference counter for example will have no meaning for someone who received this file from the network. Send only the fields that will allow recipient to create Mat by himself. Those are width, height and type of the image.

Preview: (hide)

Comments

1

Hi! I am trying to do the same conversion in java desktop with opencv 2.4.4. I want to transform the Mat data in byte array to create ImageIcon to display in JLabel. I tried this, but it doesn't work..

Mat image = new Mat(); VideoCapture camCapture = new VideoCapture(0); //then I read the capture from the camera camCapture.read(frame)

//I know that the type of mat is CV_8UC3 continuous so, I try to use that functions

public ImageIcon getImageIconFromMatTo(Mat frame){
    ImageIcon retImage = null;
    byte[] imageInBytes = new byte[(safeLongToInt(frame.total())) * frame.channels()];
    frame.get(0, 0, imageInBytes);

    retImage = new ImageIcon(imageInBytes);

    return retImage;
}

public int safeLongToInt(long l) {
    if (l < Integer.MIN_VALUE || l > Integer.MAX_VALU

help please!

joseFraunhofer gravatar imagejoseFraunhofer (Mar 7 '13)edit

sorry, I couldn't write all

public int safeLongToInt(long l) {
    if (l < Integer.MIN_VALUE || l > Integer.MAX_VALUE) {
        throw new IllegalArgumentException
            (l + " cannot be cast to int without changing its value.");
    }
    return (int) l;
}
joseFraunhofer gravatar imagejoseFraunhofer (Mar 7 '13)edit

I didn't used Java for a very long time, and never used OpenCV for Java, so I can't give you a reliable answer. I recommend you to post new question that will be visible by other people (not just me). Don't forget to mention that you are talking about OpenCV for Java in body of your question and in tags of question. Otherwise people will think you are talking about C++.

4

answered Dec 3 '12

pranith12 gravatar image

updated Dec 3 '12

This is how I did it: byte[] to Mat:

        byte[] raw_data = ...;
        Mat mat = new Mat();
        mat.put(0, 0, raw_data);

Mat to byte[]:

        byte[] return_buff = new byte[(int) (result_mat.total() * 
                                            result_mat.channels())];
        result_mat.get(0, 0, return_buff);
Preview: (hide)

Comments

This method is throwing an error: OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cvtColor, file /home/pranith/devops/code/opencv.git/opencv/modules/imgproc/src/color.cpp, line 3326 329809 [pool-2-thread-2] WARN org.apache.avro.ipc.Responder - user error

pranith12 gravatar imagepranith12 (Dec 4 '12)edit

I get the byte value from mat object with this way, but while convert the byte value to bufferedimage an error occured. Then I get byte value from FileInputStream object and the mat to byte and file to byte results were different. I need to convert Mat to byte[] and then byte[] to BufferedImage. How can I solve the problem?

obetron gravatar imageobetron (Apr 11 '13)edit
2

answered Dec 1 '12

Nghia gravatar image

updated Dec 2 '12

Not sure if there's a single function to do it but you have access to the following variables in cv::Mat

  • data (raw data)
  • rows, cols
  • elem_size (size in byte of each element)

So rows * cols * elem_size should give you to the total bytes to send.

Preview: (hide)
0

answered Dec 5 '12

wuling gravatar image

I use

Mat src; src.create(cv::Size(cols,rows),CV_8UC1); for (int i=0;i<rows;i++) {="" memcpy(src.ptr(i),srcdata+i*cols,cols);="" }<="" p="">

Althougth,it's not very smart.But it can work

Preview: (hide)

Question Tools

3 followers

Stats

Asked: Nov 30 '12

Seen: 76,903 times

Last updated: Dec 05 '12