Click here to Skip to main content
15,881,044 members
Please Sign up or sign in to vote.
2.33/5 (2 votes)
i am doing project of final year in that i Extract road from image .in that i take image , image can be segment into three different cluster by using k-means clustering algorithm that image divide into three different cluster after i tried to select longest connected component cluster from that image but not getting what i want .i want to delete all the portion from that image (Except Road)or want to visible only road from that image . how can do that
o input image
o output image
o Expected Output
Java
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");
	}

}
</code>
main method
<code> //
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);
	}

}
Posted
Updated 1-Dec-13 21:15pm
v4
Comments
Peter Leow 2-Dec-13 2:50am    
Question is not complete?
baliram bhande 2-Dec-13 3:28am    
question is that i have given image in that i want to extract road from that image means we can remove all part from that image (Except road)
Richard MacCutchan 2-Dec-13 3:13am    
Please use <pre> tags around your code; see the code link above the edit window.
baliram bhande 2-Dec-13 3:23am    
yes sir

1 solution

visit here...

road Extraction from image[^]
 
Share this answer
 
Comments
baliram bhande 2-Dec-13 3:45am    
i will agree with you but problem is that road color is same as other part of image (i.e house,building ,other obstacles)ex. road color is black and building color is black or any other small object is black color how we can delete other small object because of that we can extract only road part of that image .

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900