当前位置:网站首页>Morphology of image
Morphology of image
2022-07-28 22:45:00 【WL725】
List of articles
1 Probability of morphology
Image morphology refers to the morphological features of processed images , In essence, convolution kernel is used to calculate the shape or feature of the extracted image .
Morphological processing is generally black-and-white pixel pictures ( The pixel is 0 perhaps 255)
Common morphological processing methods :
- Expansion and corrosion
- Open operation
- Closed operation
- Top hat
- Black hat
2 Image global binarization
Global binarization : Turn each pixel into 0or255
cv2.threshold(src, thresh, maxval, type):
src: picture , Preferably a grayscale image
thresh: threshold , How about being greater than a certain value , How about less than a certain value
maxval: Maximum , Is not necessarily 255
type: Operation type :
cv2.THRESH_BINARY : Exceeding the threshold is max
cv2.THRESH_BINARY_INV: Exceeding the threshold is 0
cv2.THRESH_TOZERO: When the threshold is exceeded, it remains unchanged , Less than change 0
import cv2
import numpy as np
img = cv2.imread('imgs/66.jpg')
# Binarization of gray-scale photos , Grayscale first
gary = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Two valued , Return two values : threshold + Processed pictures
# The higher the threshold, the darker , Reasonable adjustment
thresh, dst = cv2.threshold(gary, 120, 255, cv2.THRESH_BINARY)
cv2.imshow(' Two valued ', np.hstack((gary, dst)))
cv2.waitKey(0)
3 Adaptive threshold binarization
Using global binarization can not get a suitable threshold for some pixels of the image , Through the automatic calculation of a certain pixel of the picture , Get the corresponding threshold .
- Parameter description :
cv2.adaptiveThreshold(src, maxValue, adaptiveMethod, thresholdType, blockSize, C) - adaptiveMethod Method of calculating threshold :
-cv2.ADAPTIVE_THRESH_MEAN_C: Take the average value of adjacent areas
-cv2.ADAPTIVE_THRESH_GAUSSIAN_C: A weighted sum of adjacent areas , Gaussian window - blockSize: The adjacent threshold size when calculating the threshold
- C: constant : Average or Subtract this constant from the weighted average
import cv2
import numpy as np
img = cv2.imread('imgs/66.jpg')
# Binarization of gray-scale photos , Grayscale first
gary = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Adaptive binarization
dst = cv2.adaptiveThreshold(gary,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,9,0)
cv2.imshow(' Adaptive binarization ', np.hstack((gary, dst)))
cv2.waitKey(0)
4 Corrosion operation
After convolution kernel scanning of corrosion operation , It is white only when all the contents covered are white , Otherwise it's black .
K K K = [ 1 1 1 1 1 1 1 1 1 ] \begin{bmatrix} 1 &1&1 \\ 1 & 1& 1 \\ 1 & 1& 1 \end{bmatrix} ⎣⎡111111111⎦⎤
Parameter description :
- cv2.erode(src,kernel)
import cv2
import numpy as np
img = cv2.imread('imgs/66.jpg')
# Binarization of gray-scale photos , Grayscale first
gary = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Corrosion operation
kernel = np.zeros((3, 3), np.uint8)
dst = cv2.erode(gary, kernel)
cv2.imshow(' Corrosion operation ', np.hstack((gary, dst)))
cv2.waitKey(0)
5 Morphological convolution kernel
The above method also needs to be called manually kernel, Use opencv Built in api Convolution kernel can be obtained directly
cv2.getStructuringElement(shape,ksize)
- shape Is the size of the convolution kernel ,33,55 etc.
- cv2.MORPH_RECT : The value of convolution kernel is 1 Matrix
- cv2.MORPH_ELLIPSE: The ellipse
- cv2.MORPH_CROSS: cross
import cv2
import numpy as np
img = cv2.imread('imgs/66.jpg')
img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
# Get the convolution kernel
kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(5,5))
# Corrosion operation
std = cv2.erode(img,kernel)
cv2.imshow(' Automatically obtain convolution kernel ',np.hstack((img,std)))
cv2.waitKey(0)
6 Expansion operation
The expansion operation is completely opposite to the corrosion operation ,
Enlarge the pixel by one circle , Turn white
- cv2.dilate(scr,kernel)
import cv2
import numpy as np
img = cv2.imread('imgs/66.jpg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Generate convolution kernel
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))
# Expansion operation
std = cv2.dilate(img, kernel)
# display picture
cv2.imshow(' inflation ', np.hstack((img, std)))
cv2.waitKey(0)
7 Open operation
Open operation = corrosion + inflation
- cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel,iterations=2)
import cv2
import numpy as np
img = cv2.imread('imgs/66.jpg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Generate convolution kernel
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))
# Use the open operation : Corrosion before expansion
std = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel,iterations=2)
# display picture
cv2.imshow(' inflation ', np.hstack((img, std)))
cv2.waitKey(0)
8 Closed operation
Expand first and then corrode
cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel,iterations=2)
import cv2
import numpy as np
img = cv2.imread('imgs/66.jpg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Generate convolution kernel
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))
# Use closed operations : Expand first and then corrode . Used to remove the noise of content
std = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel,iterations=2)
# display picture
cv2.imshow(' inflation ', np.hstack((img, std)))
cv2.waitKey(0)
9 Morphological gradients
- Basic concepts : The original image minus the image after corrosion : That is, the picture edge
- std = cv2.morphologyEx(img, cv2.MORPH_GRADIENT, kernel,iterations=1)
import cv2
import numpy as np
img = cv2.imread('imgs/66.jpg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Generate convolution kernel
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
# Get the edge of morphology
std = cv2.morphologyEx(img, cv2.MORPH_GRADIENT, kernel,iterations=1)
# display picture
cv2.imshow(' inflation ', np.hstack((img, std)))
cv2.waitKey(0)
10 Top hat operation
Top hat = Original picture - Open operation
Open operation can pick out the noise outside the picture
namely : Top hat operation can get rid of the noise
- std = cv2.morphologyEx(img, cv2.MORPH_TOPHAT, kernel, iterations=1)
import cv2
import numpy as np
img = cv2.imread('imgs/66.jpg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Generate convolution kernel
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))
# Top hat operation , Get the noise outside the picture
std = cv2.morphologyEx(img, cv2.MORPH_TOPHAT, kernel, iterations=1)
# display picture
cv2.imshow(' inflation ', np.hstack((img, std)))
cv2.waitKey(0)
11 Black hat operation
- Black hat = Original picture - Closed operation
- Close operation is to remove the noise inside the picture
- Black hat gets the internal noise of the picture
- std = cv2.morphologyEx(img, cv2.MORPH_BLACKHAT, kernel, iterations=1)
import cv2
import numpy as np
img = cv2.imread('imgs/66.jpg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Generate convolution kernel
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))
# Black hat operation , Get the noise in the picture
std = cv2.morphologyEx(img, cv2.MORPH_BLACKHAT, kernel, iterations=1)
# display picture
cv2.imshow(' inflation ', np.hstack((img, std)))
cv2.waitKey(0)
边栏推荐
- Gd32f303 firmware library development (10) -- dual ADC polling mode scanning multiple channels
- LVS+KeepAlived高可用部署实战应用
- Bluetooth smart Bracelet system based on STM32 MCU
- Use PCL to batch convert point cloud.Bin files to.Pcd
- flask之蓝图 补充openpyxl
- 从 IPv4 向 IPv6 的迁移
- 771. 字符串中最长的连续出现的字符
- Stm32subeide (10) -- ADC scans multiple channels in DMA mode
- Using PCL to batch display PCD point cloud data flow
- Baidu map usage
猜你喜欢

