【问题标题】:How can I count the number of tips in an image in OpenCV?如何计算 OpenCV 中图像中的提示数?
【发布时间】:2015-10-12 13:24:11
【问题描述】:

我有一组平假名字符,我想计算字符的端点/提示数。

示例: 输入图片:

想要的输出图像:

我尝试过使用凸包

代码:(基于opencv教程here

    findContours(threshold_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));

    vector<vector<Point> >hull(contours.size());

    for (int i = 0; i < contours.size(); i++)
    {
        convexHull(Mat(contours[i]), hull[i], false);
    }

    Mat drawing = Mat::zeros(threshold_output.size(), CV_8UC3);
    for (int i = 0; i< contours.size(); i++)
    {
        if (hierarchy[i][3] == 0) {
            Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
            drawContours(drawing, hull, i, color, 1, 8, vector<Vec4i>(), 0, Point());
        }
    }

然后是 conrnerHarris() 但它返回了太多不需要的角

代码:(基于opencv教程here

    int blockSize = 2;
    int apertureSize = 3;

    /// Detecting corners
    drawing = binarizeImage(drawing); // otsu's
    cornerHarris(drawing, dst, blockSize, apertureSize, 0.04, BORDER_DEFAULT);

    /// Normalizing
    normalize(dst, dst_norm, 0, 255, NORM_MINMAX, CV_32FC1, Mat());
    convertScaleAbs(dst_norm, dst_norm_scaled);

    int countCorner = 0;

    /// Drawing a circle around corners
    for (int j = 0; j < dst_norm.rows; j++)
    {
        for (int i = 0; i < dst_norm.cols; i++)
        {
            if ((int)dst_norm.at<float>(j, i) > 50)
            {
                circle(output, Point(i, j), 2, Scalar::all(255), -1, 8, 0);
                countCorner++;
            }
        }
    }

它检测到 11 个角。

我在想这可能和指尖检测一样,但我不知道该怎么做。

[我使用的是 OpenCV 2.4.9。]

【问题讨论】:

    标签: c++ image opencv convex-hull corner-detection


    【解决方案1】:

    我不倾向于使用 OpenCV,因为我可以通过 ImageMagick 获得所需的东西,它是免费的,安装在大多数 Linux 发行版上,也可用于 OSX 和 Windows。所以,我尝试了 ImageMagick,也许你可以调整我的方法——这只是在命令行中完成的。

    # Thin input image down to a skeleton
    convert char.jpg -threshold 50% -negate -morphology Thinning:-1 Skeleton skeleton.jpg
    

    # Find line-ends, using Hit-or-Miss morphology, and make them green (lime). Save as "lineends.jpg"
    convert skeleton.jpg -morphology HMT LineEnds -threshold 50% -fill lime -opaque white lineends.jpg
    

    # Find line-junctions, using Hit-or-Miss morphology, and make them red. Save as "line junctions.jpg"
    convert skeleton.jpg -morphology HMT LineJunctions -threshold 50% -fill red -opaque white linejunctions.jpg
    

    # Superpose the line-ends and line junctions into a result
    convert lineends.jpg linejunctions.jpg -compose lighten -composite result.jpg
    

    现在您将在线路末端附近有一个红色和两个绿色点,并且在交叉点附近只有红色点,但没有匹配的绿色点。因此,您将计算附近有绿点的红点。

    我将只展示叠加了点的骨架,以便您了解它们之间的关系:

    composite -blend 30% skeleton.jpg result.jpg z.jpg
    

    【讨论】:

      猜你喜欢
      • 2020-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多