opencv 直线拟合

本文介绍了如何利用OpenCV进行直线拟合,特别是在霍夫直线检测效果不佳时,通过二值分析、骨架提取来拟合直线的方法。涉及距离变换、水平与垂直投影,并提供了C++和Python的实现代码示例。
摘要由CSDN通过智能技术生成

一、话说直线拟合

霍夫直线检测容易受到线段形状与噪声的干扰而失真,这个时候我们需要另辟蹊径,通过对图像进行二值分析,提取骨架,对骨架像素点拟合生成直线,这种做法在一些场景下非常有效,而且效果还比较好。前面已经讲过霍夫变换,这里就不再叙述,直接上干货。

二、距离变换

距离变换是二值图像处理与操作中常用手段,在骨架提取,图像窄化中常有应用。距离变换的结果是得到一张与输入图像类似的灰度图像,但是灰度值只出现在前景区域。并且越远离背景边缘的像素灰度值越大。OpenCV中距离变换的函数如下:

void distanceTransform( InputArray src, OutputArray dst, int distanceType, int maskSize, int dstType=CV_32F);@param src 8-bit, single-channel (binary) source image.

@param dst Output image with calculated distances. It is a 8-bit or 32-bit floating-point,single-channel image of the same size as src .

@param distanceType Type of distance, see cv::DistanceTypes

@param maskSize Size of the distance transform mask, see cv::DistanceTransformMasks. In case of the  DIST_L1 or DIST_C distance type, the parameter is forced to 3 because a \f$3\times 3\f$ mask gives
the same result as \f$5\times 5\f$ or any larger aperture.
@param dstType Type of output image. It can be CV_8U or CV_32F. Type CV_8U can be used only for  the first variant of the function and distanceType == DIST_L1.

DIST_L1、DIST_L2、DIST_C计算像素距离方式如下:

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值