OpenCV——轮廓

前言

当我们通过阈值分割提取到图像中的目标物体后,我们就需要通过边缘检测来提取目标物体的轮廓,使用这两种方法基本能够确定物体的边缘或者前景。接下来,我们通常需要做的是拟合这些边缘的前景,如拟合出包含前景或者边缘像素点的最小外包矩形、圆、凸包等几何形状,为计算它们的面积或者模板匹配等操作打下坚实的基础。

       注意:边缘和轮廓并不完全相同.

轮廓发现 ( find contour)

       首先了解一下轮廓的定义。一个轮廓代表一系列的点(像素),这一系列的点构成一个有序的点集,所以可以把一个轮廓理解为一个有序的点集。

API 介绍

void findContours(InputOutputArray image, 
	OutputArrayOfArrays contours,
	OutputArray hierarchy, 
	int mode,int method, 
	Point offset = Point());

轮廓绘制 ( draw contour)

API 介绍

void drawContours(InputOutputArray image, 
	              InputArrayOfArrays contours, 
	              int contourIdx,
	              const Scalar& color,
	              int thickness = 1,
	              int lineType = 8, 
	              InputArray hierarchy = noArray(),
	              int maxLevel = INT_MAX, 
	              Point offset = Point())

代码实现

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

using namespace std;
using namespace cv;

Mat src_img, dst_img;
const string output_win = "找到的轮廓";
int threshold_value = 100;
int threshold_max = 255;
RNG rng;
void Demo_Contours(int, void*);
int main()
{
	src_img = imread("tahiti.jpg");
	imshow("image", src_img);
	cvtColor(src_img, src_img, COLOR_BGR2GRAY);

	Demo_Contours(0, 0);

	waitKey(0);
	return 0;
}

void Demo_Contours(int, void*)
{
	Mat canny_output;
	vector<vector<Point>> contours;
	vector<Vec4i> hierachy;

// Canny 边缘检测
	Canny(src_img, canny_output, threshold_value, threshold_value * 2, 3, false);

// 轮廓发现
	findContours(canny_output, contours, hierachy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0));

	dst_img = Mat::zeros(src_img.size(), CV_8UC3);
	RNG rng(12345);
	for (auto i = 0; i < contours.size(); ++i)
	{
        // 随机颜色
		Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));   
        // 轮廓绘制 
		drawContours(dst_img, contours, i, color, 2, 8, hierachy, 0, Point(0, 0));
	}
	imshow(output_win, dst_img);
}

 

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
轮廓拟合直线是指通过对图像中的轮廓进行拟合操作,得到与该轮廓相关联的直线。OpenCV提供了fitLine()函数来实现轮廓拟合直线的功能。该函数的参数包括输入的点集(轮廓点集),以及输出的直线。输出的直线可以使用cv::Vec4f类型(二维)或cv::Vec6f类型(三维)表示。输出的直线包含了直线的方向和一点,可以通过点斜式方程计算直线的斜率和截距。通过这些信息,我们可以得到一条完整的点斜式直线方程。具体的实现代码如下: ```cpp Vec4f oneline; fitLine(contours[i], oneline, DIST_L1, 0, 0.01, 0.01); float k, b; k = oneline / oneline - k * oneline`表示输入的轮廓点集,`oneline`表示输出的直线参数。通过计算直线的斜率和截距,我们可以得到一条完整的点斜式直线方程。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [OpenCV4学习笔记(26)——轮廓拟合直线、寻找极值点、点多边形检测](https://blog.csdn.net/weixin_45224869/article/details/104888172)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *2* [【OpenCV3】直线拟合——cv::fitLine()详解](https://blog.csdn.net/guduruyu/article/details/69505487)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DDsoup

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值