栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

多边形逼近C++与opencv实现

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

多边形逼近C++与opencv实现

#include
#include
#include   // CV_BGR2GRAY
#include
using namespace std;
using namespace cv;

Mat src01, gray_src01, dst01;
int threshold_v = 170;
int threshold_max = 255;
void contours_callback(int, void*)
{
	//二值化
	Mat binary_out;
	threshold(gray_src01, binary_out, threshold_v, threshold_max, THRESH_BINARY);
	vector> contours;
	vector hierachy;
	findContours(binary_out, contours, hierachy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0));
	imshow("输出图", binary_out);

	vector> contours_poly(contours.size());
	vector ploy_rects(contours.size());
	for (size_t i = 0;i < contours.size();i++)
	{
		//多边形轮廓
		approxPolyDP(contours[i], contours_poly[i], 3, true);
		//矩形包围框
		ploy_rects[i] = boundingRect(contours_poly[i]);
	}
	//绘图
	src01.copyTo(dst01);
	RNG rng(123);
	for (size_t t = 0;t < contours.size();t++)
	{
		Scalar color = Scalar(rng.uniform(0,255), rng.uniform(0, 255), rng.uniform(0, 255));
		rectangle(dst01,ploy_rects[t],color,2,8);
	}
	imshow("矩形框轮廓", dst01);
}
int main01()
{
	src01 = imread("C:\Users\dell\Desktop\stuff.jpg");
	namedWindow("原图", WINDOW_AUTOSIZE);
	namedWindow("输出图", WINDOW_AUTOSIZE);
	namedWindow("矩形框轮廓", WINDOW_AUTOSIZE);
	cvtColor(src01, gray_src01, CV_BGR2GRAY);
	blur(gray_src01, gray_src01, Size(3, 3), Point(-1, -1));
	imshow("原图", gray_src01);
	createTrackbar("阈值", "输出图", &threshold_v, threshold_max, contours_callback);
	contours_callback(0, 0);

	waitKey(0);
	return 0;
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/883941.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号