Click here to Skip to main content
15,888,816 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
i am working on image processing projects in that i have given specific pixel i want to find out all pixel which are connected to that pixel.ex. i have give white pixel for specific position i want to find all connected pixel to the white (only connected to white ) how can do that or how can check pixel are connected or not.
Posted
Comments
TorstenH. 6-Dec-13 8:24am    
do I get this right?

You've set a pixel on a grid (10:10) on a specific position (like 3:5).
And now you want to figure wich grid positions are aside?

by using Flood Fill algorithm i have done this


Java
import java.awt.image.BufferedImage;

import javax.imageio.ImageIO;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import java.awt.Point;
import java.io.File;
 
class mm1
{
	  private static void repaintAndDelay(BufferedImage image)
	  {
	    _imageLabel.setIcon(new ImageIcon(image));
	    _imageLabel.repaint();
	    try { Thread.sleep(1); } catch (Exception ignore) {}
	  }
	 
  public static void floodFill(BufferedImage image, int x,int y, int fillColor)
  {
    java.util.ArrayList<point> examList=new java.util.ArrayList<point>();
 
    int initialColor=image.getRGB(x,y);
    examList.add(new Point(x,y));
 
    while (examList.size()>0)
    {
      Point p = examList.remove(0);
      if (image.getRGB(p.x,p.y)==initialColor)
      {
        x = p.x;  y = p.y;
        image.setRGB(x, y, fillColor); 
 examList.add(new Point(x-1,y));        
        examList.add(new Point(x+1,y));        
        examList.add(new Point(x,y-1));        
        examList.add(new Point(x,y+1));        
        repaintAndDelay(image); 
      }
    }
 
  }
  public static int packRgb(int r,int g,int b)
  {
    return (r*256+g)*256+b;
  }
 static JLabel _imageLabel;
  public static void main(String[] args) throws Exception
  {
    BufferedImage image=ImageIO.read(new File("test1.png"));
    JLabel imageLabel=new JLabel();
    _imageLabel = imageLabel; 
    imageLabel.setIcon(new ImageIcon(image));
   javax.swing.JFrame window=new javax.swing.JFrame();
    window.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
    window.add(imageLabel);
  window.pack();
    window.setVisible(true);
    int yellow = packRgb(255,0,0);
    int x=100, y=100;  
    floodFill(image,x,y, yellow);
 
    imageLabel.setIcon(new ImageIcon(image));
    
 
  }
}</point></point>
 
Share this answer
 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900