有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

在Jpanel上绘制OpenCV图像的java问题

我试图显示一幅从openCV库提供的VideoCapture对象捕获的图像。我制作了一个小GUI,它有一个jpanel组件,我想在上面描绘图像。这些图像被捕获为Mat对象,必须转换为BuffereImage才能显示在GUI上

我在以前的一个工作项目中做过这件事。然后,我从那里复制了大部分代码。问题是,当前项目上绘制的图像一团糟。我没有很好的方法来解释它是什么样子,所以我将在这里发布一个打印屏幕:

broken image

RemoteCam是jpanel的变量名。setRemoteCamImage是从读取videoCapture对象的外部线程调用的

代码:

public void setRemoteCamImage(Mat aMat){
    BufferedImage aImg = matToImg(aMat);
    if (!(aImg == null)) {
        Graphics g = RemoteCam.getGraphics();
        g.drawImage(aImg, 0, 0, RemoteCam.getWidth(), RemoteCam.getHeight(), 0, 0, aImg.getWidth(), aImg.getHeight(), null);
    }
}

public static BufferedImage matToImg(Mat in) {
    BufferedImage out;
    byte[] data = new byte[in.height() * in.width() * (int) in.elemSize()];
    int type;
    in.get(0, 0, data);

    if (in.channels() == 0) {
        type = BufferedImage.TYPE_BYTE_GRAY;
    } else {
        type = BufferedImage.TYPE_3BYTE_BGR;

        // Arange the colors from BGR to RGB
        byte b;
        for(int i = 0; i < data.length; i = i + 3){
            b = data[i];
            data[i] = data[i + 2];
            data[i + 2] = b;
        }
    }
    out = new BufferedImage(in.height(), in.width(), type);
    out.getRaster().setDataElements(0, 0, in.height(), in.width(), data);
    return out;
}

提前感谢您的回复


共 (0) 个答案