OpenCV凸性缺陷绘图

问题描述 投票:0回答:1

enter image description here

你好。我有上面的图像并使用“findContours”功能。然后我使用“凸性缺陷”函数来找到角点。

结果如下。

enter image description here

这段代码的问题在于它找不到圆角。你找不到像下面这样的点。

enter image description here

这是我的代码

#include "opencv2/imgcodecs.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/videoio.hpp"
#include <opencv2/highgui.hpp>
#include <opencv2/video.hpp>
#include <iostream>
#include <sstream>
#include <fstream>

using namespace cv;
using namespace std;


int main(int argc, char** argv)
{
    cv::Mat image = cv::imread("find_Contours.png");
    //Prepare the image for findContours
    cv::cvtColor(image, image, CV_BGR2GRAY);
    cv::threshold(image, image, 128, 255, CV_THRESH_BINARY);
    //Find the contours. Use the contourOutput Mat so the original image doesn't get overwritten
    std::vector<std::vector<cv::Point> > contours;
    cv::Mat contourOutput = image.clone();
    cv::findContours(contourOutput, contours, CV_RETR_LIST, CV_CHAIN_APPROX_NONE);
    ////convexityDefects 
    vector<vector<Point> >hull(contours.size());
    vector<vector<int> > hullsI(contours.size()); // Indices to contour points
    vector<vector<Vec4i>> defects(contours.size());
    for (int i = 0; i < contours.size(); i++)
    {
        convexHull(contours[i], hull[i], false);
        convexHull(contours[i], hullsI[i], false);
        if (hullsI[i].size() > 3) // You need more than 3 indices          
        {
            convexityDefects(contours[i], hullsI[i], defects[i]);
        }
    }
    ///// Draw convexityDefects
    for (int i = 0; i < contours.size(); ++i)
    {
        for (const Vec4i& v : defects[i])
        {
            float depth = v[3]/256;
            if (depth >= 0) //  filter defects by depth, e.g more than 10
            {
                int startidx = v[0]; Point ptStart(contours[i][startidx]);
                int endidx = v[1]; Point ptEnd(contours[i][endidx]);
                int faridx = v[2]; Point ptFar(contours[i][faridx]);
                circle(image, ptFar, 4, Scalar(255, 255, 255), 2);
                cout << ptFar << endl;

            }
        }
    }
    //
    cv::imshow("Input Image", image);
    cvMoveWindow("Input Image", 0, 0);
    //
    waitKey(0);
}

有人可以制作代码并找到红点吗?请帮忙。

现在我想从内部找到“凸性缺陷”,而不是像这样的图像外面:enter image description here

有人可以帮帮我吗?

opencv image-processing contour convexity-defects
1个回答
1
投票

使用非常重要

convexHull(contours[i], hullsI[i], true);

也就是说,索引的最后一个参数为“true”。我几乎可以肯定这是它找不到所有缺陷的原因。在解决这个问题之前,尝试找到其他错误(如果有的话)是没有多大意义的。

© www.soinside.com 2019 - 2024. All rights reserved.