OpenCV中的形态学8

文章目录

  • 形态学概述
  • 图像全局二值化
  • 阈值类型
  • 自适应阈值二值化
  • OpenCV腐蚀
  • 获取形态学卷积核
  • OpenCV膨胀
  • 开运算
  • 闭运算
  • 形态学梯度
  • 顶帽运算
  • 黑帽操作
  • 小结

形态学概述

OpenCV中的形态学8_第1张图片
开运算:先做腐蚀后做膨胀(腐蚀可以理解为缩小)
闭运算:先膨胀后腐蚀
OpenCV中的形态学8_第2张图片
OpenCV中的形态学8_第3张图片

图像全局二值化

低于threshold就置为0,高于threshold就置为255
OpenCV中的形态学8_第4张图片
OpenCV中的形态学8_第5张图片
OpenCV中的形态学8_第6张图片

# -*- 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()

OpenCV中的形态学8_第7张图片

# -*- 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()

OpenCV中的形态学8_第8张图片

# -*- 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()

OpenCV中的形态学8_第9张图片

阈值类型

OpenCV中的形态学8_第10张图片

自适应阈值二值化

OpenCV中的形态学8_第11张图片

# -*- 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()


在这里插入图片描述
OpenCV中的形态学8_第12张图片
OpenCV中的形态学8_第13张图片
OpenCV中的形态学8_第14张图片

# -*- 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()

OpenCV腐蚀

OpenCV中的形态学8_第15张图片
OpenCV中的形态学8_第16张图片
OpenCV中的形态学8_第17张图片
OpenCV中的形态学8_第18张图片
OpenCV中的形态学8_第19张图片

# -*- 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()

OpenCV中的形态学8_第20张图片
OpenCV中的形态学8_第21张图片
OpenCV中的形态学8_第22张图片

获取形态学卷积核

OpenCV中的形态学8_第23张图片
OpenCV中的形态学8_第24张图片

# -*- 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()

OpenCV中的形态学8_第25张图片
OpenCV中的形态学8_第26张图片
OpenCV中的形态学8_第27张图片

OpenCV膨胀

原来黑色区域变成白色
OpenCV中的形态学8_第28张图片
卷积核大小意味着膨胀的速度,卷积核越大膨胀越快
OpenCV中的形态学8_第29张图片
OpenCV中的形态学8_第30张图片

# -*- 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()

OpenCV中的形态学8_第31张图片

# -*- 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()

OpenCV中的形态学8_第32张图片

# -*- 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()

OpenCV中的形态学8_第33张图片

开运算

开运算能达到消除噪点的效果
OpenCV中的形态学8_第34张图片
OpenCV中的形态学8_第35张图片
对噪点比较大的图像,我们用大的卷积核,消除噪点能力更强
OpenCV中的形态学8_第36张图片

# -*- 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()

OpenCV中的形态学8_第37张图片
OpenCV中的形态学8_第38张图片

# -*- 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()

OpenCV中的形态学8_第39张图片

闭运算

闭运算消除里面的噪点,开运算消除外面的噪点
OpenCV中的形态学8_第40张图片
OpenCV中的形态学8_第41张图片
OpenCV中的形态学8_第42张图片

# -*- 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()

OpenCV中的形态学8_第43张图片
噪点比较大就要大一点的卷积核
OpenCV中的形态学8_第44张图片

形态学梯度

可以求边缘
OpenCV中的形态学8_第45张图片
OpenCV中的形态学8_第46张图片
OpenCV中的形态学8_第47张图片

# -*- 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()

OpenCV中的形态学8_第48张图片
因为7x7的kernel太大,腐蚀太多,造成里面的黑线很少

现在我们换成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()

OpenCV中的形态学8_第49张图片

顶帽运算

获取的是外面的噪点
OpenCV中的形态学8_第50张图片
OpenCV中的形态学8_第51张图片
OpenCV中的形态学8_第52张图片

# -*- 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()

OpenCV中的形态学8_第53张图片

黑帽操作

获取图内的噪点
OpenCV中的形态学8_第54张图片
OpenCV中的形态学8_第55张图片
OpenCV中的形态学8_第56张图片

# -*- 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()

在这里插入图片描述

小结

OpenCV中的形态学8_第57张图片
OpenCV中的形态学8_第58张图片
OpenCV中的形态学8_第59张图片

之后我会持续更新,如果喜欢我的文章,请记得一键三连哦,点赞关注收藏,你的每一个赞每一份关注每一次收藏都将是我前进路上的无限动力 !!!↖(▔▽▔)↗感谢支持!

你可能感兴趣的:(OpenCV,opencv,人工智能,计算机视觉,经验分享)