概述
1.轮廓查找与绘制代码
#include<opencv2/opencv.hpp>
#include<opencv2/core.hpp>
#include<opencv2/highgui.hpp>
#include<iostream>
using namespace std;
using namespace cv;
Mat src, temp;
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
int main()
{
src = imread("src1.png"); //待搜索图像
temp = src.clone(); //原图克隆图像
cvtColor(src, src, COLOR_BGR2GRAY);
threshold(src, src, 100, 255, THRESH_BINARY);
imshow("src", src);
//查找轮廓并绘制轮廓【查找轮廓之前要将输入凸显转化为二值图】
findContours(src, contours, hierarchy,RETR_LIST,CHAIN_APPROX_SIMPLE);
drawContours(temp, contours, -1, Scalar(0, 255, 0), 2, 8);
imshow("temp", temp);
waitKey(0);
}
2.绘制轮廓的凸包
#include<opencv2/opencv.hpp>
#include<opencv2/core.hpp>
#include<opencv2/highgui.hpp>
#include<iostream>
using namespace std;
using namespace cv;
int main()
{
Mat src = imread("1.png"); //读取图像
Mat dst = src.clone(); //原图的克隆图像
cvtColor(src, src, COLOR_BGR2GRAY); //灰度化
threshold(src, src, 100, 255, THRESH_BINARY_INV);
//imshow("src", src);
vector<vector<Point>> contours;
vector<Vec4i> hierachy;
findContours(src, contours, hierachy, RETR_EXTERNAL, CHAIN_APPROX_NONE);
//drawContours(dst, contours, -1, Scalar(0, 255, 0), 2, 8);
//imshow("dst", dst);
//应用凸包检测
vector<vector<Point>> hull(contours.size());
for (int i = 0; i < contours.size(); i++)
{
convexHull(contours[i], hull[i], true); //为每一个轮廓都查找凸包
}
for (int i = 0; i < contours.size(); i++)
{
drawContours(dst, contours, i, Scalar(0, 255, 255), 2, 8); //绘制轮廓
drawContours(dst, hull, i, Scalar(0, 255, 0), 2, 8); //绘制轮廓
}
imshow("hull", dst);
waitKey(0);
}
![](https://file2.kaopuke.com:8081/files_image/2023060410/99bb3fb224574bb89acbf6f6b01054c5.png)
3.绘制轮廓的外接矩形 Rectangle1
#include<opencv2/opencv.hpp>
#include<opencv2/core.hpp>
#include<opencv2/highgui.hpp>
#include<iostream>
using namespace std;
using namespace cv;
int main()
{
Mat src = imread("1.png"); //读取图像
Mat dst = src.clone(); //原图的克隆图像
cvtColor(src, src, COLOR_BGR2GRAY); //灰度化
threshold(src, src, 100, 255, THRESH_BINARY_INV); //二值化
//imshow("src", src);
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
findContours(src, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_NONE); //查找轮廓
//画出轮廓并绘制矩形
vector<Rect> boundRect(contours.size()); //定义外接矩形的集合
drawContours(dst, contours, -1, Scalar(0, 255, 255), 2, 8); //绘制轮廓
//遍历每一个轮廓,对每一个轮廓画 Rectangle1
for (int i = 0; i < contours.size(); i++)
{
boundRect[i] = boundingRect((Mat)contours[i]); //查找每一个外界矩形
rectangle(dst, boundRect[i], Scalar(0,255,0),2,8);
}
imshow("dst", dst);
waitKey(0);
}
4.绘制轮廓的最小外接矩形 Rectangle2
#include<opencv2/opencv.hpp>
#include<opencv2/core.hpp>
#include<opencv2/highgui.hpp>
#include<iostream>
using namespace std;
using namespace cv;
int main()
{
Mat src = imread("1.png"); //读取图像
Mat dst = src.clone(); //原图的克隆图像
cvtColor(src, src, COLOR_BGR2GRAY); //灰度化
threshold(src, src, 100, 255, THRESH_BINARY_INV); //二值化
//查找轮廓
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
findContours(src, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_NONE);
//定义最小外接矩形的集合 以及 不带方向的外接矩形
vector<RotatedRect> Rect2(contours.size());
vector<Rect> Rect1(contours.size());
//定义外接矩形四个端点的集合
Point2f rect[4];
for (int i = 0; i < contours.size(); i++)
{
drawContours(dst, contours, i, Scalar(0, 255, 0), 2, 8); //描绘轮廓
//生成最小外接矩形 Rectangle2
Rect2[i] = minAreaRect((Mat)contours[i]);
//生成不带方向的外接矩形 Rectangle1
Rect1[i] = boundingRect((Mat)contours[i]);
Rect2[i].points(rect); //将最小外接矩形的四个端点复制给rect数组
rectangle(dst, Rect1[i], Scalar(255, 0, 0), 2, 8);
for (int j = 0; j < 4; j++)
{
line(dst, Point(rect[j]), Point(rect[(j + 1) % 4]), Scalar(0, 255, 0), 2, 8);
}
}
imshow("dst", dst);
waitKey(0);
}
5.绘制轮廓的最小外接圆 Circle 【MinEnclosingCircle】
#include<opencv2/opencv.hpp>
#include<opencv2/core.hpp>
#include<opencv2/highgui.hpp>
#include<iostream>
using namespace std;
using namespace cv;
Mat edges; //Canny边缘检测的边缘轮廓
int main()
{
Mat src = imread("1.png"); //读取图像
Mat dst = src.clone(); //原图的克隆图像
cvtColor(src, src, COLOR_BGR2GRAY);
Canny(src, edges, 100, 255);
//生成轮廓
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
Point2f centor;
float radius;
findContours(edges, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_NONE);
for (int i = 0; i < contours.size(); i++)
{
drawContours(dst, contours, i, Scalar(0, 255, 0), 2, 8);
minEnclosingCircle((Mat)contours[i], centor, radius);
circle(dst, centor, radius, Scalar(255, 0, 0), 2, 8);
}
imshow("dst", dst);
waitKey(0);
}
最后
以上就是开放故事为你收集整理的opencv [c++] 轮廓查找与绘制及轮廓特征属性的应用的全部内容,希望文章能够帮你解决opencv [c++] 轮廓查找与绘制及轮廓特征属性的应用所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复