首页 文章

检测图像的像素块

提问于
浏览
1

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

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

My question: 是否有其他解决方案来分离黑/白图像的黑色区域以获得列出的坐标?

Example:

我将此图像保存到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

My Code:

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 回答

  • 3

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

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

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

    您将找到以blob检测器,连通分量分析等名称进行算法的算法......

    我建议你阅读this Wikipedia article作为起点 .

相关问题