#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2011
    Posts
    43
    Rep Power
    4

    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 07:06 PM.

IMN logo majestic logo threadwatch logo seochat tools logo