Opencv Convex Hull (凸包)

Posted herd

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Opencv Convex Hull (凸包)相关的知识,希望对你有一定的参考价值。

#include <iostream>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

Mat img1, img2, img3, img4, img_result, img_gray1, img_gray2, img_gray3, img_canny1;

char win1[] = "window1";
char win2[] = "window2";
char win3[] = "window3";
char win4[] = "window4";
char win5[] = "window5";

int thread_value = 100;
int max_value = 255;
RNG rng1(12345);

int Demo_Convex_Hull();
void Demo_1(int, void*);

//发现凸包
int Demo_Convex_Hull()
{
  namedWindow(win1, CV_WINDOW_AUTOSIZE);
  namedWindow(win2, CV_WINDOW_AUTOSIZE);
  //namedWindow(win3, CV_WINDOW_AUTOSIZE);

  img1 = imread("D://images//1//temp2.jpg");
  //img2 = imread("D://images//1//p5_1.jpg");
  if (img1.empty())
  {
    cout << "could not load image..." << endl;
    return 0;
  }

  imshow(win1, img1);
  img4 = Mat::zeros(img1.size(),CV_8UC3);

  //转灰度图
  cvtColor(img1, img_gray1, CV_BGR2GRAY);
  //模糊处理
  blur(img_gray1, img2, Size(3, 3), Point(-1, -1),BORDER_DEFAULT);

  createTrackbar("track", win1, &thread_value, max_value, Demo_1);
  Demo_1(0,0);

  return 0;
}

void Demo_1(int,void*)
{
  vector<vector<Point>> vec_p;
  vector<Vec4i> vec_4i;

  threshold(img2, img3, thread_value, max_value, THRESH_BINARY);
  findContours(img3, vec_p, vec_4i, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0));

  vector<vector<Point>> convexs(vec_p.size());
  for (size_t i=0;i<vec_p.size();i++)
  {
    convexHull(vec_p[i], convexs[i], false, true);
  }

  for (size_t j=0;j<vec_p.size();j++)
  {
    Scalar color_1 = Scalar(rng1.uniform(0,255), rng1.uniform(0, 255), rng1.uniform(0, 255));
    drawContours(img4, vec_p, j, color_1, 2, LINE_8, vec_4i, 0, Point(0, 0));
    drawContours(img4, convexs, j, color_1, 2, LINE_8, vec_4i, 0, Point(0, 0));
  }
  imshow(win2,img4);
}

int main()
{
  Demo_Convex_Hull();

  waitKey(0);
  return 0;
}

 技术分享图片














































以上是关于Opencv Convex Hull (凸包)的主要内容,如果未能解决你的问题,请参考以下文章

Convex hull凸包

youcans 的 OpenCV 例程200篇126. 形态算法之凸壳(Convex hull)

R语言为散点图添加凸包(convex hull):数据预处理(创建一个包含每组数据凸包边界的数据集)ggplot2使用geom_polygon函数为可视化图像添加凸包(convex hull)

凸包(Convex Hull)构造算法——Graham扫描法

[算法课][分治]寻找凸包 (Convex Hull)

Gym 101986D Making Perimeter of the Convex Hull Shortest(凸包+极角排序)