OpenCV图像处理—— 凸包检测
生活随笔
收集整理的這篇文章主要介紹了
OpenCV图像处理—— 凸包检测
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
前言
凸包(Convex Hull)是一個計算幾何(圖形學)中的概念,在一個實數向量空間V中,對于給定集合X,所有包含X的凸集的交集S被稱為X的凸包。通俗的講就是把檢測到一個平面的點以最大的外包多邊形包進去。
凸包檢測
1.C++ API 原型
void convexHull(InputArray points, OutputArray hull, bool clockwise=false, bool returnPoints=true )InputArray points: 輸入二維點集,利用Mat或者vector存儲
OutputArray hull: 輸出的convex hull點集。有兩種情況:直接點集合或者points的索引
bool false: 負責順時針或者逆時針
bool returnPoints: 控制傳回的是點集,還是點集的索引
2.代碼演示
#include <opencv2/opencv.hpp> #include <iostream> #include <math.h> #include <opencv2/highgui/highgui.hpp>using namespace std; using namespace cv;void convexHullDetection(cv::Mat &src, cv::Mat &dst);int main(int argc, char** argv) {Mat src, dst;src = imread("bin.jpg");if (!src.data) {std::cout << "could not load image..." << std::endl;return -1;}dst = src.clone();convexHullDetection(src, dst);cv::namedWindow("dst", 0);cv::imshow("dst", dst);waitKey(0);return 0; }void convexHullDetection(cv::Mat &src, cv::Mat &dst) {vector<vector<Point>> contours;vector<Vec4i> hierachy;cv::Mat src_gray, bin_output;if (src.channels() > 1){cvtColor(src, src_gray, COLOR_BGR2GRAY);}threshold(src_gray, bin_output, 127, 255, THRESH_BINARY);findContours(bin_output, contours, hierachy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0));//發現輪廓得到的候選點vector<vector<Point>> convexs(contours.size());for (size_t i = 0; i < contours.size(); i++) {convexHull(contours[i], convexs[i], false, true);}// 繪制vector<Vec4i> empty(0);for (size_t k = 0; k < contours.size(); k++) {drawContours(dst, convexs, k, Scalar(0, 0, 255), 5, LINE_8, empty, 0, Point(0, 0));} }3.運行結果
總結
以上是生活随笔為你收集整理的OpenCV图像处理—— 凸包检测的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 图像处理——基于深度学习HED实现目标边
- 下一篇: OpenCV图像处理——copyTo与m