Ask Your Question

jishnu's profile - activity

2015-02-13 00:57:48 -0600 commented question Mat to Byte[] conversion not working in java

Thanks Berek , You saved the day.

2015-02-10 06:21:03 -0600 commented question Mat to Byte[] conversion not working in java

Yes,

Highgui.imwrite("path/filename.bmp",iwebcame_image); // Creates image in folder

but

MatOfByte inputframe=new MatOfByte(frameArray);

Highgui.imwrite("path/fname.jpeg", inputframe);// creates Black  image

also

inputframe.fromArray(frameArray);

Highgui.imwrite("path/filename1.jpeg", inputframe);// creates image Black

inputframe.put(0,0,frameArray);

            b=  Highgui.imwrite("path/file.bmp",inputframe);//Creates Black image
2015-02-10 05:04:23 -0600 commented answer How to get and modify the pixel of Mat in Java?

Not working for me too....

2015-02-10 04:03:46 -0600 commented question Mat to Byte[] conversion not working in java

updated with code

2015-02-10 01:34:36 -0600 asked a question Mat to Byte[] conversion not working in java

I tried to convert Mat to byte array in java . On conversion of Mat to Byte[] and Byte[] to Mat, I am not able to retain the original value of Mat. The .get method in Mat has parameter which is not working. Can anyone help me with the same?

Use Case: To grab each frame of video and send it via kafka to consumer containing byte[] message then consumer would recieve the byte array and convert to Mat and save it as image.

I came accross similar posts in java but no solution was found. link

See my code:

    System.loadLibrary("opencv_java249");
    MatOfByte webcam_image = new MatOfByte();
    VideoCapture capture = new VideoCapture(
            "/home/jishnu/CodeT/TrainingDataSet/Video.mp4");
    System.out.println("Frame Grabber started");
    byte[] frameArray;
    int i=0;
    if (capture.isOpened()) {
        while (true) {
            capture.read(webcam_image);
            frameArray = new byte[(int) (webcam_image.total() * webcam_image
                    .channels())];
            if (!webcam_image.empty()) {                    
            //  System.out.print(".");  
                webcam_image.get(0,0,frameArray);

                producer.send(new KeyedMessage<String, byte[]>("imageTopic",frameArray));

                //Below statements are only for debugging
                System.out.println(frameArray.length);      
                MatOfByte inputframe=new MatOfByte(frameArray);
                boolean b=  Highgui.imwrite("/home/jishnu/CodeT/Today7.jpeg", inputframe);

                if(b){System.out.println("save image success");}
                else System.out.println("save image failed");

                inputframe.fromArray(frameArray);
                b=  Highgui.imwrite("/home/jishnu/CodeT/Today6.bmp",inputframe);

                if(b){System.out.println("save image success");System.exit(0);}
                else System.out.println("save image failed");

            } else {
                System.out.println(" --(!) No captured frame -- Break!");