将Mat转换为BufferedImage

我在opencv中编码了一个侵蚀和膨胀,我尝试将Mat对象转换为BufferedImage,以便可以在JFrame中显示图像。 我已经尝试了这个链接中的代码如何匹配BufferedImage和Mat的颜色模型?

但它没有显示我想要的腐蚀/扩大的图像。

public class Erotion {
    public static void main (String[] args){
        try{
            System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

            File input = new File ("hallo.jpg");
            BufferedImage image = ImageIO.read(input);

            byte[] data = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();

            Mat source = new Mat(image.getHeight(), image.getWidth(), CvType.CV_8UC1);
            source.put (0,0, data);

            Mat destination = new Mat(image.getHeight(), image.getWidth(), CvType.CV_8UC1);

            destination = source;

            int erosion_size =  5;
            int dilation_size= 5;


            Mat element1 = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(2*dilation_size + 1, 2*dilation_size+1));


            Imgproc.dilate(source, destination, element1);

            BufferedImage image1 = new BufferedImage(destination.cols(),destination.rows(),BufferedImage.TYPE_BYTE_GRAY);

            byte[] data1 = new byte[destination.rows()*destination.cols()*(int)(destination.elemSize())];

            destination.get(0, 0, data1);
            image1.getRaster().setDataElements(0,0,destination.cols(),destination.rows(),data1);

            ImageIO.write(image1, "jpg", new File("dilation.jpg"));

        } catch(Exception e){
            System.out.println(" Error : " +e.getMessage());
        }
    }
}

这是我想要扩大的边缘检测图像

扩大图像

链接地址: http://www.djcxy.com/p/23703.html

上一篇: Convert Mat to BufferedImage

下一篇: What Operations can I use to extract the Hard Edges/Lines from this image