Ask Your Question
4

Mat to byte array

asked 2012-11-30 14:28:13 -0600

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.

edit retag flag offensive close merge delete

4 answers

Sort by ยป oldest newest most voted
5

answered 2012-12-02 05:52:47 -0600

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.

edit flag offensive delete link more

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 ( 2013-03-07 05:01:13 -0600 )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 ( 2013-03-07 05:03:11 -0600 )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++.

Michael Burdinov gravatar imageMichael Burdinov ( 2013-03-07 09:11:31 -0600 )edit
4

answered 2012-12-03 12:35:12 -0600

pranith12 gravatar image

updated 2012-12-03 12:36:55 -0600

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);
edit flag offensive delete link more

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 ( 2012-12-03 22:11:27 -0600 )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 ( 2013-04-10 20:30:30 -0600 )edit
obetron gravatar imageobetron ( 2013-04-11 17:15:01 -0600 )edit
2

answered 2012-12-01 03:39:03 -0600

Nghia gravatar image

updated 2012-12-02 03:50:32 -0600

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.

edit flag offensive delete link more
0

answered 2012-12-05 07:11:04 -0600

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

edit flag offensive delete link more

Question Tools

3 followers

Stats

Asked: 2012-11-30 14:28:13 -0600

Seen: 76,303 times

Last updated: Dec 05 '12