用Java将BufferedImage转换为Mat(OpenCV) [英] Converting BufferedImage to Mat (OpenCV) in Java

查看:3486
本文介绍了用Java将BufferedImage转换为Mat(OpenCV)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已尝试过链接,并提供以下代码。我的程序以BufferedImage格式导入图像,然后将其显示给用户。我在OpenCV中使用 matchingTemplate 功能。要求我将其转换为Mat格式。

I've tried this link and have the code below. My program imports images in a BufferedImage format and then displays it to the users. I'm using the matchingTemplate function in OpenCV which requires me to convert it to a Mat format.

如果导入图像代码有效 - >将其转换为Mat然后使用imwrite保存图像。该程序还允许用户裁剪图像,然后使用模板匹配以将其与另一个图像进行比较。我尝试将裁剪后的图像转换为Mat时出现问题我需要使用以下代码将其从Int转换为Byte:

The code works if I import the image -> convert it to Mat then save the image using imwrite. The program also allows the user to crop an image and then use the Template matching to compare it to another image. The issue come when I tried to convert the cropped image to Mat I need to convert it from Int to Byte using this code:

im = new BufferedImage(im.getWidth(), im.getHeight(),BufferedImage.TYPE_3BYTE_BGR);

然而,这会产生黑色图像。但是如果我摆脱它它只适用于导入的图像而不是裁剪。这里发生了什么?我确定它与coversion过程有关,因为我使用读入图像测试了模板匹配函数。

This however results in a black image. But if I get rid of it it only works with imported images and not cropped. What is going on here? I'm certain it is something to do with the coversion process as I have tested the template matching function using read in images.

// Convert image to 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;

}


推荐答案

你可以尝试这种方法,实际将图像转换为 TYPE_3BYTE_BGR (您的代码只是创建了一个相同大小的空白图像,这就是全黑的原因)。

You can try this method, to actually convert the image to TYPE_3BYTE_BGR (your code simply created a blank image of the same size, that is why it was all black).

用法:

// Convert any type of image to 3BYTE_BGR
im = toBufferedImageOfType(im, BufferedImage.TYPE_3BYTE_BGR);

// Access pixels as in original code

转换方法:

public static BufferedImage toBufferedImageOfType(BufferedImage original, int type) {
    if (original == null) {
        throw new IllegalArgumentException("original == null");
    }

    // Don't convert if it already has correct type
    if (original.getType() == type) {
        return original;
    }

    // Create a buffered image
    BufferedImage image = new BufferedImage(original.getWidth(), original.getHeight(), type);

    // Draw the image onto the new buffer
    Graphics2D g = image.createGraphics();
    try {
        g.setComposite(AlphaComposite.Src);
        g.drawImage(original, 0, 0, null);
    }
    finally {
        g.dispose();
    }

    return image;
}

这篇关于用Java将BufferedImage转换为Mat(OpenCV)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