如何在OpenCV中计算图像中的角点数量?

4
我有一组平假名字符,并希望计算字符具有的端点/尖端数量。
例如: 输入图像:

enter image description here

期望输出图像:

enter image description here

我尝试使用凸包

enter image description here

代码:(基于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()函数,但它返回了太多不需要的角点。

enter image description here

代码:(基于opencv教程这里

    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。]

1个回答

5

我不太使用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

enter image description here

# 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

enter image description here

# 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

enter image description here

# 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

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接