Results 1 to 5 of 5
- 07-28-2011, 11:29 PM #1
Member
- 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...
Algoritharium is just a jar file that opens up an image viewer and is attached to this thread.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!!!
- 07-29-2011, 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?
- 07-29-2011, 04:01 AM #3
Member
- 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...
- 07-29-2011, 01:51 PM #4
What is the assigment?where 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.
- 09-04-2011, 08:56 AM #5
Member
- Join Date
- Aug 2011
- Posts
- 1
- Rep Power
- 0
Similar Threads
-
Simple java program help.
By CGHMN in forum New To JavaReplies: 35Last Post: 07-15-2011, 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: 04-27-2011, 06:04 AM -
Simple Java program
By Rolle in forum New To JavaReplies: 3Last Post: 10-26-2009, 04:05 PM -
Exponential Shape Program
By checkmylongboarding in forum New To JavaReplies: 3Last Post: 09-23-2008, 03:31 AM -
help with simple java program
By leonard in forum New To JavaReplies: 3Last Post: 07-30-2007, 09:40 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks