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

Blurred image after floodfill

$
0
0
I use the following code to obtain the biggest connected component in a binary image.
public class Connected {
    
    public static BufferedImage bigConnected(BufferedImage image){
        int height = image.getHeight();
	int width = image.getWidth();
	
        int white = 0xffffff;
        int red=0xff0000;
        int black=0x000000;
        
        int coordinates[] = new int[3];
        int pixelCount = 0;
        
	for (int x = 0; x < width; x++) {
		try {
                    for (int y = 0; y < height; y++) {
			int color = image.getRGB(x, y);
                        if(compareColor(color,black)){
                                pixelCount = floodFill(image,x,y,black,red);
                                if(pixelCount>coordinates[0]) {
                                    coordinates[0] = pixelCount;
                                    coordinates[1] = x;
                                    coordinates[2] = y;
        //                            for(int i : coordinates)
        //                                System.out.println("CoordinateArray "+i);
                                }   
                        }
                    }
                }catch (Exception e) {
                        e.getMessage();
		}
        }
        
        floodFill(image,coordinates[1],coordinates[2],red,black);
        
        for (int x = 0; x < width; x++) {
		try{
                    for (int y = 0; y < height; y++) {
			int color = image.getRGB(x, y);
                        if(compareColor(color,red)) {
                            image.setRGB(x,y,white);
                        }
                    }
                }catch (Exception e) {
                        e.getMessage();
                }
        }
        return image;
    }
    
    public static boolean compareColor(int iColor, int rColor){
        int tolerance = 5;
        int  iR   = (iColor & 0x00ff0000) >> 16;
        int  iG = (iColor & 0x0000ff00) >> 8;
        int  iB  =  iColor & 0x000000ff;
        int  rR   = (rColor & 0x00ff0000) >> 16;
        int  rG = (rColor & 0x0000ff00) >> 8;
        int  rB  =  rColor & 0x000000ff;
        if(Math.abs(iR-rR)<tolerance && Math.abs(iG-rG)<tolerance && Math.abs(iB-rB)<tolerance) {
            return true;
        }
        else {
            return false;
        }
    }
        
    public static final int stackSize = 16777216;
    public static int mod = 1000;
    public static int stack[] = new int[stackSize];
    public static int stackPointer = 0;
    
    public static int floodFill(BufferedImage image,int x, int y, int oldColor, int newColor){
        int w = image.getWidth();
        int h = image.getHeight();
        int p=0, vy=0;
        
        int pixelCount=0;
        
        boolean spanLeft, spanRight;

        emptyStack();
        push(convertCoor(x,y));
        
        p = pop();
        while (p!=-1) {
            y = p%mod;
            x = p/mod;
            vy = y;
            while (vy >= 0 && compareColor(image.getRGB(x,vy),oldColor)) {
                vy--;
            }
            vy++;
            spanLeft = spanRight = false;
            while (vy < h && compareColor(image.getRGB(x,vy),oldColor)) {
                image.setRGB(x, vy, newColor);
                pixelCount++;
                if (!spanLeft && x > 0 && compareColor(image.getRGB(x-1,vy),oldColor)) {
                    if (!push(convertCoor(x - 1, vy))) {
                        break;
                    }
                    spanLeft = true;
                } else if (spanLeft && x > 0 && !compareColor(image.getRGB(x-1,vy),oldColor)) {
                    spanLeft = false;
                }
                if (!spanRight && x < w - 1 && compareColor(image.getRGB(x+1,vy),oldColor)) {
                    if (!push(convertCoor(x + 1, vy))) {
                        break;
                    }
                    spanRight = true;
                } else if (spanRight && x < w - 1 && compareColor(image.getRGB(x+1,vy),oldColor)) {
                    spanRight = false;
                }
                vy++;
            }
        p=pop();
        }
    return pixelCount;
    }
    
    public static int convertCoor(int x, int y){
        return mod*x+y;
    }
    
    public static int pop(){ 
        if (stackPointer > 0) {
            int p = stack[stackPointer];
            stackPointer--;
            return p;
        } else {
            return -1;
        }
    }

    public static boolean push(int p) {
        if (stackPointer < stackSize - 1) {
            stackPointer++;
            stack[stackPointer] = p;
            return true;
        } else {
            return false;
        }
    }

    @SuppressWarnings("empty-statement")
    public static void emptyStack(){
        int p;
        while(pop()!=-1);
    }
}

public class DrawToPanel {
	static final double SCALE_FACTOR = .8;	// In percentage
	static final double DIVISION_FACTOR = .8; 	//In percentage
	static final int WINDOW_WIDTH = 640;		
	static final int WINDOW_HEIGHT = 480;
	static int THRESHOLD = 80;		
	static double pThreshold = 0; 
	BufferedImage bi=null;
	BufferedImage si = null;

	/** Change this path to the image location on your harddisk **/
	String imagePath = "D:\\Project\\Temp\\";   /** image name are just1.jpg, just2.jpg so adjust them accordingly, 1.jpg and 2.jpg are appended by the program itself beause I coded them so.**/
	int red, green, blue, alpha, gray;

	DrawToPanel(String title) {

		Dimension size = new Dimension((int) (WINDOW_WIDTH * DIVISION_FACTOR),
				(int) (WINDOW_HEIGHT * DIVISION_FACTOR));

		JFrame window = new JFrame();
		window.setTitle(title);

		JLabel lblDrawingPanel = new JLabel("Drawing Panel");
		DrawPanel drawingPanel = new DrawPanel();
	        drawingPanel.add(lblDrawingPanel,BorderLayout.EAST);
		
		JLabel lblRef = new JLabel("Reference Panel ");
		ReferencePanel referencePanel = new ReferencePanel();

		JPanel buttons = new JPanel();
		buttons.setLayout(new FlowLayout(FlowLayout.LEFT));
		drawingPanel.setPreferredSize(size);

		referencePanel.setPreferredSize(size);
		referencePanel.add(lblRef,BorderLayout.WEST);

		JButton btnOk = new JButton(" OK ");
		JButton btnCancel = new JButton(" Cancel ");

		window.add(drawingPanel, BorderLayout.EAST);
		window.add(referencePanel, BorderLayout.WEST);

		buttons.add(btnOk);
		buttons.add(btnCancel);

		window.add(buttons, BorderLayout.SOUTH);

		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		window.pack();
		window.setVisible(true);

	}

    

	class DrawPanel extends JPanel {
		/** This paint method actually puts the image inside JPanel **/
            @Override
		public void paint(Graphics g) {

			Graphics2D g2d = (Graphics2D) g;
			try {
				 bi = ImageIO.read(new File(imagePath + "1.jpg"));
				 si = ImageIO.read(new File(imagePath + "2.jpg"));
				 
				BufferedImage imageToThreshold =  Connected.bigConnected(si);
				
				 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
				 g2d.setRenderingHint(RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_QUALITY);
				 g2d.drawImage(imageToThreshold, 10, 10, (int) (WINDOW_WIDTH * SCALE_FACTOR),(int) (WINDOW_HEIGHT * SCALE_FACTOR), Color.WHITE, null);

			} catch (Exception e) {
				System.out.println(e.getMessage());
			}
                
		}
	}
	
	/**
	 * JPanel to displays the Reference Image.
	 * @author Asee Shrestha
	 *
	 */

class ReferencePanel extends JPanel {
    @Override
	public void paint(Graphics g) {
		Graphics2D g2d = (Graphics2D) g;
			try {
				 bi = ImageIO.read(new File(imagePath + "1.jpg"));
				g2d.drawImage(bi, 10, 10, (int) (WINDOW_WIDTH * SCALE_FACTOR),
						(int) (WINDOW_HEIGHT * SCALE_FACTOR), Color.RED, null);
			} catch (IOException e) {
				System.out.println(e.getMessage());
			}
		}
	}
}

public class Test {

	public static void main(String[] args) {
		DrawToPanel d = new DrawToPanel("Sample Window");
              
	}

}


Problem is that the images after processing looks a little washed out.

Posted Image

How can I retain the original sharpness of the image? I am a beginner in java so sorry for any stupid mistake :dontgetit:/>

Viewing all articles
Browse latest Browse all 51036

Trending Articles



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