Results 1 to 7 of 7
 07282011, 11:29 PM #1Member
 Join Date
 Jul 2011
 Posts
 2
 Rep Power
 0
Simple Shape Recognition Java Program
I'm having trouble with this program of mine. Essentially, it is supposed to take an image, and detect the shape (circle, square, rectangle, triangle, polygon). So far I have something that works, however when an image has two shapes on it instead of one, that's where I run into the problem...
Java Code:import algoritharium.*; import java.awt.Color; public class NameThatShape { 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 x = 0; x < img.getWidth()  1; x++){ for (int y = 0; y < img.getHeight()  1; y++) { while (c[x][y] == Color.BLACK && x < img.getWidth()) { row++; y++; } if (row > rowTotal) rowTotal = row; row = 0; } } System.out.println(rowTotal); 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) rowCount++; } if (rowCount > 0) { blackPixels[arrayNumber] = rowCount; arrayNumber++; rowCount = 0; } } double maxB = max(blackPixels); double meanB = mean(blackPixels); double varB = var(blackPixels); double stddevB = stddev(blackPixels); System.out.println(); //mean(blackPixels); //var(blackPixels); //stddev(blackPixels); System.out.println("Total # Rows: " + rowTotal); System.out.println("Height: " + maxB); System.out.println("Width: " + blackPixels.length); //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."); } 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); } public static double max(double[] a) { // Compute maximum value in 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) { // Compute the average of the values in a[] double sum = 0.0; for (int i = 0; i < a.length; i++) sum += a[i]; System.out.println("mean: " + sum / a.length); return sum / a.length; } public static double var(double[] a) { // Compute the sample variance of the values in 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) { double dev = Math.sqrt(var(a)); System.out.println("stddev: " + dev); return dev; } }
The shapes are in a .zip file attached to this thread.
ANY help would be much appreciated!!!
 07292011, 12:28 AM #2
If you can recognize a shape in a rectangle, can you make a rectangle around each shape and then look in that rectangle for what shape is in it?
 07292011, 04:01 AM #3Member
 Join Date
 Jul 2011
 Posts
 2
 Rep Power
 0
What my problem is, is that I'm not even sure where to start even detecting if there are two shapes in the image or not. The code I posted correctly identifies the shape if there is only one shape in the image...
 07292011, 01:51 PM #4where to start even detecting if there are two shapes in the image or not.
Are you given any information about the image to be scanned? Can it be any image from any source?
What assumptions can you make? If you have the whole world of images as possible input, the problem becomes very difficult. If you are given an image that has two shapes and the two shapes do not overlap both vertically and horizontally, then your code can be modified to detect the two shapes.
 09042011, 08:56 AM #5Member
 Join Date
 Aug 2011
 Posts
 1
 Rep Power
 0
I download your codes and everything but I can't even run it :( Something wrong with the Color[][] c = img.getPixelColors(); ?
 07312013, 08:47 PM #6Member
 Join Date
 Jul 2013
 Posts
 1
 Rep Power
 0
Re: Simple Shape Recognition Java Program
Hey friend you had done really great job.But for some shapes it gives wrong output and for some shapes it gives exception like "ArrayIndexOutOfBound".
While running i had taken shapes from your shapes folder.For shape 1 image it gives output triangle but it's oval!!!!
And for shape 6 and shape 9 images it gives exceptionArrayIndexOutOfBound. So can you please help me?
And one more question  Why are you calculating mean ,variance and standard deviation to detect images.Can you please explain in brief the conditions in if and else if statements(line no 57 to 68).So that i can get relationship between object detection and mean,variance calculation.
Thank you.
 07312013, 09:25 PM #7
Similar Threads

Simple java program help.
By CGHMN in forum New To JavaReplies: 35Last Post: 07152011, 12:31 PM 
can anione help mi with the shape recognition part of the java programming
By muhaimin in forum New To JavaReplies: 5Last Post: 04272011, 06:04 AM 
Simple Java program
By Rolle in forum New To JavaReplies: 3Last Post: 10262009, 04:05 PM 
Exponential Shape Program
By checkmylongboarding in forum New To JavaReplies: 3Last Post: 09232008, 03:31 AM 
help with simple java program
By leonard in forum New To JavaReplies: 3Last Post: 07302007, 09:40 AM
Bookmarks