2021-08-19opencv-轮廓发现

该博客主要介绍了如何使用OpenCV库在C++中进行图像处理,特别是通过Canny边缘检测和findContours函数来发现图像中的轮廓。作者展示了一个简单的程序,该程序加载图像,转换为灰度,设置阈值,并通过跟踪条动态调整阈值。然后,程序找到并绘制轮廓,最终显示在输出窗口中。这为图像分析和对象识别提供了基础。
摘要由CSDN通过智能技术生成

opencv-轮廓发现

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

using namespace  cv;
using namespace  std;

int threshold_value = 100;
int threshold_max = 255;
Mat src, dst;
const char* output = "findcontours-demo";
void Demo_Contours(int, void*);
RNG rng;

int main(int,void*) {
	src = imread("D:/b.jpeg", 1);
	if (src.empty()) {
		printf("could not load image");
		return -1;
	}
	imshow("input", src);
	cvtColor(src, src, COLOR_BGR2GRAY);
	namedWindow(output, WINDOW_AUTOSIZE);
	

	const char* tarckbar_title = "threshold Value:";//滑动条
	createTrackbar(tarckbar_title, output, &threshold_value, threshold_max, Demo_Contours);
	Demo_Contours(0,0);
	waitKey(0);
	return 0;
}

void Demo_Contours(int, void*) {
	Mat canny_output;
	vector<vector<Point>>contours;//生成二维的点
	vector<Vec4i>hierachy;
	Canny(src, canny_output, threshold_value, threshold_value * 2, 3, false);
	findContours(canny_output, contours, hierachy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0));//发现轮廓

	dst = Mat::zeros(src.size(), CV_8UC3);//重新生成一个图片
	RNG rng(12345);
	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(dst, contours, i, color, 2, 8, hierachy, 0, Point(0, 0));//描绘轮廓
	}
	imshow(output, dst);
	}
	

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值