opencv - 从轮廓 OpenCV 中检测卡片 MinArea Quadrilateral

标签 opencv image-processing

另一个关于检测图片中的卡片。 我几乎成功地隔离了图片中的卡片,我有一个靠近的凸包,从这里我被卡住了。

对于上下文/约束,目标:

  • 检测图片中的卡片
  • 纯色背景(见示例)
  • 前面固定的卡片类型(意思是:我们有宽/高比)
  • 每张照片一个对象(至少现在)

我使用的方法:

  1. 缩小规模
  2. 灰度
  3. 光模糊
  4. 精明
  5. 寻找轮廓
  6. 删除列表中所有小于 120 点(尝试/错误值)的轮廓
  7. 案例 1:我有 1 个轮廓:我的卡片的完美轮廓:第 9 步
  8. 案例 2:我有多个轮廓
    • 凸包
    • 近似多边形 ?
  9. ???

第 1、3 和 6 步主要是去除噪声和小伪影。

所以我几乎停留在第 9 步。 我试过示例图片:

Sample

调试图片上:

  • 绿色:轮廓
  • 红色:凸包
  • 紫色/粉红色:使用 approxPolyDp
  • 黄色:minAreaRect

(结果图像是从minAreaRect中提取的)

所以轮廓是可以接受的,我可以通过调整 canny 或第一个模糊的参数来做得更好。 但现在这是可以接受的,现在的问题是,我怎样才能得到将形成“minarea quadrilateral”的 4 个点。 如您所见,minAreaRect 给出了一个不完美的矩形,并且 approxPolyDp 丢失了太多卡片。

有什么线索可以解决这个问题吗? 我尝试在使用 approxPolyDp 时使用 epsilon 值(我使用了 arcLength*0.1),但没有。

此方法的另一个问题是,在 canny 期间会丢失一个角(参见示例),它不会起作用(除非使用 minAreaRect)。但这可能可以在之前(通过更好的预处理)或之后(因为我们知道宽度/高度比)来解决。

enter image description here

这里不要求代码,只是想知道如何解决这个问题,

谢谢!

编辑:Yves Daoust 的解决方案:

  • 从凸包中获取与谓词匹配的 8 个点: (最大化 x, x+y, y, -x+y, -x, -x-y, -y, x-y)
  • 从这个八边形中,取4条最长的边,求交点

结果:

result

编辑 2: 使用 Hough 变换(而不是 8 个极值点)在找到 4 个边的所有情况下都能得到更好的结果。如果找到超过 4 行,可能我们有重复,所以使用一些数学来尝试过滤并保留 4 行。我使用行列式(如果平行则接近 0)和点线距离公式编写了草稿)

最佳答案

这是我在您的输入图像上尝试过的管道:

第 1 步:检测边缘

  • 模糊灰度输入并使用Canny 过滤器检测边缘

第 2 步:找到卡片的角

  • 计算轮廓
  • 长度对轮廓进行排序,只保留最大
  • 生成此轮廓的凸包
  • 用凸包创建一个掩膜
  • 使用HoughLinesP 找到你卡片的4 面
  • 计算 4 条边的交点

第 3 步:单应性

  • 使用 findHomography 找到卡片的仿射变换(使用在第 2 步 找到的 4 个交点)
  • Warp 使用计算出的单应矩阵输入图像

结果如下: card detection pipeline

请注意,您必须找到一种方法来对 4 个交点进行排序,以便始终保持相同的顺序(否则 findHomography 将不起作用)。

我知道你没有要求代码,但我必须测试我的管道,所以在这里......:)

Vec3f calcParams(Point2f p1, Point2f p2) // line's equation Params computation
{
    float a, b, c;
    if (p2.y - p1.y == 0)
    {
        a = 0.0f;
        b = -1.0f;
    }
    else if (p2.x - p1.x == 0)
    {
        a = -1.0f;
        b = 0.0f;
    }
    else
    {
        a = (p2.y - p1.y) / (p2.x - p1.x);
        b = -1.0f;
    }

    c = (-a * p1.x) - b * p1.y;
    return(Vec3f(a, b, c));
}

