cvDistTransform距离变换

本文探讨了深度学习在图像处理领域的应用,包括AR特效、AI音视频处理、图像处理等技术,详细介绍了OpenCV图像处理、OpenGL ES滤镜、人脸标定AR等关键技术,并阐述了其在实时视频处理、人像分割、人体姿态估计等方面的实际应用。






  1. /*F///////////////////////////////////////////////////////////////////////////////////////
  2. //    Name:     cvDistTransform
  3. //    Purpose:  calculates distance transform of binary image 
  4. //    Context:
  5. //    Parameters:
  6. //      src - source binary image
  7. //      dst - output floating-point image, whose pixel values are distances from
  8. //            the correspondend pixel in the source image to the nearest 0-pixel.
  9. //      disType - type of metric used
  10. //      maskType - size of discrete aperture that approximates the metric.
  11. //      mask - array of 2 (for 3x3 mask) or 3 numbers (for 5x5 mask) that characterizes
  12. //             metric if disType is CV_DIST_USER (user-defined metric)
  13. //    Notes:
  14. //F*/
  15. OPENCVAPI  void  cvDistTransform( IplImage* src, IplImage* dst, CvDisType disType,
  16.                                CvDisMaskType maskType, float* mask CV_DEFAULT(0));


================================================================================




DistTransform

计算输入图像的所有非零元素对其最近零元素的距离

void cvDistTransform( const CvArr* src, CvArr* dst, int distance_type=CV_DIST_L2,

                      int mask_size=3, const float* mask=NULL );

src

输入 8-比特、单通道 (二值) 图像.

dst

含计算出的距离的输出图像(32-比特、浮点数、单通道).

distance_type

距离类型; 可以是 CV_DIST_L1, CV_DIST_L2, CV_DIST_C 或 CV_DIST_USER.

mask_size

距离变换掩模的大小,可以是 3 或 5. 对 CV_DIST_L1 或 CV_DIST_C 的情况,参数值被强制设定为 3, 因为 3×3 mask 给出 5×5 mask 一样的结果,而且速度还更快。

mask

用户自定义距离情况下的 mask。 在 3×3 mask 下它由两个数(水平/垂直位量,对角线位移量)组成, 5×5 mask 下由三个数组成(水平/垂直位移量,对角位移和 国际象棋里的马步(马走日))

函数 cvDistTransform 二值图像每一个象素点到它最邻近零象素点的距离。对零象素,函数设置 0 距离,对其它象素,它寻找由基本位移(水平、垂直、对角线或knight's move,最后一项对 5×5 mask 有用)构成的最短路径。 全部的距离被认为是基本距离的和。由于距离函数是对称的,所有水平和垂直位移具有同样的代价 (表示为 a ), 所有的对角位移具有同样的代价 (表示为 b), 所有的 knight's 移动具有同样的代价 (表示为 c). 对类型 CV_DIST_C 和 CV_DIST_L1,距离的计算是精确的,而类型 CV_DIST_L2 (欧式距离) 距离的计算有某些相对误差 (5×5 mask 给出更精确的结果), OpenCV 使用 [Borgefors86] 推荐的值:

CV_DIST_C (3×3):

a=1, b=1

CV_DIST_L1 (3×3):

a=1, b=2

CV_DIST_L2 (3×3):

a=0.955, b=1.3693

CV_DIST_L2 (5×5):

a=1, b=1.4, c=2.1969

下面用户自定义距离的的距离域示例 (黑点 (0) 在白色方块中间): 用户自定义 3×3 mask (a=1, b=1.5)

4.5

4

3.5

3

3.5

4

4.5

4

3

2.5

2

2.5

3

4

3.5

2.5

1.5

1

1.5

2.5

3.5

3

2

1

0

1

2

3

3.5

2.5

1.5

1

1.5

2.5

3.5

4

3

2.5

2

2.5

3

4

4.5

4

3.5

3

3.5

4

4.5

用户自定义 5×5 mask (a=1, b=1.5, c=2)

4.5

3.5

3

3

3

3.5

4.5

3.5

3

2

2

2

3

3.5

3

2

1.5

1

1.5

2

3

3

2

1

0

1

2

3

3

2

1.5

1

1.5

2

3

3.5

3

2

2

2

3

3.5

4

3.5

3

3

3

3.5

4

典型的使用快速粗略距离估计 CV_DIST_L2, 3×3 mask , 如果要更精确的距离估计,使用 CV_DIST_L2, 5×5 mask。

When the output parameter labels is not NULL, for every non-zero pixel the function also finds the nearest connected component consisting of zero pixels. The connected components themselves are found as contours in the beginning of the function.

In this mode the processing time is still O(N), where N is the number of pixels. Thus, the function provides a very fast way to compute approximate Voronoi diagram for the binary image.

  1. /*F///////////////////////////////////////////////////////////////////////////////////////
  2. //    Name:     cvDistTransform
  3. //    Purpose:  calculates distance transform of binary image 
  4. //    Context:
  5. //    Parameters:
  6. //      src - source binary image
  7. //      dst - output floating-point image, whose pixel values are distances from
  8. //            the correspondend pixel in the source image to the nearest 0-pixel.
  9. //      disType - type of metric used
  10. //      maskType - size of discrete aperture that approximates the metric.
  11. //      mask - array of 2 (for 3x3 mask) or 3 numbers (for 5x5 mask) that characterizes
  12. //             metric if disType is CV_DIST_USER (user-defined metric)
  13. //    Notes:
  14. //F*/
  15. OPENCVAPI  void  cvDistTransform( IplImage* src, IplImage* dst, CvDisType disType,
  16.                                CvDisMaskType maskType, float* mask CV_DEFAULT(0));
内容概要:本文系统介绍了算术优化算法(AOA)的基本原理、核心思想及Python实现方法,并通过图像分割的实际案例展示了其应用价值。AOA是一种基于种群的元启发式算法,其核心思想来源于四则运算,利用乘除运算进行全局勘探,加减运算进行局部开发,通过数学优化器加速函数(MOA)和数学优化概率(MOP)动态控制搜索过程,在全局探索与局部开发之间实现平衡。文章详细解析了算法的初始化、勘探与开发阶段的更新策略,并提供了完整的Python代码实现,结合Rastrigin函数进行测试验证。进一步地,以Flask框架搭建前后端分离系统,将AOA应用于图像分割任务,展示了其在实际工程中的可行性与高效性。最后,通过收敛速度、寻优精度等指标评估算法性能,并提出自适应参数调整、模型优化和并行计算等改进策略。; 适合人群:具备一定Python编程基础和优化算法基础知识的高校学生、科研人员及工程技术人员,尤其适合从事人工智能、图像处理、智能优化等领域的从业者;; 使用场景及目标:①理解元启发式算法的设计思想与实现机制;②掌握AOA在函数优化、图像分割等实际问题中的建模与求解方法;③学习如何将优化算法集成到Web系统中实现工程化应用;④为算法性能评估与改进提供实践参考; 阅读建议:建议读者结合代码逐行调试,深入理解算法流程中MOA与MOP的作用机制,尝试在不同测试函数上运行算法以观察性能差异,并可进一步扩展图像分割模块,引入更复杂的预处理或后处理技术以提升分割效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值