2

我在这里阅读了这篇文章,建议我正在做的事情应该起作用:Converting BufferedImage to Mat in opencv

谁能告诉我为什么我的输出与桌面左上角 10x10 像素的输出有如此大的不同?

编辑:

原始 10x10 图像(基本上是 Eclipse 徽标):http ://tinypic.com/r/35l5clt/5

加载到垫子后: http: //tinypic.com/r/af7w90/5

public static void main(String[] args) throws InterruptedException,
        AWTException, IOException {

    byte[] bgrPixelData = ScreenToImage.GetCurrentScreenImage();
    Mat screenFrame = new Mat(10, 10, CvType.CV_8UC3);
    screenFrame.put(0, 0, bgrPixelData);

    // Create a blank output image, that we will draw onto.
    Mat outputFrame = new Mat(screenFrame.size(), CvType.CV_8UC3);

    // Save output and display the openCV Mat image onto the screen.
    ImageToScreen.DrawImageToScreen("c:\\temp\\wtf.png", outputFrame);
}


public class ScreenToImage {

public static byte[] GetCurrentScreenImage() throws AWTException,
        IOException {
    Robot robot = new Robot();
    Dimension d = new Dimension();
    d.height = 10;
    d.width = 10;

    BufferedImage screenShot = robot.createScreenCapture(new Rectangle(d));

    BufferedImage bgrScreenshot = new BufferedImage(screenShot.getWidth(),
            screenShot.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
    boolean done = bgrScreenshot.getGraphics().drawImage(screenShot, 0, 0, null);

    return ((DataBufferByte) bgrScreenshot.getRaster().getDataBuffer())
            .getData();
}
}
static {
    System.loadLibrary("opencv_java246");
}

public class ImageToScreen extends JFrame{

private static final long serialVersionUID = 1L;

public static void DrawImageToScreen(String imgStr, Mat m){
    Highgui.imwrite(imgStr, m);
    JFrame frame = new JFrame("Screen Capture");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setResizable(true);
    frame.setLocationRelativeTo(null);

    ImageIcon image = new ImageIcon(imgStr);
    frame.setSize(image.getIconWidth()+10,image.getIconHeight()+35);
    // Draw the Image data into the BufferedImage
    JLabel label1 = new JLabel(" ", image, JLabel.CENTER);
    frame.getContentPane().add(label1);

    frame.validate();
    frame.setVisible(true);
}
}
4

1 回答 1

2

您正在创建空图像

Mat outputFrame = new Mat(screenFrame.size(), CvType.CV_8UC3);

然后你将它保存到文件并重新读取为BufferedImage

Highgui.imwrite(imgStr, m);
ImageIcon image = new ImageIcon(imgStr);

因此,该文件包含一些来自内存的随机值。尝试ImageToScreen.DrawImageToScreenscreenFrame对象调用,它应该工作。

您也可以BufferedImage直接从Mat(不使用临时文件)创建:

public static BufferedImage createBufferedImage(Mat mat) {
    BufferedImage image = new BufferedImage(mat.width(), mat.height(), BufferedImage.TYPE_3BYTE_BGR);
    WritableRaster raster = image.getRaster();
    DataBufferByte dataBuffer = (DataBufferByte) raster.getDataBuffer();
    byte[] data = dataBuffer.getData();
    mat.get(0, 0, data);
    return image;
}
于 2014-01-17T16:23:17.170 回答