开运算:先做腐蚀后做膨胀(腐蚀可以理解为缩小)
闭运算:先膨胀后腐蚀
低于threshold就置为0,高于threshold就置为255
# -*- coding: utf-8 -*-
import cv2
import numpy as np
img = cv2.imread('E://pic//9.jpg')
img2 = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, dst = cv2.threshold(img2, 180, 255, cv2.THRESH_BINARY)
print(dst.shape)
cv2.imshow('img', img)
cv2.imshow('gray', img2)
cv2.imshow('bin', dst)
key = cv2.waitKey(0) & 0xff
if key == ord('q'):
cv2.destroyAllWindows()
# -*- coding: utf-8 -*-
import cv2
import numpy as np
img = cv2.imread('E://pic//9.jpg')
img2 = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, dst = cv2.threshold(img2, 180, 255, cv2.THRESH_BINARY_INV)
print(dst.shape)
cv2.imshow('img', img)
cv2.imshow('gray', img2)
cv2.imshow('bin', dst)
key = cv2.waitKey(0) & 0xff
if key == ord('q'):
cv2.destroyAllWindows()
# -*- coding: utf-8 -*-
import cv2
import numpy as np
img = cv2.imread('E://pic//9.jpg')
img2 = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, dst = cv2.threshold(img2, 60, 255, cv2.THRESH_BINARY)
ret, dst2 = cv2.threshold(img2, 60, 255, cv2.THRESH_BINARY_INV)
print(dst.shape)
cv2.imshow('img', img)
cv2.imshow('gray', img2)
cv2.imshow('bin', dst)
cv2.imshow('bin2', dst2)
key = cv2.waitKey(0) & 0xff
if key == ord('q'):
cv2.destroyAllWindows()
# -*- coding: utf-8 -*-
import cv2
import numpy as np
# img = cv2.imread('E://pic//9.jpg')
cv2.namedWindow('img', cv2.WINDOW_NORMAL)
cv2.namedWindow('gray', cv2.WINDOW_NORMAL)
cv2.namedWindow('bin', cv2.WINDOW_NORMAL)
cv2.namedWindow('bin2', cv2.WINDOW_NORMAL)
img = cv2.imread('./math.png')
img2 = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, dst = cv2.threshold(img2, 180, 255, cv2.THRESH_BINARY)
ret, dst2 = cv2.threshold(img2, 180, 255, cv2.THRESH_BINARY_INV)
print(dst.shape)
cv2.imshow('img', img)
cv2.imshow('gray', img2)
cv2.imshow('bin', dst)
cv2.imshow('bin2', dst2)
key = cv2.waitKey(0) & 0xff
if key == ord('q'):
cv2.destroyAllWindows()
# -*- coding: utf-8 -*-
import cv2
import numpy as np
cv2.namedWindow('img', cv2.WINDOW_NORMAL)
cv2.namedWindow('gray', cv2.WINDOW_NORMAL)
cv2.namedWindow('bin', cv2.WINDOW_NORMAL)
cv2.namedWindow('bin2', cv2.WINDOW_NORMAL)
img = cv2.imread('./math.png')
img2 = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
dst = cv2.adaptiveThreshold(img2, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 0)
dst2 = cv2.adaptiveThreshold(img2, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 11, 0)
cv2.imshow('img', img)
cv2.imshow('gray', img2)
cv2.imshow('bin', dst)
cv2.imshow('bin2', dst2)
key = cv2.waitKey(0) & 0xff
if key == ord('q'):
cv2.destroyAllWindows()
# -*- coding: utf-8 -*-
import cv2
import numpy as np
cv2.namedWindow('img', cv2.WINDOW_NORMAL)
cv2.namedWindow('dst', cv2.WINDOW_NORMAL)
img = cv2.imread('./j.png')
kernel = np.ones((3, 3), np.uint8)
dst = cv2.erode(img, kernel, iterations=1)
cv2.imshow('img', img)
cv2.imshow('dst', dst)
key = cv2.waitKey(0) & 0xff
if key == ord('q'):
cv2.destroyAllWindows()
# -*- coding: utf-8 -*-
import cv2
import numpy as np
cv2.namedWindow('img', cv2.WINDOW_NORMAL)
cv2.namedWindow('dst', cv2.WINDOW_NORMAL)
img = cv2.imread('./j.png')
# kernel = np.ones((7, 7), np.uint8)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (7, 7))
print(kernel)
dst = cv2.erode(img, kernel, iterations=1)
cv2.imshow('img', img)
cv2.imshow('dst', dst)
key = cv2.waitKey(0) & 0xff
if key == ord('q'):
cv2.destroyAllWindows()
原来黑色区域变成白色
卷积核大小意味着膨胀的速度,卷积核越大膨胀越快
# -*- coding: utf-8 -*-
import cv2
import numpy as np
cv2.namedWindow('img', cv2.WINDOW_NORMAL)
cv2.namedWindow('dst', cv2.WINDOW_NORMAL)
img = cv2.imread('./j.png')
# kernel = np.ones((7, 7), np.uint8)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (7, 7))
print(kernel)
# 腐蚀
# dst = cv2.erode(img, kernel, iterations=1)
# 膨胀
dst = cv2.dilate(img, kernel, iterations=1)
cv2.imshow('img', img)
cv2.imshow('dst', dst)
key = cv2.waitKey(0) & 0xff
if key == ord('q'):
cv2.destroyAllWindows()
# -*- coding: utf-8 -*-
import cv2
import numpy as np
cv2.namedWindow('img', cv2.WINDOW_NORMAL)
cv2.namedWindow('dst', cv2.WINDOW_NORMAL)
cv2.namedWindow('dst2', cv2.WINDOW_NORMAL)
img = cv2.imread('./j.png')
# kernel = np.ones((7, 7), np.uint8)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (7, 7))
print(kernel)
# 腐蚀
dst = cv2.erode(img, kernel, iterations=1)
# 膨胀
dst2 = cv2.dilate(dst, kernel, iterations=1)
cv2.imshow('img', img)
cv2.imshow('dst', dst)
cv2.imshow('dst2', dst2)
key = cv2.waitKey(0) & 0xff
if key == ord('q'):
cv2.destroyAllWindows()
# -*- coding: utf-8 -*-
import cv2
import numpy as np
cv2.namedWindow('img', cv2.WINDOW_NORMAL)
cv2.namedWindow('dst', cv2.WINDOW_NORMAL)
cv2.namedWindow('dst2', cv2.WINDOW_NORMAL)
img = cv2.imread('./j.png')
# kernel = np.ones((7, 7), np.uint8)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
print(kernel)
# 腐蚀
dst = cv2.erode(img, kernel, iterations=1)
# 膨胀
dst2 = cv2.dilate(dst, kernel, iterations=1)
cv2.imshow('img', img)
cv2.imshow('dst', dst)
cv2.imshow('dst2', dst2)
key = cv2.waitKey(0) & 0xff
if key == ord('q'):
cv2.destroyAllWindows()
开运算能达到消除噪点的效果
对噪点比较大的图像,我们用大的卷积核,消除噪点能力更强
# -*- coding: utf-8 -*-
import cv2
import numpy as np
cv2.namedWindow('img', cv2.WINDOW_NORMAL)
cv2.namedWindow('dst', cv2.WINDOW_NORMAL)
cv2.namedWindow('dst2', cv2.WINDOW_NORMAL)
img = cv2.imread('./dotj.png')
# kernel = np.ones((7, 7), np.uint8)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (7, 7))
# print(kernel)
# 腐蚀
dst = cv2.erode(img, kernel, iterations=1)
# 膨胀
dst2 = cv2.dilate(dst, kernel, iterations=1)
cv2.imshow('img', img)
cv2.imshow('dst', dst)
cv2.imshow('dst2', dst2)
key = cv2.waitKey(0) & 0xff
if key == ord('q'):
cv2.destroyAllWindows()
# -*- coding: utf-8 -*-
import cv2
import numpy as np
cv2.namedWindow('img', cv2.WINDOW_NORMAL)
cv2.namedWindow('dst', cv2.WINDOW_NORMAL)
# cv2.namedWindow('dst2', cv2.WINDOW_NORMAL)
img = cv2.imread('./dotj.png')
# kernel = np.ones((7, 7), np.uint8)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (7, 7))
# print(kernel)
# 腐蚀
# dst = cv2.erode(img, kernel, iterations=1)
# 膨胀
# dst2 = cv2.dilate(dst, kernel, iterations=1)
dst = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)
cv2.imshow('img', img)
cv2.imshow('dst', dst)
# cv2.imshow('dst2', dst2)
key = cv2.waitKey(0) & 0xff
if key == ord('q'):
cv2.destroyAllWindows()
# -*- coding: utf-8 -*-
import cv2
import numpy as np
cv2.namedWindow('img', cv2.WINDOW_NORMAL)
cv2.namedWindow('dst', cv2.WINDOW_NORMAL)
# cv2.namedWindow('dst2', cv2.WINDOW_NORMAL)
img = cv2.imread('./dotinj.png')
# kernel = np.ones((7, 7), np.uint8)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (7, 7))
# print(kernel)
# 腐蚀
# dst = cv2.erode(img, kernel, iterations=1)
# 膨胀
# dst2 = cv2.dilate(dst, kernel, iterations=1)
# dst = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)
dst = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)
cv2.imshow('img', img)
cv2.imshow('dst', dst)
# cv2.imshow('dst2', dst2)
key = cv2.waitKey(0) & 0xff
if key == ord('q'):
cv2.destroyAllWindows()
# -*- coding: utf-8 -*-
import cv2
import numpy as np
cv2.namedWindow('img', cv2.WINDOW_NORMAL)
cv2.namedWindow('dst', cv2.WINDOW_NORMAL)
# cv2.namedWindow('dst2', cv2.WINDOW_NORMAL)
img = cv2.imread('./j.png')
# kernel = np.ones((7, 7), np.uint8)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (7, 7))
# print(kernel)
# 腐蚀
# dst = cv2.erode(img, kernel, iterations=1)
# 膨胀
# dst2 = cv2.dilate(dst, kernel, iterations=1)
# 开运算
# dst = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)
# 闭运算
# dst = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)
dst = cv2.morphologyEx(img, cv2.MORPH_GRADIENT, kernel)
cv2.imshow('img', img)
cv2.imshow('dst', dst)
# cv2.imshow('dst2', dst2)
key = cv2.waitKey(0) & 0xff
if key == ord('q'):
cv2.destroyAllWindows()
现在我们换成3x3的kernel
# -*- coding: utf-8 -*-
import cv2
import numpy as np
cv2.namedWindow('img', cv2.WINDOW_NORMAL)
cv2.namedWindow('dst', cv2.WINDOW_NORMAL)
# cv2.namedWindow('dst2', cv2.WINDOW_NORMAL)
img = cv2.imread('./j.png')
# kernel = np.ones((7, 7), np.uint8)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
# print(kernel)
# 腐蚀
# dst = cv2.erode(img, kernel, iterations=1)
# 膨胀
# dst2 = cv2.dilate(dst, kernel, iterations=1)
# 开运算
# dst = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)
# 闭运算
# dst = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)
dst = cv2.morphologyEx(img, cv2.MORPH_GRADIENT, kernel)
cv2.imshow('img', img)
cv2.imshow('dst', dst)
# cv2.imshow('dst2', dst2)
key = cv2.waitKey(0) & 0xff
if key == ord('q'):
cv2.destroyAllWindows()
# -*- coding: utf-8 -*-
import cv2
import numpy as np
# cv2.namedWindow('img', cv2.WINDOW_NORMAL)
# cv2.namedWindow('dst', cv2.WINDOW_NORMAL)
# cv2.namedWindow('dst2', cv2.WINDOW_NORMAL)
img = cv2.imread('./tophat.png')
# kernel = np.ones((7, 7), np.uint8)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (19, 19))
# print(kernel)
# 腐蚀
# dst = cv2.erode(img, kernel, iterations=1)
# 膨胀
# dst2 = cv2.dilate(dst, kernel, iterations=1)
# 开运算
# dst = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)
# 闭运算
# dst = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)
# 形态学梯度
# dst = cv2.morphologyEx(img, cv2.MORPH_GRADIENT, kernel)
# 顶帽运算
dst = cv2.morphologyEx(img, cv2.MORPH_TOPHAT, kernel)
cv2.imshow('img', img)
cv2.imshow('dst', dst)
# cv2.imshow('dst2', dst2)
key = cv2.waitKey(0) & 0xff
if key == ord('q'):
cv2.destroyAllWindows()
# -*- coding: utf-8 -*-
import cv2
import numpy as np
# cv2.namedWindow('img', cv2.WINDOW_NORMAL)
# cv2.namedWindow('dst', cv2.WINDOW_NORMAL)
# cv2.namedWindow('dst2', cv2.WINDOW_NORMAL)
img = cv2.imread('./dotinj.png')
# kernel = np.ones((7, 7), np.uint8)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (7, 7))
# print(kernel)
# 腐蚀
# dst = cv2.erode(img, kernel, iterations=1)
# 膨胀
# dst2 = cv2.dilate(dst, kernel, iterations=1)
# 开运算
# dst = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)
# 闭运算
# dst = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)
# 形态学梯度
# dst = cv2.morphologyEx(img, cv2.MORPH_GRADIENT, kernel)
# 顶帽运算
# dst = cv2.morphologyEx(img, cv2.MORPH_TOPHAT, kernel)
# 黑帽操作
dst = cv2.morphologyEx(img, cv2.MORPH_BLACKHAT, kernel)
cv2.imshow('img', img)
cv2.imshow('dst', dst)
# cv2.imshow('dst2', dst2)
key = cv2.waitKey(0) & 0xff
if key == ord('q'):
cv2.destroyAllWindows()
之后我会持续更新,如果喜欢我的文章,请记得一键三连哦,点赞关注收藏,你的每一个赞每一份关注每一次收藏都将是我前进路上的无限动力 !!!↖(▔▽▔)↗感谢支持!