How do I convert byte[] to Mat.

asked 2018-03-06 12:29:10 -0600

Marko5280 gravatar image

updated 2018-03-06 16:05:59 -0600

I am trying to get the LEGO EV3 to work with OpenCV. I have a working LEGO sample and a working OpenCV sample. The problem comes when when I try to merge the two samples. The video protocols are different. The lego video input is byte[] and the OpenCV output is Mat. I have tried conversion code from the blogs but have not gotten anything to work. How do I convert LEGO byte [] to OpenCV Mat? The two samples and my program follow … . . . My program ...

package my_PACKAGE;

import java.io.IOException;               //    PACKAGE

import org.opencv.core.Mat;
import org.opencv.core.MatOfByte;
import org.opencv.core.Point;
import org.opencv.core.Size;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;

import lejos.hardware.Brick;
import lejos.hardware.BrickFinder;        //    PACKAGE
import lejos.hardware.Button;             //    PACKAGE
import lejos.hardware.LocalBTDevice;      //    PACKAGE
import lejos.hardware.ev3.EV3;            //    PACKAGE
import lejos.hardware.lcd.Font;           //    PACKAGE
import lejos.hardware.lcd.GraphicsLCD;    //    PACKAGE
import lejos.hardware.video.Video;        //    PACKAGE
import lejos.hardware.video.YUYVImage;    //    PACKAGE

public class Email    //    CLASS

 {    ///////////////////////////////////////////////////////////////////////////////////
                                                                                       //
 private static final int WIDTH = 160;                                                 //
 private static final int HEIGHT = 120;                                                //
 //    private static final String WIDTH = null;                                       //
                                                                                       //
                                                                                       //
 public static void main(String[] args)    //  METHOD                                  //
                                                                                       //
     {    /////////////////////////////////////////////////////////////////////////    //
                                                                                 //    //
Brick MyBrick;                                                                   //    //
MyBrick = BrickFinder.getLocal();                                                //    //
String MyType;                                                                   //    //
MyType = MyBrick.getType();                                                      //    //
String MyName;                                                                   //    //
MyName = MyBrick.getName();                                                      //    //
LocalBTDevice MyIPAddressS;                                                      //    //
MyIPAddressS = MyBrick.getBluetoothDevice();                                     //    //
EV3 School_red_EV3;                                                              //    //
School_red_EV3 = (EV3) BrickFinder.getLocal();                                   //    //
Video Insten;                                                                    //    //
Insten = School_red_EV3.getVideo();                                              //    //
GraphicsLCD GraphicsScreen;                                                      //    //
GraphicsScreen = School_red_EV3.getGraphicsLCD();                                //    //
GraphicsScreen.setFont(Font.getSmallFont());                                     //    //
try {                                                                            //    //
Insten.open(WIDTH,HEIGHT);                                                       //    //
} catch (IOException e) {                                                        //    //
// TODO Auto-generated catch block                                               //    //
e.printStackTrace();                                                             //    //
}                                                                                //    //
byte[] Still;                                                                    //    //
Still = Insten.createFrame();                                                    //    //
YUYVImage img;                                                                   //    //
img = new YUYVImage(Still, WIDTH, HEIGHT);                                       //    //
int threshold = 128;                                                             //    //
GraphicsScreen.clear();                                                          //    //
GraphicsScreen.refresh();                                                        //    //
while (!Button.ESCAPE.isDown())                                                  //    //
{    ////////////////////////////////////////////                                //    //
try {                                                                            //    //
Insten.grabFrame(Still);                                                         //    //
} catch (IOException e) {                                                        //    //
// TODO Auto-generated catch block                                               //    //
e.printStackTrace();                                                             //    //
}                                                                                //    //
threshold = img.getMeanY();                                                      //    //
img.display(GraphicsScreen, 0, 0, threshold);                                    //    //
}    ///////                                                                     //    //
try {                                                                            //    //
Insten.close();                                                                  //    //
} catch (IOException e) {                                                        //    //
// TODO Auto-generated catch block                                               //    //
e.printStackTrace();                                                             //    //
}                                                                                //    //
GraphicsScreen.clear();                                                          //    //
Mat mat = null;                                                                  //    //
//Mat src = Still;                                                               //    //
String filename = ((args.length > 0) ? args[0] : "lena.jpg");                    //    //
Object H;                                                                        //    //
mat = Imgcodecs.imdecode(new MatOfByte(Still), Imgcodecs.CV_LOAD_IMAGE_UNCHANGED);     //
//    Mat newImg = Mat(HEIGHT, WIDTH, CV_8UC1, Still + FRAMEINFO_SIZEF );        //    //
//    mat.put(0, 0, Still);    //    Error on this line                          //    //
Mat src = mat;                                                                   //    //
Mat dst = null;                                                                  //    //
double i = 5;                                                                    //    //
Imgproc.blur(src, dst, new Size(i, i), new Point(-1, -1));                       //    //
}    //////////////////////////////////////////////////////////////////////////////    //
                                                                                       //
 }     //////////////////////////////////////////////////////////////////////////////////

