OpenCV反凸缺陷图

时间:2019-03-13 23:41:54

标签: c++ opencv image-processing opencv3.1 convexity-defects

我使用此代码查找“凸性缺陷”,但是此代码从外侧查找“凸性缺陷”,我想从内侧进行查找,就像这张照片一样

enter image description here

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], true);;
        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);
}

0 个答案:

没有答案