Converting `BufferedImage` to `Mat` in OpenCV

后端 未结 9 1812
北荒
北荒 2020-11-29 04:49

How can I convert a BufferedImage to a Mat in OpenCV?

I\'m using the JAVA wrapper for OpenCV(not JavaCV

相关标签:
9条回答
  • 2020-11-29 05:12

    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);
    
    0 讨论(0)
  • 2020-11-29 05:13

    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 );
    
    0 讨论(0)
  • 2020-11-29 05:15

    You can do it in OpenCV as follows:

    File f4 = new File("aa.png");
    Mat mat = Highgui.imread(f4.getAbsolutePath());
    
    0 讨论(0)
提交回复
热议问题