如何在OpenCV中的四个点内定义四边形内的像素?

时间:2016-02-08 05:27:12

标签: java opencv image-processing computer-vision chess

我是OpenCV的新手,但是有点运气和很多时间我能够将一些检测棋盘中单个细胞的代码组合在一起,如下所示:

chessboard

图像框架存储在Mat中,并且角落存储在MatOfPoint2f中。

代码显示我如何使用矩阵单独绘制单元格:

private void draw(final Mat frame) {
    for (int x = 0; x < BOARD_SIZE - 1; x++)
        for (int y = 0; y < BOARD_SIZE - 1; y++) {
            final int index = x + y * BOARD_SIZE;
            final Point topLeft = cornerPoints.get(index);
            final Point bottomLeft = cornerPoints.get(index + BOARD_SIZE);
            final Point topRight = cornerPoints.get(index + 1);
            final Point bottomRight = cornerPoints.get(index + 1 + BOARD_SIZE);

            // left line
            Imgproc.line(frame, topLeft, bottomLeft, DEBUG_COLOR);
            // right line
            Imgproc.line(frame, topRight, bottomRight, DEBUG_COLOR);
            // top line
            Imgproc.line(frame, topLeft, topRight, DEBUG_COLOR);
            // bottom line
            Imgproc.line(frame, bottomLeft, bottomRight, DEBUG_COLOR);
        }
}

如何使用四个点(单元格的角点)来获取每个四边形内部像素的RGB值?

2 个答案:

答案 0 :(得分:2)

从顶点创建遮罩。你可以使用fillPoly。 然后迭代像素。如果像素(x,y)在掩码中有效,则读取RGB else。使用极端顶点限制像素迭代范围。

答案 1 :(得分:1)

掩蔽工作。如果你有很多多边形,或者没有太多的RAM,多边形点测试可能会更有效,特别是如果你可以保证你的四边形是凸的。见this reference