【OpenCV】轮廓与凸包

来源:互联网 发布:深圳盘古数据有限公司 编辑:程序博客网 时间:2024/05/17 06:28

轮廓

void cv::findContours   (   InputOutputArray image, OutputArrayOfArrays contours,                            OutputArray hierarchy , int mode,                            int method            , Point   offset = Point() 说明:    hierarchy:层级关系(详见:https://docs.opencv.org/3.1.0/d9/d8b/tutorial_py_contours_hierarchy.html)    mode:RETR_EXTERNAL、RETR_LIST、RETR_CCOMP、RETR_TREE、RETR_FLOODFILL )   

  本节只绘制轮廓形状。

  绘制轮廓的包围框和圆参考:https://docs.opencv.org/master/da/d0c/tutorial_bounding_rects_circles.html

  绘制轮廓的旋转框和椭圆参考:https://docs.opencv.org/master/de/d62/tutorial_bounding_rotated_ellipses.html


代码示例

#include "opencv2/imgcodecs.hpp"#include "opencv2/highgui.hpp"#include "opencv2/imgproc.hpp"#include <iostream>using namespace cv;using namespace std;Mat src, src_gray;int thresh = 100, max_thresh = 255;RNG rng(12345);     // 伪随机函数,初始化后随机种子是固定的void thresh_callback(int, void*);int main(int argc, char** argv){    String filename = "../data/balloon.jpg";    src = imread(filename);    if (src.empty()) { return -1; }    cvtColor(src, src_gray, COLOR_BGR2GRAY);                // 转换灰度图像    blur(src_gray, src_gray, Size(3, 3));                   // 平滑    const char* source_window = "Source";    namedWindow(source_window, WINDOW_AUTOSIZE);    imshow(source_window, src);    createTrackbar("Canny thresh", "Source", &thresh, max_thresh, thresh_callback);    thresh_callback(0, 0);    waitKey(0);    return 0;}void thresh_callback(int, void*){    Mat canny_output;    vector<vector<Point>>   contours;    vector<Vec4i> hierarchy;    Canny(src_gray, canny_output, thresh, thresh * 2, 3);   // Canny算子边缘检测    findContours(canny_output, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0));   // 轮廓    Mat drawing = Mat::zeros(canny_output.size(), CV_8UC3);    for (size_t i = 0; i < contours.size(); i++)    {        Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255));        drawContours(drawing, contours, (int)i, color, 2, 8, hierarchy, 0, Point());                // 绘制轮廓    }    imshow("Contours", drawing);}

运行结果


凸包

代码示例

#include "opencv2/imgcodecs.hpp"#include "opencv2/highgui.hpp"#include "opencv2/imgproc.hpp"#include <iostream>using namespace cv;using namespace std;Mat src, src_gray;int thresh = 100, max_thresh = 255;RNG rng(12345);void thresh_callback(int, void*);int main(int,char** argv){    src = imread("../data/hand.jpg");    cvtColor(src, src_gray, COLOR_BGR2GRAY);    blur(src_gray, src_gray, Size(3, 3));    char * source_window = "Source image";    namedWindow(source_window, WINDOW_AUTOSIZE);    imshow(source_window, src);    createTrackbar("Threshold", "Source image", &thresh, max_thresh, thresh_callback);    thresh_callback(0, 0);    waitKey(0);    return 0;}void thresh_callback(int, void*){    Mat output;    vector<vector<Point>> contours;    vector<Vec4i> hierarchy;    threshold(src_gray, output, thresh, 255, THRESH_BINARY);    findContours(output, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0));    vector<vector<Point>> hull(contours.size());    for (size_t i = 0; i < contours.size(); i++)    {        convexHull(Mat(contours[i]), hull[i], false);    }    Mat drawing = Mat::zeros(output.size(), CV_8UC3);    for (size_t i = 0; i < contours.size(); i++)    {        Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));        drawContours(drawing, contours, (int)i, color, 1, 8, vector<Vec4i>(), 0, Point());        drawContours(drawing, hull, (int)i, color, 1, 8, vector<Vec4i>(), 0, Point());    }    imshow("Hull", drawing);}


运行结果


原创粉丝点击