检测图像的像素块

时间:2017-08-08 16:25:48

标签: java image-processing

我正在研究Java中的代码,它应该检测像素区域并创建一个列表,其中列表的像素坐标相互连接。 对于我想要创建新列表的每个区域。

我写了一个方法,首先应检测区域的边缘,然后检查像素是否连接到另一个。我移交黑/白图像并将其转换为2D像素阵列。在每个索引中,我设置图像的等效像素的颜色。但它不起作用。

我的问题:是否还有其他方法可以分隔黑/白图像的黑色区域以获得列出的坐标?

示例:

我将此图像保存到2D整数数组中,如果颜色为黑色,则此索引处的值为" 1"否则,如果颜色是白色,那么" -1"。

我想获得2个列表(因为有2个单独的区域)

  

第一个清单:坐标[(1,1),(2,1),(0,2),(1,2),(2,2)]

     

第二个清单:坐标[(6,2),(4,3),(5,3),(6,3),(7,3),(4,4) ,(5,4),(6,4)]

Pixel Blocks with Coordinates

我的代码:

private static List<Group> detectGroups(BufferedImage image) {
    int[][] colorArray = imageToColorArray(image);
    List<Coordinate> edgeCoordinates = new ArrayList<Coordinate>();
    List<Group> pixelGroups = new ArrayList<Group>(); // empty now
    // Detect Edges
    for (int y = 1; y < colorArray[0].length - 1; y++) {
        for (int x = 1; x < colorArray.length - 1; x++) {
            if (colorArray[x][y] == black && colorArray[x - 1][y] == white
                    || colorArray[x][y] == black && colorArray[x + 1][y] == white
                    || colorArray[x][y] == black && colorArray[x][y - 1] == white
                    || colorArray[x][y] == black && colorArray[x][y + 1] == white) {
                colorArray[x][y] = edge;
                edgeCoordinates.add(new Coordinate(x, y));
            }
        }
    }
    // Detect Groups
    for (int i = 0; i < edgeCoordinates.size(); i++) {
        Coordinate index = edgeCoordinates.get(i);
        int x = index.getX();
        int y = index.getY();

        Coordinate c = new Coordinate(x, y);
        Coordinate topLeftFromPixel = new Coordinate(x - 1, y - 1);
        Coordinate topMiddleFromPixel = new Coordinate(x, y - 1);
        Coordinate topRightFromPixel = new Coordinate(x + 1, y - 1);
        Coordinate middleLeftFromPixel = new Coordinate(x - 1, y);
        Coordinate middleRightFromPixel = new Coordinate(x + 1, y);
        Coordinate bottomLeftFromPixel = new Coordinate(x - 1, y + 1);
        Coordinate bottomMiddleFromPixel = new Coordinate(x, y + 1);
        Coordinate bottomRightFromPixel = new Coordinate(x + 1, y + 1);

        if (pixelGroups.isEmpty()) {
            Group group = new Group();
            group.addCoordinate(c);
            pixelGroups.add(group);
        } else {
            /*
             * Überprüfe ob die Pixel sich berühren und ermittle Pixelgruppen und erstelle
             * falls nötig eine neue Gruppe
             */

            for (int j = 0; j < pixelGroups.size(); j++) {
                List<Coordinate> coordinates = pixelGroups.get(j).getCoordinates();

                for (int l = 0; l < coordinates.size(); l++) {
                    Coordinate k = coordinates.get(l);
                    if (k.equals(topLeftFromPixel) || k.equals(topMiddleFromPixel) || k.equals(topRightFromPixel)
                            || k.equals(middleLeftFromPixel) || k.equals(middleRightFromPixel)
                            || k.equals(bottomLeftFromPixel) || k.equals(bottomMiddleFromPixel)
                            || k.equals(bottomRightFromPixel)) {
                        pixelGroups.get(j).addCoordinate(c);
                    } else {
                        Group newGroup = new Group();
                        newGroup.addCoordinate(c);
                         pixelGroups.add(0, newGroup);
                    }
                }
            }
        }
    }

    return pixelGroups;
}

1 个答案:

答案 0 :(得分:3)

我假设您的图像应该以单个像素查看放大的图像。

查找连接的像素组是图像处理中非常常见的问题。有许多不同的方法。

这些连接区域通常称为BLOBS(二进制大对象)或连接组件,区域......

你会发现在blob探测器,连通分量分析等名称下进行算法的算法......

我建议您阅读this Wikipedia article作为起点。

相关问题