Computer Vision Tutorial 2-7: Angels and Demons

Get into the habit of writing together! This is the 4th day of my participation in the "Nuggets Daily New Plan·April Update Challenge", click to view the details of the event .


1 Image Morphological Operations

In Computer Vision Tutorial 2-2: Detailed Image Filtering Algorithms (with Python combat) , we classify image filtering as follows:

  • Neighborhood filtering
  1. Linear filtering
  2. nonlinear filtering
  • Frequency Domain Filtering
  1. low pass filtering
  2. high pass filter

In nonlinear filtering , only median filtering has been introduced before . In fact, there is also a very common nonlinear filtering method called image morphological operations (Morphological operations) .

Image morphological operations are a class of nonlinear filtering techniques based on image shape operations. The basic idea is to use some special structural elements to measure or extract the corresponding shapes and features in images for further image analysis and processing. The structural element here is equivalent to the template we involve in filtering - a matrix of given pixels, the shape of this matrix can be arbitrary, but generally square.

Next, let's analyze several classic image morphological operation algorithms, and then program and test~

2 Corrosion

Erosion is to replace the target pixel value with the local grayscale minimum value to achieve the erosion of the highlighted area.

For example, there is an image and a structuring element where the blue square of the structuring element represents the template origin.

insert image description here

Next, we start to traverse this image. When we reach the position in the figure below, the minimum grayscale value in the template is the pixel of the gray square, so the pixel at the blue square in the template origin is replaced with gray (originally white, which is replaced by gray). corroded)

insert image description here

After traversing this image, we get

insert image description here

3 Expansion

The expansion is to replace the target pixel value with the local gray value maximum to realize the expansion of the highlighted area.

和腐蚀类似,遍历这张图像,到下图这个位置的时候,模板内灰度最大值是白色方格的像素,因此替换掉模板原点中蓝色方格处的像素为白色(原本为灰色,膨胀了)

insert image description here

就这样遍历完这张图像得到膨胀的最终结果为

insert image description here

上个实物图感受下腐蚀和膨胀的效果

insert image description here

4 开运算与闭运算

理解了图像腐蚀与膨胀,那么开闭运算就很容易了

  • 开运算(Opening):先腐蚀再膨胀
  • 闭运算(Closing):先膨胀再腐蚀

开运算能够除有效去除孤立点、毛刺和小桥;闭运算能够填平小孔,弥合缝隙

insert image description here

上图假设灰色区域为高亮。

5 顶帽运算与底帽运算

顶帽运算与底帽运算是用于表征开闭运算与原图像间差异的运算,类似边缘检测的梯度差

  • 图像顶帽运算:表征原图像与开运算得到的图像之间的区别
  • 图像底帽运算:表征原图像与闭运算得到的图像之间的区别

6 恶魔与天使

先看看原图,一对可爱的猫咪

insert image description here

进行腐蚀操作,首先创建结构元

int eSize = 3;  //结构元尺寸
int s = eSize * 2 + 1;
Mat structureElement = getStructuringElement(MORPH_RECT, Size(s, s), Point(-1, -1)); 
复制代码

接着用结构元遍历原图,OpenCV有封装好的API,直接调用即可

erode(src, dst, structureElement);
imshow("腐蚀操作后:", dst);
复制代码

得到腐蚀后的图像如下所示,眼睛空洞洞的,有点恐怖片内味了,这还是原来那两只猫吗?

insert image description here

膨胀操作类似于腐蚀,也有OpenCV封装好的API

dilate(srcImg, dstImg, structureElement, Point(-1, -1), 1);
imshow("膨胀操作后:", dstImg);
复制代码

膨胀出来的小猫就显得轻松很多,感觉升华了。

insert image description here 今后要是想有一张照片创造光与影正与邪天使与恶魔两种反差,可以考虑采用图像腐蚀和膨胀操作。

完整的工程文件请关注~


计算机视觉基础教程大纲

章号                                    内容

  0                              色彩空间与数字成像

  1                              计算机几何基础

  2 Image enhancement, filtering, pyramids

  3 Image Feature Extraction

  4 Image Feature Description

  5 Image Feature Matching

  6 Stereoscopic vision

  7 Project combat

More high-quality content and supporting codes are welcome to my AI channel "AI Technology Club"

Guess you like

Origin juejin.im/post/7082551497539452942