Join the Stack Overflow Community
Stack Overflow is a community of 6.6 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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<rows; r++){
              for(int c=0; c<cols; c++){
                  newMat.put(r, c, image.getRGB(r, c));
              }
          }

          Highgui.imwrite("Lena_copy.png",newMat);

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

share|improve this question
    
is it your custom java wrapper ? for javacv it is just IplImage.createFrom(bufferedImage). please post some code where u stucked ? – Nikson Kanti Paul Feb 19 '13 at 13:38
2  
Opencv has it's own Java binding as of version 2.4.4. opencv.org/opencv-java-api.html – Jompa234 Feb 19 '13 at 21:51
    
i see, did not check it yet, thanks – Nikson Kanti Paul Feb 20 '13 at 8:57
up vote 17 down vote accepted

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

share|improve this answer
3  
Well, there is something that viktorich didn't say in his answer. The BufferedImage object must be declared like this: img = new BufferedImage(320, 240, BufferedImage.TYPE_3BYTE_BGR);<br> img.setRGB(0, 0, 320, 240, intArray, 0, 320); In other way, the above code will not work. – Kevin Infante May 2 '14 at 23:10
    
The code will work in other cases, too. Both formats have to match, for example BufferedImage.TYPE_BYTE_GRAY and CV_8UC(1) works as well. – Mene Mar 16 '15 at 17:18

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;
}
share|improve this answer
    
Thank you. Above functionality worked for me with a little change. I am dealing with gray scale single channel images. I used Mat mat = new Mat(bi.getHeight(), bi.getWidth(), CvType.CV_8UC1); and it worked for me. – Karthik N G Dec 20 '16 at 15:38

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 );
share|improve this answer
    
That would require JNI. – Link TheProgrammer Feb 21 '15 at 22:19

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);
share|improve this answer

You can do it in OpenCV as follows:

File f4 = new File("aa.png");
Mat mat = Highgui.imread(f4.getAbsolutePath());
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.