LEGO sample

package my_PACKAGE;

import java.io.IOException;               //    PACKAGE

import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Mat.*;
import org.opencv.core.MatOfByte;
import org.opencv.imgcodecs.Imgcodecs;

import lejos.hardware.Brick;
import lejos.hardware.BrickFinder;        //    PACKAGE
import lejos.hardware.Button;             //    PACKAGE
import lejos.hardware.LocalBTDevice;      //    PACKAGE
import lejos.hardware.ev3.EV3;            //    PACKAGE
import lejos.hardware.lcd.Font;           //    PACKAGE
import lejos.hardware.lcd.GraphicsLCD;    //    PACKAGE
import lejos.hardware.video.Video;        //    PACKAGE
import lejos.hardware.video.YUYVImage;    //    PACKAGE

public class Black_White_Camera   //    CLASS    
 {    //////////////////////////////////////////////////////////////
 private static final int WIDTH = 160;                //   
 private static final int HEIGHT = 120;                             
 public static void main(String[] args)    //  METHOD      /       
 {    ////////////////////////////////////////////////////////////
Brick MyBrick;                                                                
MyBrick = BrickFinder.getLocal();                                          
String MyType;                                                    
MyType = MyBrick.getType();                                      
if (MyType != "EV3")                                                     
        {System.out.println ...
(more)
edit retag flag offensive close merge delete

Comments

80% of the ill-formatted mess are unrelated to the problem.

please try to shorten it to the relevant part, and use proper formatting (mark it , press 10101 button)

(if noone can read it, noone will help you)

berak gravatar imageberak ( 2018-03-06 12:37:02 -0600 )edit

it's getting somewhat better ;)

berak gravatar imageberak ( 2018-03-06 13:02:42 -0600 )edit

byte[] Still; that's where the pixels are ?

do a quick test like:

Still.length == HEIGHT * WIDTH * 3;

if that's true, maybe a simple:

Mat cvyuv = new Mat(HEIGHT,WIDTH,CV_8UC1, Still);
Mat cvbgr = new Mat();
Imgproc.cvtColor(cvyuv, cvbgr, Imgproc.COLOR_YUV2BGR);

does the trick already

(but note, that opencv has it in bgr order, while your logo thingy might be using rgb)

[UPDATE, 8UC1, not 8UC3 as stated before !!]

berak gravatar imageberak ( 2018-03-06 13:06:38 -0600 )edit

Add comment -- Try 2

THANK YOU This is a unique editor. It needs short lines and uses proportional spacing while the formatted text is not in proportional spacing. I have to type the post to see the published version so that I can correct it. I did not want to provide too little information as to hide the issue. However, considering the high quality of the advice, I guess the only confusion is mine. I thought that I had spent three hours editing the post before you even saw it. Now I see that you see my post immediately. I do not work at that pace. I believe that the post is up to your standards now.

Marko5280 gravatar imageMarko5280 ( 2018-03-06 17:55:10 -0600 )edit
1

My best research gets me

Mat newImg = Mat(HEIGHT, WIDTH, CV_8UC1, Still  );

Except that CV_8UC1 cannot be resolved to a variable. I now find out that CV_8UC3 cannot be resolved to a variable

I am not importing CV_8UC1 or CV_8UC3. But, note that I am importing Mat and Improc.blur

Marko5280 gravatar imageMarko5280 ( 2018-03-06 18:03:11 -0600 )edit
1

CV_8UC3 in Java is addressed as CvType.CV_8UC3

Marko5280 gravatar imageMarko5280 ( 2018-03-07 06:29:23 -0600 )edit

The question makes an invalid assumption. While it is true that the LEGO webcam video/frame format is byte[], The pertinent OpenCV "Mat" method in Java receives the data format “ByteBuffer”. So, first I must convert from byte[] to "ByteBuffer".

Marko5280 gravatar imageMarko5280 ( 2018-03-07 07:15:11 -0600 )edit

When I try to execute

mat = new Mat(HEIGHT, WIDTH, CvType.CV_8UC3, Still );

I get the message

The constructor Mat(int, int, int, byte[]) is undefined.

This is similar to CV_8UC3 is undefined Do I need to make it "?.Mat"

By the way: I cannot find Mat(int, int, int, byte[]) in the Mat.class Mat(int, int, int, ByteBuffer) is in the Mat.class But I get that it is undefined Mat(int, int, int, Scalar) is in the Mat.class and I get that it is defined.

Marko5280 gravatar imageMarko5280 ( 2018-03-07 10:04:14 -0600 )edit