OpenCv学习笔记(二)------imgproc模块

线性滤波

一般就是用来去噪点,用不同的卷积核进行操作,具体参数可以看官方文档

使用filter2D函数可以自定义自己的滤波。

 filter2D(src, dst, ddepth , kernel, anchor, delta, BORDER_DEFAULT );

src: Source image
dst: Destination image
ddepth: The depth of dst. A negative value (such as −1) indicates that the depth is the same as the source.
kernel: The kernel to be scanned through the image
anchor: The position of the anchor relative to its kernel. The location Point(-1, -1) indicates the center by default.
delta: A value to be added to each pixel during the correlation. By default it is 0
BORDER_DEFAULT: We let this value by default (more details in the following tutorial)

上面我们忽略了一个问题,那就是图像边缘部分没有办法应用核。需要用copyMakeBorder扩展边缘(有两种扩展方式)。

copyMakeBorder( src, dst, top, bottom, left, right, borderType, value );

src: Source image
dst: Destination image
top, bottom, left, right: Length in pixels of the borders at each side of the image. We define them as being 5% of the original size of the image.
borderType: Define what type of border is applied. It can be constant or replicate for this example.
value: If borderType is BORDER_CONSTANT, this is the value used to fill the border pixels.

形态学处理(Morphology Operations)

Morphology is a set of image processing operations that process images based on predefined structuring elements known also as kernels. The value of each pixel in the output image is based on a comparison of the corresponding pixel in the input image with its neighbors. By choosing the size and shape of the kernel, you can construct a morphological operation that is sensitive to specific shapes regarding the input image.

简单来说,就是预定义一个核(矩阵),对输入的图像进行处理,每个像素依赖于其周围的像素,采用不同的核得到不同的输出,所以核是关键。

腐蚀(Erosion)和膨胀(Dilation)
分别在卷积核范围内取最小、最大值。
由于图像是黑白图像,白色是主题pixel值大,黑色是背景。
- cv::erode:腐蚀运算,取小值,白色变黑,图形缩小。
- cv::dilate:膨胀运算,与腐蚀相反。

在这两种运算的基础上,还有OpeningClosingMorphological GradientTop HatBlack Hat运算,具体可以查看文档

知道腐蚀和膨胀的原理之后,还可以提取图像中的特殊线条,比如横线竖线等。

可以学一下这个函数createTrackbar()显示一个进度条,便于调节参数直观看到效果。

Image Pyramids

图像金字塔?进行图像的缩放。
有两种常用的方法:

  • Gaussian pyramid
  • Laplacian pyramid

这里用的是高斯.
高斯核:

1 16 [ 1 4 6 4 1 4 16 24 16 4 6 24 36 24 6 4 16 24 16 4 1 4 6 4 1 ]

缩小时:对图像用高斯核进行卷积运算,然后去掉偶数行和列,将图案缩小至原来的四分之一。
放大时:用高斯核做卷积估计缺失的像素。
OpenCV里提供了两个函数:

Thresholding Operations

HSV色彩空间:
HSV

  • hue(色彩):描述颜色,根据颜色分割物体时有用
  • saturation(饱和度):从白逐渐变黑?
  • Value:描述颜色的亮度或者密度

RGB色彩空间:
RGB
每种颜色三个通道,无法用来根据颜色分割物体。


可以用cv::cvtColor()函数转换色彩空间

// Convert from BGR to HSV colorspace
cvtColor(frame, frame_HSV, COLOR_BGR2HSV);

threshold()函数:根据某个阈值将图像分割。
inRange(src, low, up, dst)函数:简单来说就是将src图像中颜色在low和up之间的像素提取到dst,是threshold函数的进阶版。

边缘检测

  • 一阶导
    这里用到了导数。图像边缘就是像素值变化较大的区域,即像素值变化率大,即导数值。
    OpenCV提供了Sobel()函数。

    Sobel(src_gray, grad_x, ddepth, 1, 0, ksize, scale, delta, BORDER_DEFAULT);
    Sobel(src_gray, grad_y, ddepth, 0, 1, ksize, scale, delta, BORDER_DEFAULT);

    src_gray: In our example, the input image. Here it is CV_8U
    grad_x / grad_y : The output image.
    ddepth: The depth of the output image. We set it to CV_16S to avoid overflow.
    x_order: The order of the derivative in x direction.
    y_order: The order of the derivative in y direction.
    scale, delta and BORDER_DEFAULT: We use default values.

    这里分别计算了x方向和y方向的边缘grad_xgrad_y,然后再相加就得到图像的边缘。

  • 二阶导
    还可以用二阶导数值为0来判断边缘,OpenCV提供了 Laplacian() 函数

  • Canny Edge Detector
    cv::Canny函数

检测直线

在检测出边缘的基础上检测直线。

r θ = x 0 cos θ + y 0 sin θ

上式中若 ( x 0 , y 0 ) 为定值,那么任意 ( θ , r θ ) 对表示经过 ( x 0 , y 0 ) 的直线簇。
若三点的直线簇有公共直线,那么这三点共线。

HoughLines():得到检测到的直线的 ( θ , r θ )
HoughLinesP():得到直线的两端点

检测圆:先做边缘检测推断圆心,然后对待定圆心找到最合适的半径。用这个函数HoughCircles()

仿射变换

cv::remap:对像素进行变换,比如水平翻转图像

getAffineTransform:得到放射变换矩阵

cv::warpAffine:根据变换矩阵进行变换,扭曲?

cv::getRotationMatrix2D :旋转


未完待续:)

猜你喜欢

转载自blog.csdn.net/yijiull/article/details/81545619