目录
1--检测绘制轮廓
利用OpenCV检测轮廓的一般步骤:
①对原图像进行灰度化处理;
②对原图像进行二值化处理;
③检测并提取二值化图像的轮廓;
1-1--检测轮廓API
void cv::findContours(cv::InputArray image, cv::OutputArrayOfArrays contours, cv::OutputArray hierarchy, int mode, int method, cv::Point offset = cv::Point())
1-2--绘制轮廓API
void cv::drawContours(cv::InputOutputArray image, cv::InputArrayOfArrays contours, int contourIdx, const cv::Scalar &color, int thickness = 1, int lineType = 8, cv::InputArray hierarchy = noArray(), int maxLevel = 2147483647, cv::Point offset = cv::Point())
1-3--代码实例
# include <opencv2/opencv.hpp>
# include <cstdio>
# include <iostream>
cv::Mat src, dst;
int Threshold = 100;
int Thereshold_max = 255;
void Demo_Contours(int, void*){
cv::Mat canny;
std::vector<std::vector<cv::Point>> contours;
std::vector<cv::Vec4i> hierachy;
cv::Canny(src, canny, Threshold, Threshold *2, 3, false); // Canny二值化
cv::findContours(canny, contours, hierachy, cv::RETR_TREE, cv::CHAIN_APPROX_SIMPLE, cv::Point(0,0));
dst.create(src.size(), CV_8UC3);
cv::RNG rng(12345);
for(size_t i = 0; i < contours.size(); i++){
cv::Scalar color = cv::Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255)); // 利用随机数生成颜色
cv::drawContours(dst, contours, i, color, 2, 8, hierachy, 0, cv::Point(0, 0));
}
cv::imshow("output", dst);
}
int main(int argc, char** argv){
src = cv::imread("C:/Users/Liujinfu/Desktop/opencv_bilibili/test1.jpg");
if (src.empty()){
printf("could not load image..\n");
return -1;
}
cv::imshow("input", src);
cv::cvtColor(src, src, cv::COLOR_BGRA2GRAY); // 灰度化
// 创建Trackbar
cv::createTrackbar("Threshold", "input", &Threshold, Thereshold_max, Demo_Contours);
Demo_Contours(0, 0);
cv::waitKey(0);
return 0;
}
2--检测并绘制凸包
2-1--凸包的概念
常规定义:在一个多边形中,其边缘或内部任意两个点的连线都包含在多边形的边界或者内部,这个多边形称为凸包。
严谨定义:包含点集合S中所有点的最小凸多边形称为凸包。
2-2--OpenCV API
void cv::convexHull(cv::InputArray points, cv::OutputArray hull, bool clockwise = false, bool returnPoints = true)
检测凸包的原理可参考博客1,OpenCV检测并绘制凸包的常用流程如下:
① 图像灰度化;
② 图像二值化;
③ 检测轮廓,获取候选点集;
④ 调用API检测凸包;
⑤ 绘制凸包;
2-3--代码实例
# include <opencv2/opencv.hpp>
# include <cstdio>
# include <iostream>
cv::Mat src, src_gray, dst;
int threshold_value = 100;
int threshold_max = 255;
cv::RNG rng(12345);
void Threshold_Callback(int, void*){
cv::Mat bin_output;
std::vector<std::vector<cv::Point>> contours;
std::vector<cv::Vec4i> hierachy;
cv::threshold(src_gray, bin_output, threshold_value, threshold_max, cv::THRESH_BINARY);
cv::findContours(bin_output, contours, hierachy, cv::RETR_TREE, cv::CHAIN_APPROX_SIMPLE, cv::Point(0, 0));
std::vector<std::vector<cv::Point>> convexs(contours.size());
for (size_t i = 0; i < contours.size(); i++){
cv::convexHull(contours[i], convexs[i], false, true);
}
//绘制
dst = cv::Mat::zeros(src.size(), CV_8UC3);
std::vector<cv::Vec4i> empty(0);
for (size_t k = 0; k < contours.size(); k++){
cv::Scalar color = cv::Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
//cv::drawContours(dst, contours, k, color, 2, cv::LINE_8, hierachy, 0, cv::Point(0, 0));
cv::drawContours(dst, convexs, k, color, 2, cv::LINE_8, empty, 0, cv::Point(0, 0));
}
cv::imshow("output", dst);
}
int main(int argc, char** argv){
src = cv::imread("C:/Users/Liujinfu/Desktop/opencv_bilibili/test0101.jpg");
if (src.empty()){
printf("could not load image..\n");
return -1;
}
cv::imshow("input", src);
cv::cvtColor(src, src_gray, cv::COLOR_BGR2GRAY);
cv::blur(src_gray, src_gray, cv::Size(3, 3), cv::Point(-1, -1), cv::BORDER_DEFAULT);
cv::imshow("gray", src_gray);
cv::namedWindow("output");
cv::createTrackbar("Threshold:", "output", &threshold_value, threshold_max, Threshold_Callback);
Threshold_Callback(0, 0);
cv::waitKey(0);
return 0;
}
上图的绿色框表示其中一个检测并绘制的凸包。