Convert Mat to BufferedImage

I coded a erosion and dilation in opencv, I tried to convert Mat object to BufferedImage so that the image can be displayed in JFrame. I have tried the code in this link How to match the color models of BufferedImage and Mat?

But it didn't show the eroded/dilated image as I want.

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());
        }
    }
}

Here is the edge detection image which I want to dilate

Dilated image

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

上一篇: 从背景中分离物体。 (使用轮廓检测​​)

下一篇: 将Mat转换为BufferedImage