c++ - 骨架的分支点

标签 c++ opencv image-processing mat

我的代码应该找到骨架的分支点,但它找到的点不是它们。这很可能是因为 IMREAD_COLOR = 1,而不是 0。但如果我将值设置为 0,那么它将完全停止查找和标记点。是不是因为点的类型(p1、p2等)不匹配Mattype或者是其他东西?我该如何解决?

int main() {
    cv::Mat thresh = cv::imread("binary_skeleton.jpg", 1);
    if (thresh.empty())
        return -1;

    for (int i = 1; i < thresh.rows - 1; i++)
    {
        for (int j = 1; j < thresh.cols - 1; j++)
        {
            uchar p1 = thresh.at<uchar>(i, j);
            if (0 == p1)
            {
                uchar p1 = thresh.at<uchar>(i, j);
                uchar p2 = thresh.at<uchar>(i - 1, j);
                uchar p3 = thresh.at<uchar>(i - 1, j + 1);
                uchar p4 = thresh.at<uchar>(i, j + 1);
                uchar p5 = thresh.at<uchar>(i + 1, j + 1);
                uchar p6 = thresh.at<uchar>(i + 1, j);
                uchar p7 = thresh.at<uchar>(i + 1, j - 1);
                uchar p8 = thresh.at<uchar>(i, j - 1);
                uchar p9 = thresh.at<uchar>(i - 1, j - 1);

                int same = 0;
                if (p9 == p1) { ++same; }
                if (p8 == p1) { ++same; }
                if (p7 == p1) { ++same; }
                if (p6 == p1) { ++same; }
                if (p5 == p1) { ++same; }
                if (p4 == p1) { ++same; }
                if (p3 == p1) { ++same; }
                if (p2 == p1) { ++same; }

                if (same == 2)
                {
                    circle(thresh, Point(j, i), 3, Scalar(255, 120, 0), 1);
                }
            }
        }
    }
    cv::imshow("", thresh);
    cv::waitKey(0);
}

Incorrect image

最佳答案

您应该对代码执行以下操作:

  • 使用 cv::imread("binary_skeleton.jpg", cv::IMREAD_GRAYSCALE) 读取图像.您使用的选项强制图像为 RGB,但您使用的图像好像只有一个 channel 。
  • 而不是比较 if (0 == p1)要查找设置像素,请使用 if (128 > p1) . JPEG 压缩可以改变像素的值,因此图像不会只有 0 和 255 的值,即使这些值是写入文件的值。这些值会存在一些变化。我们只是简单地设置一半阈值,以确保任何低值都被视为骨架像素。同样,替换 if (p9 == p1)if (128 > p9) .
  • 分支点总是有两个以上的邻居集。当设置两个邻居时,它是一条线上的一个点。而不是 if (same == 2)使用 if (same > 2) .
  • 删除对 p1 的第二个分配, 没有效果。
  • 关于c++ - 骨架的分支点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63938495/

    相关文章:

    python - OpenCV 未在图像上添加线和点

    c++ - boost::const_string 是/曾经是 boost 的一部分吗?

    c++ - 链表数组 C++

    Python 和 OpenCV : Realtime Get RGB Values When Mouse Is Clicked

    python - 如何获得世界空间中二维点的射线方程

    visual-studio-2010 - VS10上的OpenCV打开然后关闭

    python - opencv python使用roxPolyDP在植物托盘中找到正方形

    c++ - 当应用程序应该退出时,生产者和消费者无限期地等待

    模板内部类中的 C++ decltype

    c++ - Open-CV - 加载不正确