从图像中提取道路

时间:2013-11-30 14:14:03

标签: java image-processing

我正在做最后一年的项目,我从图像中提取道路。我拍摄图像,图像可以分为三个 在我尝试选择image divide into three different cluster后使用k-means聚类算法的不同聚类 该图像中连接最长的组件集群但未获得 我想要什么。我想删除该图像的所有部分(除外 道路)或想要从该图像只能看到道路。怎么做呢

输入图片:

Input Image

输出图片:

Output Image

我的代码:

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.awt.image.Kernel;
import java.awt.image.ConvolveOp;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class design1 {
    static BufferedImage image;
    static int[] histogram = new int[256];

    public void makeHistogram() throws IOException {
        image = ImageIO.read(new File("input.png"));
        for (int i = 0; i < 256; i++)
            histogram[i] = 0;
        for (int h = 0; h < image.getHeight(); h++) {
            for (int w = 0; w < image.getWidth(); w++) {
                Color color = new Color(image.getRGB(w, h));
                int greyvalue = color.getRed();
                histogram[greyvalue] += 1;
            }
        }
    }

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

        new Kmeans1(image, 3, histogram);
    }

}

k-mean clustering

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class Kmeans1 {
    BufferedImage image_temp;
    boolean not_terminated;
    int loops, changedPixels;
    int[] histogram;
    static int count = 0;
    int[] lowerbounds = new int[3];
    static int g = 0;
    int[] lb = new int[3];
    int[] ub = new int[3];
    int[] mean = new int[3];
    BufferedImage imaged;
    BufferedImage thresholdimage, dilateimage;

    public Kmeans1(BufferedImage image, int bins, int[] histogram)
            throws IOException {
        this.histogram = histogram;
        initialize(image, bins);
        calcbounds();

        // / calculateMean(histogram);
        processImage(image, bins);
        imaged = returnimage();
        imageWrite();
        for (int j = 0; j < 3; j++) {
            System.out.println("LB" + j + "=" + lb[j] + " UB" + j + "=" + ub[j]
                    + " Means" + j + "=" + mean[j]);
        }
    }

    public void initialize(BufferedImage image, int bins) {
        image_temp = image;
        not_terminated = true;
        mean[0] = 173;
        mean[1] = 100;
        mean[2] = 112;
    }

    public int createmean(BufferedImage image, int i, int bins) {
        int pixelindex = 0;
        int sum = 0;
        int value = 0;
        for (int h = 0; h < image.getHeight(); h++) {
            for (int w = 0; w < image.getWidth(); w++) {
                try {
                    pixelindex += 1;
                    if (pixelindex % bins == i) {
                        Color rgb = new Color(image.getRGB(w, h));
                        sum += rgb.getRed();
                        value += 1;
                        count++;
                    }
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        return sum / value;

    }

    public void calcbounds() {
        for (int j = 0; j < 3; j++) {
            int lb1 = calculatelb(j);
            int ub1 = calculateub(j);
            lowerbounds[j] = lb1;
            lb[j] = lb1;
            ub[j] = ub1;
        }

    }

    private int calculatelb(int index) {
        int cMean = mean[index];
        int currentBound = 0;
        for (int i = 0; i < 3; i++) {
            if (cMean > mean[i]) {
                currentBound = Math.max((cMean + mean[i]) / 2, currentBound);
            } else {
            }
        }
        return currentBound;
    }

    private int calculateub(int index) {
        int cMean = mean[index];
        int currentBound = 255;
        for (int i = 0; i < 3; i++) {
            if (cMean < mean[i]) {
                currentBound = Math.min((cMean + mean[i]) / 2, currentBound);
            } else {
            }
        }
        return currentBound;
    }

    private void processImage(BufferedImage image, int bins) {
        int delta = 255 / (bins - 1);

        for (int h = 0; h < image.getHeight(); h++) {
            for (int w = 0; w < image.getWidth(); w++)

            {
                Color rgb = new Color(image.getRGB(w, h));
                int grey = rgb.getRed();

                for (int i = 0; i < 3; i++) {
                    if (grey > lb[i] && grey < ub[i]) {
                        g = i * delta;
                        image_temp.setRGB(w, h, (new Color(g, g, g)).getRGB());
                    } else {
                        image_temp.setRGB(w, h, (new Color(g, g, g)).getRGB());
                    }
                }
            }
        }
    }

    public BufferedImage returnimage() {
        return image_temp;
    }

    // BufferedImage img1=image_temp;
    public void imageWrite() throws IOException {
        // BufferedImage img1=image_temp;
        ImageIO.write(imaged, "jpg", new File("output.jpg"));
        System.out.println("image write completed");
    }


}

0 个答案:

没有答案