#include<iostream>
#include<opencv2\core.hpp>
#include<opencv2\highgui\highgui.hpp>
#include<opencv2\imgproc.hpp>
using namespace cv;
using namespace std;
int main(int argc,char* argv[])
{
vector<Point> contour;
contour.push_back(Point(20, 20));
contour.push_back(Point(50, 70));
contour.push_back(Point(20, 120));
contour.push_back(Point(120, 120));
contour.push_back(Point(100, 70));
contour.push_back(Point(120, 20));
//计算轮廓的凸包
vector<int>hull;
convexHull(contour, hull, false, false);
//计算凸包的缺陷
vector<Vec4i> defects;
for (auto it = hull.begin(); it != hull.end(); it++)
cout << *it << endl;
convexityDefects(contour, hull, defects);
//打印凸包的缺陷
for (int i = 0; i < defects.size(); i++)
cout << defects[i] << endl;
system("pause");
return 0;
}
import cv2
import numpy as np
if __name__=="__main__":
contour=np.array([[20,20],[50,70],[20,120],[120,120],[100,70],[120,20]],np.int32)
#轮廓的凸包
hull=cv2.convexHull(contour,returnPoints=False)
defects=cv2.convexityDefects(contour,hull)
print(hull)
print(defects)