Click here to Skip to main content
15,867,594 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Java


package clustering;
import java.awt.Color;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;

public class Kmeans extends JFrame
{
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];

public Kmeans(BufferedImage image,int bins,int[] histogram)
{
this.histogram = histogram;
initialize(image,bins);
calcbounds();
/// calculateMean(histogram);
processImage(image, bins);
System.out.println("Hello.............");
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]=180;
// mean[1]=125;
// mean[2]=150;
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;
}
}
in above code i will convert image into three different clusters i want to give only largest region growing cluster how can i do that or give me suggestion to how can i extract only one region or only one cluster from the image in java
Posted

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