使用JAVA进行图像细化 [英] Image Thinning using JAVA

查看:108
本文介绍了使用JAVA进行图像细化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!
我用Java编写了一些代码,将彩色图像转换为黑白图像,然后尝试对该灰度图像进行细化处理.黑白转换成功完成,但是图像细化仍然无法提供正确的输出.请帮助我解决问题.我的代码如下:

Hi!
I have written some code in java to convert a colored image into black and white image and then tried to perform thinning on that gray-scale image. Black and white conversion is done successfully, but image thinning is still not giving correct output. Kindly help me in fixing my problem. My code is as follows:

 //colored image to black and white conversion; black and white image to thinned image.

public static void main(String[] args) 
{
    try
    {
        //colored image path
        BufferedImage colored_image = ImageIO.read(new File("D:\\logo.jpg"));
        //getting width and height of image
        double image_width = colored_image.getWidth();
        double image_height = colored_image.getHeight();
        BufferedImage img = colored_image;

        //drawing a new image
        BufferedImage bimg = new BufferedImage((int)image_width, (int)image_height, BufferedImage.TYPE_BYTE_GRAY);
        Graphics2D gg = bimg.createGraphics();
        gg.drawImage(img, 0, 0, img.getWidth(null), img.getHeight(null), null);

        //saving black and white image onto drive
        String temp = "logo in blackAndwhite.jpeg";
        File fi = new File("D:\\" + temp);
        ImageIO.write(bimg, "jpg", fi);

        //thinning by resizing gray scale image to desired eight and width
        BufferedImage bimg2 = new BufferedImage((int)image_width, (int)image_height, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2 = bimg2.createGraphics();

        // Perform your drawing here
        g2.setColor(Color.BLACK);
        g2.drawLine(0, 0, 200, 200);

       //saving thinned image onto drive
       String temp2 = "logo thinned.jpeg";
       File fi2 = new File("D:\\" + temp2);
       ImageIO.write(bimg2, "jpg", fi2);
       //g2.dispose();
    }
    catch (Exception e)
    {
        System.out.println(e);
    }       
 }

推荐答案

我已经看到了您的代码.在我看来,您不是要对图像进行细化处理,而是仅将图像保存为灰度(?).为了细化图像,请根据以下算法进行功能设置:

1)细化是一个迭代过程.比其他成像过程需要更长的时间.

2)如果您的图像是RGB格式,请将其更改为灰度".实际上,最好将格式更改为BW(二进制或二进制).

3)您需要结构元素"来细化图像.结构元素是3 x 3的矩阵值,分别为"1"和"0".例如:
[0 0 0]
[x 1 x]
[1 1 1]
(请注意,"x"表示空元素.这意味着您在过程中不会考虑它.仅考虑"1"和"0"元素).另外,请注意,"0"表示前景,"1"表示背景(可以反转).

3)现在,您有了BW图像(原始图像)和支撑元件.

4)现在,我们进行逐像素操作,即将结构元素的中心像素放到BW图像中的第一个像素"上.如果结构元素中的"1"(或"0")的值与图像中的"1"(或"0")的值完全匹配(我们的图像为BW格式,则只有两个值"1")和"0"),则将结构元素原点下方的图像像素设置为"1"(背景).否则,它保持不变.此操作将应用于图像中的所有像素.

此过程称为命中或错过"转换.请执行此操作,直到检测不到任何更改为止(执行几次).

5)然后,对原始图像和命中或未命中"转换图像的结果进行逻辑减法(可选).这样会得到更好的结果.

6)您获得了BW格式的细化图像.

问候.
I have seen your code. It seems to me you are not doing thinning of an image, but saving the image in gray scale only(?). In order to thinning an image, please make your function based on the below algorithm:

1) Thinning is an iterative process. It takes longer time than other imaging procedure.

2) If your image is in RGB format, change it to Grayscale. Actually, changing format to BW (bitonal or binary) is the best.

3) You need "Structuring Element" for thinning an image. Structuring Element is a 3 x 3 matrix values of "1" and "0". For example:
[0 0 0]
[x 1 x]
[1 1 1]
(Note that "x" indicates empty element. That means you do not take consider it during the procedure. Only consider "1" and "0" elements). Also, note that "0" indicates foreground and "1" indicates background (you may reverse it).

3) Now, you have BW image (original image) and struturing element.

4) Now, we do pixel-wise operation, namely, put the structuring element''s center pixel to the "first pixel" in the BW image. If the values of "1" (or "0") in the structuring element exactly match the value of "1" (or "0") in the image (our image is in BW format and we have only two values "1" and "0"), then the image pixel underneath the origin of the structuring element is set to "1" (background). Otherwise it is left unchanged. This operation will be applied all pixels in the image.

THIS procedure called "Hit Or Miss" transformation. Please do this until no changes will be detected (do it several times).

5) Then, take a logical subtraction of Original image and result of the "Hit Or Miss" transformed image (optional). This will be given better result.

6) You got the thinning image in BW format.

Regards.


我更改了格式-您很快就会看到代码在某个时候变成了粉红色:

I changed the formatting - and you will soon see that the code goes all pink at a certain point:

//saving black and white image onto drive
        String temp = "logo in blackAndwhite.jpeg";
        File fi = new File("D:\\" + temp);
        ImageIO.write(bimg, "jpg", fi);



您只需用双反斜线遮盖"即可.



you simply masked the " with the double backslash!

//saving black and white image onto drive
        String temp = "logo in blackAndwhite.jpeg";
        File fi = new File("D:/" + temp);
        ImageIO.write(bimg, "jpg", fi);



看起来好多了.



looks simply better.


这篇关于使用JAVA进行图像细化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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