版权声明:本文为博主原创文章,可以自由转载。 https://blog.csdn.net/u010953692/article/details/83957367
python opencv 图像膨胀
1,opencv 图像 形态变换
#! /usr/local/bin/python3
# coding:utf-8
"""
图像二值化 全局阀值
"""
from PIL import Image
import cv2
import numpy as np
src = "/root/captcha_png.png"
import cv2
img = cv2.imread(src , 0)
#高斯滤波
gauss = cv2.GaussianBlur(img,(5,5),0)
#全局二值化
ret , thresh1 = cv2.threshold(gauss ,127, 255, cv2.THRESH_BINARY)
cv2.imwrite("/root/captcha_gauss.png", thresh1)
kernel = np.ones((3,3), np.uint8)
#erosion = cv2.erode(thresh1, kernel, iterations=1)
dilation = cv2.dilate(thresh1, kernel, iterations=1)
cv2.imwrite("/root/captcha_dilation.png", dilation)
cv2.imshow("dilation", dilation)
cv2.waitKey()
- 二值化 高斯滤波
- 膨胀后图片
2,闭运算 开运算
#! /usr/local/bin/python3
# coding:utf-8
"""
图像二值化 全局阀值
"""
from PIL import Image
import cv2
import numpy as np
src = "/root/captcha_png.png"
import cv2
img = cv2.imread(src , 0)
#高斯滤波
gauss = cv2.GaussianBlur(img,(5,5),0)
#全局二值化
ret , thresh1 = cv2.threshold(gauss ,127, 255, cv2.THRESH_BINARY)
cv2.imwrite("/root/captcha_gauss.png", thresh1)
kernel = np.ones((3,3), np.uint8)
#erosion = cv2.erode(thresh1, kernel, iterations=1)
dilation = cv2.dilate(thresh1, kernel, iterations=1)
cv2.imwrite("/root/captcha_dilation.png", dilation)
#cv2.imshow("dilation", dilation)
#cv2.waitKey()
#定义结构元素
rect = cv2.getStructuringElement(cv2.MORPH_RECT, (2,2))
#闭运算
closed = cv2.morphologyEx(dilation, cv2.MORPH_CLOSE, rect)
cv2.imshow("close", closed)
cv2.waitKey()
opened = cv2.morphologyEx(dilation, cv2.MORPH_OPEN, rect)
cv2.imshow("open", opened)
cv2.waitKey()
- 闭运算
- 开运算
参考: