Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 26th, 2013, 04:59 PM
lisa92 lisa92 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2011
Posts: 43 lisa92 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 h 31 m 45 sec
Reputation Power: 2
Should detect few circles in an image, detects WAY MORE!

Im using the symmetry property in the isCenterofCircle():check at a distance +r and -r away from the pixel(center)
(vertical and horizontal direction) to see if those 4 pixels are different than the center one. If yes, highlight in red.

However, it creates way more circles even if it's just empty space.

Code:
 
public class ***1 {
 
    public static void main(String[] args) {
                try {
                        // arg 0 is the input image name
                        BufferedImage img = ImageIO.read(new File(args[0]));
 
                        // arg 1 is the min radius
                        int minr = Integer.parseInt(args[1]);
                        
                        // arg 2 is the max radius
                        int maxr = Integer.parseInt(args[2]);
 
                        // if present, arg 3 is the max width we consider
                        int w = (args.length>3) ? Integer.parseInt(args[3]) : img.getWidth();
 
                        // if present, arg 4 is the max height we consider
                        int h = (args.length>4) ? Integer.parseInt(args[4]) : img.getHeight();
 
                        findCircle(minr, img, w, h);
 
                     //   System.out.println("Coordinates: (" + row + "," + column + ")\n Radius: " + minr);
                        //----
                        // write out the image
                        File outputfile = new File("outputimage2.png");
                        ImageIO.write(img, "png", outputfile);
                } catch (Exception e) {
                        e.printStackTrace();
                }
        }
 
        // Bresenham's algorithm to draw a circle
        // requires circle center and radius, as well as the
        // image and Graphics2D object with drawing colour already set.
        static void drawCircle(int cx,int cy,int r,Graphics2D g) {
                int f = 1-r;
                int ddF_x = 1;
                int ddF_y = -2 * r;
                int x = 0;
                int y = r;
 
                // draw cardinal points
                g.drawLine(cx,cy+r,cx,cy+r);
                g.drawLine(cx,cy-r,cx,cy-r);
                g.drawLine(cx+r,cy,cx+r,cy);
                g.drawLine(cx-r,cy,cx-r,cy);
 
                // draw 1/8 of the circle, taking advantage of symmetry
                while(x < y) {
                        if(f >= 0) {
                                y--;
                                ddF_y += 2;
                                f += ddF_y;
                        }
                        x++;
                        ddF_x += 2;
                        f += ddF_x;
 
                        g.drawLine(cx+x,cy+y,cx+x,cy+y);
                        g.drawLine(cx-x,cy+y,cx-x,cy+y);
                        g.drawLine(cx+x,cy-y,cx+x,cy-y);
                        g.drawLine(cx-x,cy-y,cx-x,cy-y);
                        g.drawLine(cx+y,cy+x,cx+y,cy+x);
                        g.drawLine(cx-y,cy+x,cx-y,cy+x);
                        g.drawLine(cx+y,cy-x,cx+y,cy-x);
                        g.drawLine(cx-y,cy-x,cx-y,cy-x);
                }
 
        }
        
        /**
         * I think that this method can just be called to get each pixel rather than copying all of the pixels into an
         * array.
         * 
         * @param col The column of the pixel to get.  If this isn't a valid pixel, then null is returned.
         * @param row The rowof the pixel to get.  If this isn't a valid pixel, then null is returned.
         * @param image The image to get the pixel from.
         * @return Returns the pixel specified by col and row.  If anything is wrong, then null is returned.
         */
        static Integer getPixel(int row, int col, BufferedImage image) {
                // check the indexes and return null for bad index or return a pixel for good index values
                if (image == null) {
                        return null;
                } else if (image.getWidth() <= 0) {
                        return null;
                } else if (image.getHeight() <= 0) {
                        return null;
                } else if (col < 0 || col >= image.getHeight()) {
                        return null;
                } else if (row < 0 || row >= image.getWidth()) {
                        return null;
                } else {
                        return image.getRGB(row, col);
                }
        }
 
        static boolean isCenterOfCircle(int row, int col, int r, BufferedImage image) {
                
  
        	if(getPixel(row,col,image) == getPixel(row+r,col,image)
       	    	 || getPixel(row,col,image) == getPixel(row-r,col,image)
       	    	    || getPixel(row,col,image) == getPixel(row,col+r,image)
       	    	        || getPixel(row,col,image) == getPixel(row,col-r,image)){
        		return true;
        	}else{
        	return false;
        	}
        }
 
        static void findCircle(int r, BufferedImage image, int w, int h) {
 
                // graphical output
                Graphics2D g2 = image.createGraphics();
                
                // use red
                g2.setColor(Color.RED);
 
                // for each pixel in the image, search for a circle, if found draw it in red
                for (int row=0; row<image.getWidth(); row++) {
                        for (int col=0; col<image.getHeight(); col++) {
                                if (isCenterOfCircle(row,col,r,image)) {
                                        drawCircle(row,col,r,g2);
                                }
                        }
 
                }
        }
}

Last edited by lisa92 : January 26th, 2013 at 06:06 PM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Detect circles in an image, detects whole image instead?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap