我正在尝试在blob的二进制映像上实现Connected Components。我试图找到一个像素的4个邻居,如果像素是黑色的。我知道像素的4个邻居是(x-1,y),(x + 1,y),(x,y-1),(x,y + 1)但我在如何绘制空白实际实现它,并将相邻像素存储到自己的标签中(我当时想尝试使用HashTable并存储像素和标签)。
到目前为止,我有
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) {
}
}
}