Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在OpenCV中将BuffereImage转换为Mat_Java_Opencv - Fatal编程技术网

Java 在OpenCV中将BuffereImage转换为Mat

Java 在OpenCV中将BuffereImage转换为Mat,java,opencv,Java,Opencv,当我尝试使用此函数将BuffereImage转换为Mat时 public Mat matify(BufferedImage im) { // Convert INT to BYTE //im = new BufferedImage(im.getWidth(), im.getHeight(),BufferedImage.TYPE_3BYTE_BGR); // Convert bufferedimage to byte array byte[] pixels = ((DataBufferByte)

当我尝试使用此函数将BuffereImage转换为Mat时

public Mat matify(BufferedImage im) {
// Convert INT to BYTE
//im = new BufferedImage(im.getWidth(), im.getHeight(),BufferedImage.TYPE_3BYTE_BGR);
// Convert bufferedimage to byte array
byte[] pixels = ((DataBufferByte) im.getRaster().getDataBuffer())
        .getData();

// Create a Matrix the same size of image
Mat image = new Mat(im.getHeight(), im.getWidth(), CvType.CV_8UC3);
// Fill Matrix with image values
image.put(0, 0, pixels);

return image;

}
我得到这个错误

Exception in thread "main" java.lang.UnsupportedOperationException: Provided data element number (7955216) should be multiple of the Mat channels count (3)
at org.opencv.core.Mat.put(Mat.java:2549)
at Main.matify(Main.java:78)
at Main.doOpenCV(Main.java:48)
at Main.main(Main.java:40)
错误是由这一行引起的

图像输出(0,0,像素)


那么为什么我会犯这个错误呢?如何将BuffereImage转换为java中的opencv Mat?

这对我很有用

public Mat bufferedImageToMat(BufferedImage bi) {
    Mat mat = new Mat(bi.getHeight(), bi.getWidth(), CV_8UC(3));

    int r, g, b;
    UByteRawIndexer indexer = mat.createIndexer();
    for (int y = 0; y < bi.getHeight(); y++) {
        for (int x = 0; x < bi.getWidth(); x++) {
            int rgb = bi.getRGB(x, y);

            r = (byte) ((rgb >> 0) & 0xFF);
            g = (byte) ((rgb >> 8) & 0xFF);
            b = (byte) ((rgb >> 16) & 0xFF);

            indexer.put(y, x, 0, r);
            indexer.put(y, x, 1, g);
            indexer.put(y, x, 2, b);
        }
    }
    indexer.release();
    return mat;
}
public Mat bufferedImageToMat(BufferedImage bi){
Mat Mat=新Mat(bi.getHeight(),bi.getWidth(),CV_8UC(3));
int r,g,b;
UByteRawIndexer indexer=mat.createIndexer();
对于(int y=0;y>0)和0xFF);
g=(字节)((rgb>>8)和0xFF);
b=(字节)((rgb>>16)和0xFF);
索引器put(y,x,0,r);
索引器put(y,x,1,g);
索引器put(y,x,2,b);
}
}
index.release();
返回垫;
}

这对我来说很有用

public Mat bufferedImageToMat(BufferedImage bi) {
    Mat mat = new Mat(bi.getHeight(), bi.getWidth(), CV_8UC(3));

    int r, g, b;
    UByteRawIndexer indexer = mat.createIndexer();
    for (int y = 0; y < bi.getHeight(); y++) {
        for (int x = 0; x < bi.getWidth(); x++) {
            int rgb = bi.getRGB(x, y);

            r = (byte) ((rgb >> 0) & 0xFF);
            g = (byte) ((rgb >> 8) & 0xFF);
            b = (byte) ((rgb >> 16) & 0xFF);

            indexer.put(y, x, 0, r);
            indexer.put(y, x, 1, g);
            indexer.put(y, x, 2, b);
        }
    }
    indexer.release();
    return mat;
}
public Mat bufferedImageToMat(BufferedImage bi){
Mat Mat=新Mat(bi.getHeight(),bi.getWidth(),CV_8UC(3));
int r,g,b;
UByteRawIndexer indexer=mat.createIndexer();
对于(int y=0;y>0)和0xFF);
g=(字节)((rgb>>8)和0xFF);
b=(字节)((rgb>>16)和0xFF);
索引器put(y,x,0,r);
索引器put(y,x,1,g);
索引器put(y,x,2,b);
}
}
index.release();
返回垫;
}

对于该特定示例,缓冲图像必须声明为
缓冲图像。键入\u 3BYTE\u BGR
(如注释行所示)。不清楚你的情况是什么类型的
im
。我也面临着同样的错误。您是否得到了此问题的任何解决方案,然后请针对该特定示例,必须将缓冲图像声明为
buffereImage。键入\u 3BYTE\u BGR
(如注释行所示)。不清楚你的情况是什么类型的
im
。我也面临着同样的错误。你有没有得到这个问题的解决方案?请发邮件