Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By shengquan

Thread: Simple Shape Recognition Java Program

  1. #1
    TheShadow is offline Member
    Join Date
    Jul 2011
    Posts
    2
    Rep Power
    0

    Default 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;
    		
    	}
    }
    Algoritharium is just a jar file that opens up an image viewer and is attached to this thread.

    The shapes are in a .zip file attached to this thread.

    ANY help would be much appreciated!!!
    Attached Files Attached Files

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    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?

  3. #3
    TheShadow is offline Member
    Join Date
    Jul 2011
    Posts
    2
    Rep Power
    0

    Default

    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...

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    where to start even detecting if there are two shapes in the image or not.
    What is the assigment?
    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.

  5. #5
    shengquan is offline Member
    Join Date
    Aug 2011
    Posts
    1
    Rep Power
    0

    Default

    I download your codes and everything but I can't even run it :-( Something wrong with the Color[][] c = img.getPixelColors(); ?
    A-dia likes this.

  6. #6
    harsh is offline Member
    Join Date
    Jul 2013
    Posts
    1
    Rep Power
    0

    Default 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 exception-ArrayIndexOutOfBound. 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.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: Simple Shape Recognition Java Program

    harsh, please don't post to long dead threads and don't hijack another poster's thread. When you have a question, start your own thread -- they're free.

    db

    THREAD CLOSED
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Simple java program help.
    By CGHMN in forum New To Java
    Replies: 35
    Last Post: 07-15-2011, 01:31 PM
  2. Replies: 5
    Last Post: 04-27-2011, 07:04 AM
  3. Simple Java program
    By Rolle in forum New To Java
    Replies: 3
    Last Post: 10-26-2009, 05:05 PM
  4. Exponential Shape Program
    By checkmylongboarding in forum New To Java
    Replies: 3
    Last Post: 09-23-2008, 04:31 AM
  5. help with simple java program
    By leonard in forum New To Java
    Replies: 3
    Last Post: 07-30-2007, 10:40 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •