Recognize shapes from pictures
Hello, I'm currently working on a project that asks us to "Identify the number of shapes in each image and the type for each shape". The image backgrounds are white and the shapes are black. Some of the images have only one shape in them while others have two. I've coded the program to recognize the shape names for the images with only one shape in them but I'm having trouble identifying when there is more than one shape. Here's the link to the full assignment along with the images we should be using: http://people.uncw.edu/tompkinsj/121...ThatShape.html
This is my program so far (it identifies each shape correctly when it's the only shape in the image): Code:
import algoritharium.*;
import java.awt.Color;
public class Shapes {
public static void main(String[] args) {
new ImageViewer();
}
public static void identifyShape() {
Image img = ImageViewer.getImage();
Color[][] c = img.getPixelColors();
recolor(c);
int rowCount=0;
int row=0;
int rowTotal=0;
//Number of rows with black pixels
for(int q=0; q<img.getWidth(); q++){
for(int w=0; w<img.getHeight(); w++) {
while(c[q][w]==Color.BLACK && w<(img.getHeight())) {
row++;
w++;
}
if (row>rowTotal)
rowTotal=row;
row=0;
}
}
double blackPixels[]=new double [rowTotal];
int arrayNumber=0;
//Number of black pixels per row
for(int y=0; y<img.getHeight(); y++){
for(int x=0; x<img.getWidth(); x++) {
if(c[x][y]==Color.BLACK)
//while (c[x][y]==Color.BLACK&&x<img.getWidth()){
rowCount++;
//x++;
}
if (rowCount>0){
blackPixels[arrayNumber]=rowCount;
arrayNumber++;
rowCount=0;
}
}
double maxB= max(blackPixels);
double meanB= mean(blackPixels);
double stddevB= stddev(blackPixels);
double varB= var(blackPixels);
System.out.println();
mean(blackPixels);
var(blackPixels);
stddev(blackPixels);
System.out.println(rowTotal);
//ID SHAPES HERE
if (maxB==meanB&&rowTotal==maxB)
System.out.println("The shape is a square.");
else if (maxB==meanB&&rowTotal!=maxB)
System.out.println("The shape is a rectangle.");
else if (maxB==rowTotal&&stddevB!=varB)
System.out.println("The shape is a circle.");
else if(.5*maxB<meanB&&rowTotal>maxB)
System.out.println("The shape is a triangle.");
else if(meanB!=maxB&&(1/3)*maxB==(1/2)*rowTotal)
System.out.println("The shape is a polygon.");
else
System.out.println("The shape is unknown.");
}
public static double max(double[]a) {
double max=Double.NEGATIVE_INFINITY;
for(int i=0; i<a.length; i++)
if(a[i]>max) max=a[i];
System.out.println("max= "+max);
return max;
}
public static double mean(double []a) {
double sum=0.0;
for(int i=0; i<a.length;i++){
sum=sum+a[i];
}
System.out.println("mean= "+sum/a.length);
return sum/a.length;
}
public static double var(double[]a) {
double avg=mean(a);
double sum=0.0;
for(int i=0; i<a.length;i++)
sum+=(a[i]-avg)*(a[i]-avg);
System.out.println("var= "+sum/(a.length-1));
return sum/(a.length-1);
}
public static double stddev (double[]a){
System.out.println("stddev= "+ Math.sqrt(var(a)));
return Math.sqrt(var(a));
}
private static void recolor(Color[][] c) {
for(int y=0; y < c.length; y++)
for(int x=0; x < c[y].length; x++)
if (c[y][x].getRed() < 50)
c[y][x] = Color.BLACK;
else c[y][x] = Color.white;
ImageViewer.createImage(c);
}
}
p.s. algoritharium is a .jar file
can anyone point me in the right direction to identify when an image has two shapes in it? thank you!