【opencv 官方教程】翻译3 图像处理 下

边缘检测

  • Canny Edge Detector

    Compatibility: > OpenCV 2.0

    Author: Ana Huamán

    Where we learn a sophisticated alternative to detect edges.

理论

 Canny Edge detector,也叫optimal detector,是由John F. Canny于1986年开发的。这种算法旨在满足下面三个标尺:

  • Low error rate: Meaning a good detection of only existent edges.
  • 低错误率:仅仅在边缘检测出轮廓
  • Good localization: The distance between edge pixels detected and real edge pixels have to be minimized.
  • 精准定位:检测出的边缘尽可能的与实际边缘重合
  • Minimal response: Only one detector response per edge.
  • 最小回应:一个边缘只产生一个检测结果

步骤

 过滤噪声:参见平滑处理

 发现图像梯度大的部分:参见梯度;额外的,真正的梯度应有方向,这里是计算了方向的。

 截取非最大部分:保存轮廓线边缘

 Hysteresis(迟滞):对于高于阈值上线的部分视为边界,低于阈值下线的被忽视,处于两个阈值之间的,且于高于阈值上线部分相连的,也被视为边界。


执行过程:

Create some needed variables 声明所需变量

Loads the source image 装载图片

Create a matrix of the same type and size of src (to be dst) 为输出图像开辟空间

Convert the image to grayscale (using the function cv::cvtColor  灰度转换

Create a window to display the results 创建显示窗口

Create a Trackbar for the user to enter the lower threshold for our Canny detector 创建Trackbar对象,输入阈值下线

Let's check the CannyThreshold function, step by step Canny算法实现

We fill a dst image with zeros (meaning the image is completely black) 初始化输出图像矩阵

Finally, we will use the function cv::Mat::copyTo to map only the areas of the image that are identified as edges 将处理结果拷贝到输出图像中

We display our result:显示结果

Hough变换

  • Hough Line Transform

    Compatibility: > OpenCV 2.0

    Author: Ana Huamán

    Where we learn how to detect lines

  • Hough Circle Transform

    Compatibility: > OpenCV 2.0

    Author: Ana Huamán

    Where we learn how to detect circles

这个我并不懂啊,据说是有很好的 检测直线、圆的能力,但又被说性能不行。

重映射

  • Remapping

    Compatibility: > OpenCV 2.0

    Author: Ana Huamán

    Where we learn how to manipulate pixels locations

这节提出的理论:

 假如有一个图像表达式:h(x,y),表示在一定区间内,给定一个坐标(x,y),则对应于该坐标的图像的值为h(x,y),这是原图像在坐标上的映射。

 现在把他重映射了,即有一个映射函数f,输入参数为原图像中的某点,能得到对应输出图像中的另外一点。

g(x,y)=f(h(x,y))
d


源码中的map_x map_y构成f(),即dst(x,y) = src(map_x(x,y),map_y(x,y)),然后通过remap函数实现重映射。


补充:我觉得吧,这里看似多此一举的绘制map_x、map_y,实际上是一种高效开发的必然。这样可以让操作和功能分离,保证了功能的自由拓展能力,而避免了重复的对操作(remap)过程的调试开发。

仿射变换

  • Affine Transformations

    Compatibility: > OpenCV 2.0

    Author: Ana Huamán

    Where we learn how to rotate, translate and scale our images

理论请自行查看,这是一种相对于重映射更为固定化的过程,因此具有更具描述性的操作。

本节提出两种变换手段:

cv::warpAffine  cv::getRotationMatrix2D

Warp_Affine_Tutorial_Result_Warp.jpg

像素均衡、直方图均衡化 Histogram Equalization

  • Histogram Equalization

    Compatibility: > OpenCV 2.0

    Author: Ana Huamán

    Where we learn how to improve the contrast in our images


Histogram_Equalization_Theory_1.jpg
理论:像素直方图是一个分布不太均匀的东西,这让我们感觉图像不够饱满。现在强行把他们之间的距离拉大,比如原先只有深红、猩红、血红这样色差不大的一些颜色,现在为了增加表现力,我把他们改为红黑、亮红、浅红这样差别比较大的颜色。

背景投影

  • Back Projection

    Compatibility: > OpenCV 2.0

    Author: Ana Huamán

    Where we learn how to use histograms to find similar objects in images

。。。貌似能抓住图像中的鲜艳部分,有挖出背景的功能。各位大神原谅我这弱鳥,实在是啃不动这些图形图像专业的东西。我尽量把更多直接明了的部分放进来,有需要用到这些功能的可以方便检索。


Back_Projection1_Source_Image.jpgBack_Projection1_BackProj.jpgBack_Projection1_Histogram.jpg

模式匹配(模板搜索)

  • Template Matching

    Compatibility: > OpenCV 2.0

    Author: Ana Huamán

    Where we learn how to match templates in an image

功能:找到src中与目标最为匹配的区域

这里提出了六种匹配方式:

  1. method=CV_TM_SQDIFF

    R(x,y)=x,y(T(x,y)I(x+x,y+y))2
  2. method=CV_TM_SQDIFF_NORMED

    R(x,y)=x,y(T(x,y)I(x+x,y+y))2x,yT(x,y)2x,yI(x+x,y+y)2
  3. method=CV_TM_CCORR

    R(x,y)=x,y(T(x,y)I(x+x,y+y))
  4. method=CV_TM_CCORR_NORMED

    R(x,y)=x,y(T(x,y)I(x+x,y+y))x,yT(x,y)2x,yI(x+x,y+y)2
  5. method=CV_TM_CCOEFF

    R(x,y)=x,y(T(x,y)I(x+x,y+y))

    where

    T(x,y)=T(x,y)1/(wh)x,yT(x,y)I(x+x,y+y)=I(x+x,y+y)1/(wh)x,yI(x+x,y+y)
  6. method=CV_TM_CCOEFF_NORMED

    R(x,y)=x,y(T(x,y)I(x+x,y+y))x,yT(x,y)2x,yI(x+x,y+y)2



寻找轮廓

寻找和绘制轮廓
Find_Contours_Original_Image.jpg
Find_Contours_Result.jpg

凸包

  • Convex Hull

    Compatibility: > OpenCV 2.0

    Author: Ana Huamán

    Where we learn how to get hull contours and draw them!

Hull_Original_Image.jpg
Original
Hull_Result.jpg

创建边界区域、圆


关键看这里



  1. For every found contour we now apply approximation to polygons with accuracy +-3 and stating that the curve must me closed.

    After that we find a bounding rect for every polygon and save it to boundRect.

    At last we find a minimum enclosing circle for every polygon and save it to center and radius vectors.

    for( size_t i = 0; i < contours.size(); i++ )
    {
    approxPolyDP( Mat(contours[i]), contours_poly[i], 3, true );
    boundRect[ i] = boundingRect( Mat(contours_poly[i]) );
    minEnclosingCircle( contours_poly[i], center[i], radius[i] );
    }

    We found everything we need, all we have to do is to draw.

效果:

Bounding_Rects_Circles_Source_Image.jpg
Bounding_Rects_Circles_Result.jpg






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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值