Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Detect circles in an image, detects whole image instead?

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

In the case below, the whole image turns red...

 
public class Ass1 {
 
    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);
 
                        //----
                        // 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 col, int row, 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(col, row);
                }
        }
 
        static boolean isCenterOfCircle(int row, int col, int r, BufferedImage image) {
                
        	boolean circle = false; 
        	for (int col1=0; col1<image.getWidth(); col1++) {
        		for (int row1=0; row1<image.getHeight(); row1++) {
        	
       	    	 if(col1 != row1+r
       	     			|| col1 != row1-r
       	     			|| col1+r != row1
       	     			|| col1-r != row1){
       	    		  circle = true;

        		}else{
        			circle = false;
        			}
        		}
        	}
			return circle;
        	
        }
 
        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 col=0; col<image.getWidth(); col++) {
                        for (int row=0; row<image.getHeight(); row++) {
                                if (isCenterOfCircle(row,col,r,image)) {
                                        drawCircle(col,row,r,g2);
                                }
                        }
 
                }
        }
}


Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>