Point findIntersection(Vec3f params1, Vec3f params2)
{
    float x = -1, y = -1;
    float det = params1[0] * params2[1] - params2[0] * params1[1];
    if (det < 0.5f && det > -0.5f) // lines are approximately parallel
    {
        return(Point(-1, -1));
    }
    else
    {
        x = (params2[1] * -params1[2] - params1[1] * -params2[2]) / det;
        y = (params1[0] * -params2[2] - params2[0] * -params1[2]) / det;
    }
    return(Point(x, y));
}

vector<Point> getQuadrilateral(Mat & grayscale, Mat& output) // returns that 4 intersection points of the card
{
    Mat convexHull_mask(grayscale.rows, grayscale.cols, CV_8UC1);
    convexHull_mask = Scalar(0);

    vector<vector<Point>> contours;
    findContours(grayscale, contours, RETR_EXTERNAL, CHAIN_APPROX_NONE);

    vector<int> indices(contours.size());
    iota(indices.begin(), indices.end(), 0);

    sort(indices.begin(), indices.end(), [&contours](int lhs, int rhs) {
        return contours[lhs].size() > contours[rhs].size();
    });

    /// Find the convex hull object
    vector<vector<Point> >hull(1);
    convexHull(Mat(contours[indices[0]]), hull[0], false);

    vector<Vec4i> lines;
    drawContours(convexHull_mask, hull, 0, Scalar(255));
    imshow("convexHull_mask", convexHull_mask);
    HoughLinesP(convexHull_mask, lines, 1, CV_PI / 200, 50, 50, 10);
    cout << "lines size:" << lines.size() << endl;

    if (lines.size() == 4) // we found the 4 sides
    {
        vector<Vec3f> params(4);
        for (int l = 0; l < 4; l++)
        {
            params.push_back(calcParams(Point(lines[l][0], lines[l][1]), Point(lines[l][2], lines[l][3])));
        }

        vector<Point> corners;
        for (int i = 0; i < params.size(); i++)
        {
            for (int j = i; j < params.size(); j++) // j starts at i so we don't have duplicated points
            {
                Point intersec = findIntersection(params[i], params[j]);
                if ((intersec.x > 0) && (intersec.y > 0) && (intersec.x < grayscale.cols) && (intersec.y < grayscale.rows))
                {
                    cout << "corner: " << intersec << endl;
                    corners.push_back(intersec);
                }
            }
        }

        for (int i = 0; i < corners.size(); i++)
        {
            circle(output, corners[i], 3, Scalar(0, 0, 255));
        }

        if (corners.size() == 4) // we have the 4 final corners
        {
            return(corners);
        }
    }
    
    return(vector<Point>());
}

int main(int argc, char** argv)
{
    Mat input = imread("playingcard_input.png");
    Mat input_grey;
    cvtColor(input, input_grey, CV_BGR2GRAY);
    Mat threshold1;
    Mat edges;
    blur(input_grey, input_grey, Size(3, 3));
    Canny(input_grey, edges, 30, 100);

    vector<Point> card_corners = getQuadrilateral(edges, input);
    Mat warpedCard(400, 300, CV_8UC3);
    if (card_corners.size() == 4)
    {
        Mat homography = findHomography(card_corners, vector<Point>{Point(warpedCard.cols, 0), Point(warpedCard.cols, warpedCard.rows), Point(0,0) , Point(0, warpedCard.rows)});
        warpPerspective(input, warpedCard, homography, Size(warpedCard.cols, warpedCard.rows));
    }

    imshow("warped card", warpedCard);
    imshow("edges", edges);
    imshow("input", input);
    waitKey(0);

    return 0;
}

编辑:我对 CannyHoughLinesP 函数的参数做了一些调整,以便更好地检测卡片(程序现在适用于两个输入样本)。

关于opencv - 从轮廓 OpenCV 中检测卡片 MinArea Quadrilateral,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44127342/

相关文章:

python - 有没有办法用 OpenCV 更好地加载图像

c - 如何旋转 cvMat 中的值?

python - Heroku上带有OpenCV的Python应用程序

c# - 在 emgu CV 中使用 SURF 进行对象识别

c++ - OpenCV:如何设置像素的 alpha 透明度

ios - GIFS 的 DelayTime 或 UnclampedDelayTime

opencv - 如何修改haarcascade_frontalface_alt.xml?

node.js - 如何获取转换后的图像的缓冲区值

java - 如何将MATLAB图像处理程序转换为Java?

比较两个图像的算法