Converting BufferedImage to Mat in opencv

匿名 (未验证) 提交于 2019-12-03 02:29:01

问题:

How can I convert a BufferedImage to a Mat in OpenCV? Im using the java wrapper for OpenCV(not JavaCV). As I am new to OpenCV I have some problems understanding how Mat works.

I want to do something like this. (Based on Ted W. reply):

          BufferedImage image = ImageIO.read(b.getClass().getResource("Lena.png"));            int rows = image.getWidth();           int cols = image.getHeight();           int type = CvType.CV_16UC1;           Mat newMat = new Mat(rows,cols,type);            for(int r=0; r

This doesn't work. "Lena_copy.png" is just a black picture with the correct dimensions.

回答1:

I also was trying to do the same thing, because of need to combining image processed with two libraries. And what I’ve tried to do is to put byte[] in to Mat instead of RGB value. And it worked! So what I did was:

1.Converted BufferedImage to byte array with:

byte[] pixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData(); 

2. Then you can simply put it to Mat if you set type to CV_8UC3

image_final.put(0, 0, pixels); 

Edit: Also you can try to do the inverse as on this answer



回答2:

This one worked fine for me, and it takes from 0 to 1 ms to be performed.

public static Mat bufferedImageToMat(BufferedImage bi) {   Mat mat = new Mat(bi.getHeight(), bi.getWidth(), CvType.CV_8UC3);   byte[] data = ((DataBufferByte) bi.getRaster().getDataBuffer()).getData();   mat.put(0, 0, data);   return mat; } 


回答3:

Don't want to deal with big pixel array? Simply use this

BufferedImage to Mat

public static Mat BufferedImage2Mat(BufferedImage image) throws IOException {     ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();     ImageIO.write(image, "jpg", byteArrayOutputStream);     byteArrayOutputStream.flush();     return Imgcodecs.imdecode(new MatOfByte(byteArrayOutputStream.toByteArray()), Imgcodecs.CV_LOAD_IMAGE_UNCHANGED); } 

Mat to BufferedImage

public static BufferedImage Mat2BufferedImage(Mat matrix)throws IOException {     MatOfByte mob=new MatOfByte();     Imgcodecs.imencode(".jpg", matrix, mob);     return ImageIO.read(new ByteArrayInputStream(mob.toArray())); } 


回答4:

I found a solution here. The solution is similar to Andriys.

Camera c; c.Connect(); c.StartCapture(); Image f2Img, cf2Img; c.RetrieveBuffer(&f2Img); f2Img.Convert( FlyCapture2::PIXEL_FORMAT_BGR, &cf2Img ); unsigned int rowBytes = (double)cf2Img.GetReceivedDataSize()/(double)cf2Img.GetRows();  cv::Mat opencvImg = cv::Mat( cf2Img.GetRows(), cf2Img.GetCols(), CV_8UC3, cf2Img.GetData(),rowBytes ); 


回答5:

One simple way would be to create a new using

Mat newMat = Mat(rows, cols, type); 

then get the pixel values from your BufferedImage and put into newMat using

newMat.put(row, col, pixel); 


回答6:

I use following code in my program.

protected Mat img2Mat(BufferedImage in) {         Mat out;         byte[] data;         int r, g, b;          if (in.getType() == BufferedImage.TYPE_INT_RGB) {             out = new Mat(in.getHeight(), in.getWidth(), CvType.CV_8UC3);             data = new byte[in.getWidth() * in.getHeight() * (int) out.elemSize()];             int[] dataBuff = in.getRGB(0, 0, in.getWidth(), in.getHeight(), null, 0, in.getWidth());             for (int i = 0; i > 0) & 0xFF);                 data[i * 3 + 1] = (byte) ((dataBuff[i] >> 8) & 0xFF);                 data[i * 3 + 2] = (byte) ((dataBuff[i] >> 16) & 0xFF);             }         } else {             out = new Mat(in.getHeight(), in.getWidth(), CvType.CV_8UC1);             data = new byte[in.getWidth() * in.getHeight() * (int) out.elemSize()];             int[] dataBuff = in.getRGB(0, 0, in.getWidth(), in.getHeight(), null, 0, in.getWidth());             for (int i = 0; i > 0) & 0xFF);                 g = (byte) ((dataBuff[i] >> 8) & 0xFF);                 b = (byte) ((dataBuff[i] >> 16) & 0xFF);                 data[i] = (byte) ((0.21 * r) + (0.71 * g) + (0.07 * b));             }         }         out.put(0, 0, data);         return out;     } 

Reference: here



回答7:

You can do it in OpenCV as follows:

File f4 = new File("aa.png"); Mat mat = Highgui.imread(f4.getAbsolutePath()); 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!