STM32 - Basic timer (tim6, tim7) working process, interpretation function block diagram, timing analysis, cycle calculation
![[connect your mobile phone wirelessly] - debug your mobile device wirelessly via LAN](/img/7f/c49fd8c2cbe21585a080852833dcb4.png)
[connect your mobile phone wirelessly] - debug your mobile device wirelessly via LAN

Paddlenlp text classification based on ernir3.0: take wos dataset as an example (hierarchical classification)

Summary of common error types in JS

简单的es高亮实战
![[reprint] the token token is used in the login scenario](/img/84/77dc2316e2adc380a580e2456c0e59.png)
[reprint] the token token is used in the login scenario

【转载】token令牌在登录场景使用

Qt+ffmpeg environment construction

Using PCL to batch display PCD point cloud data flow

STM32 -- program startup process
随机推荐
ATT&CK初步了解
OSV_ q The size of tensor a (704) must match the size of tensor b (320) at non-singleton dime
PC side web page special effects (offset series, obtain the coordinates of the mouse in the box, pop-up drag effect, magnifying glass effect)
从 IPv4 向 IPv6 的迁移
842. Arrange numbers
JVM——自定义类加载器
es学习目录
LVS+KeepAlived高可用部署实战应用
JS get the current time (year month day hour minute second)
微信小程序里button点击的时候会边框有黑线
Draem+sspcab [anomaly detection: block]
PC side special effects - animation function
JS获取当前时间(年月日时分秒)
Find out the maximum value of all indicators in epoch [tips]
Labelme labels circular objects [tips]
JS array merging, de duplication, dimensionality reduction (es6: extended operator, set)
B站713故障后的多活容灾建设|TakinTalks大咖分享
Awk blank line filtering
ES6 concept
C语言学习内容总结