0

I am trying to implement Connected Components on a binary image of blobs. I am trying to find the 4-neighbors of a pixel, if the pixel is black. I know that the 4-neighbors of pixel are (x-1, y) , (x+1, y), (x, y-1), (x, y+1) But I am drawing a blank on how to actually implement it and to store the neighboring pixels into their own label (I was thinking to try and use a HashTable and store the pixel and label).

What I have so far is

public static void main(String[] args) throws IOException {

   CheckPixel();

}

public static void CheckPixel() throws IOException {

BufferedImage blackwhiteimage = ImageIO.read(new File("image.png"));
File bwFile = new File("image.png");
BufferedImage image1 = ImageIO.read(bwFile);

int w = image1.getWidth();
int h = image1.getHeight();

int[][] cc = new int[w][h];
int nextLabel = 0;

ArrayList<ArrayList<Integer>>();
Hashtable<Integer, Integer> map = new Hashtable<Integer, Integer>();

int[][] pixel = new int[w][h];

for (int i = 0; i < w; i++) {
    for (int j = 0; j < h; j++) {
        cc[i][j] = image1.getRGB(i, j);

        if ((cc[i][j] & 0xff) != 0) {

            //get the neighbors
            //neighbors = connected elements with the current elements value
            if ((cc[i - 1][j] & 0xff) !=0) {


            }

            if ((cc[i + 1][j] & 0xff) != 0) {

            }

            if ((cc[i][j - 1] & 0xff) != 0) {

            }

            if ((cc[i][j + 1] & 0xff) != 0) {

            }
        }

    }
Sherin
  • 13
  • 5
  • What do you mean by "label"? And you intend on basically checking if there is a black pixel above below left and right and if there is, assign it to it's own "neighborhood" where the center pixel represents the neighborhood and it contains a set of points that are in the neighborhood? Like say you have points (2, 3) ,(3, 3), and (2, 4), then (2, 3) is the center, coords representing the "name" or "ID" of the neighborhood. That neighborhood contains the pixels (2, 3), (3, 3), and (2, 4)? – JustAFellowCoder Mar 26 '19 at 20:12
  • If true, create class called neighborhood, in your main class create an arraylist that holds neighborhoods. For each pixel that is black check above, below, left and right. if any of those are there, create a new neighborhood which has the constructor of (x, y) being the center. Then add to a list in the neighborhood class that holds all the points, the center point. So neighborhood class has an ID (x, y) for the constructor, and an addPixel(x, y) which adds the pixels to a neighborhood list for neighborhood object. Store objects in an array. Neighborhood has getID which returns center (x, y) – JustAFellowCoder Mar 26 '19 at 20:16
  • @JustAFellowCoder thanks for the reply, I will give your suggestion a try, – Sherin Mar 26 '19 at 20:22

0 Answers0