opencv+python(二)

形态学操作篇

 

八、腐蚀、膨胀、开运算、闭运算

# 腐蚀操作(白变小,黑吃白)
import numpy as np
img = cv2.imread('erosion1.png')
kernel = np.ones((3,3), np.uint8)
erosion = cv2.erode(img, kernel, iterations = 1)    # 先腐蚀
kernel = np.ones((5,5), np.uint8)
dilate = cv2.dilate(erosion, kernel, iterations = 1)   # 再膨胀
res = np.hstack((img,dilate))
res = np.hstack((img,erosion,dilate))
cv2.imshow('0', res)
cv2.waitKey(0)
cv2.destroyAllWindows()
# 膨胀操作(白变大)
import numpy as np
img = cv2.imread('erosion1.png')
kernel = np.ones((4,4), np.uint8)
dilate = cv2.dilate(img, kernel, iterations = 3)
res = np.hstack((img,dilate))
cv2.imshow('0', res)
cv2.waitKey(0)
cv2.destroyAllWindows()
# 开运算:先腐蚀,再膨胀
import numpy as np
img = cv2.imread('erosion1.png')
kernel = np.ones((3,3), np.uint8)    # 卷积核一般为奇数
opening = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)
res = np.hstack((img,opening))
cv2.imshow('0', res)
cv2.waitKey(0)
cv2.destroyAllWindows()
# 闭运算:先膨胀,再腐蚀
import numpy as np
img = cv2.imread('erosion1.png')
kernel = np.ones((5,5), np.uint8)
closeing = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)
res = np.hstack((img,closeing))
cv2.imshow('0', res)
cv2.waitKey(0)
cv2.destroyAllWindows()

九、梯度运算

# 梯度运算
import numpy as np
img = cv2.imread('ellipse.png')
kernel = np.ones((5,5), np.uint8)
dilate = cv2.dilate(img, kernel, iterations = 3)
erosion = cv2.erode(img, kernel, iterations = 3)
res = np.hstack((img,dilate, erosion))
cv2.imshow('1', res)
# 梯度=膨胀-腐蚀
img1 = dilate - erosion
# 梯度函数
kernel1 = np.ones((5,5), np.uint8)
gradient = cv2.morphologyEx(img, cv2.MORPH_GRADIENT, kernel1)
res1 = np.hstack((img1, gradient))   # (两者有些差异)
cv2.imshow('2', res1)
cv2.waitKey(0)
cv2.destroyAllWindows()

十、顶帽与黑帽

# 顶帽 = 原始输入-开运算结果
import numpy as np
img = cv2.imread('ellipse.png')
kernel = np.ones((5,5), np.uint8)
tophat = cv2.morphologyEx(img, cv2.MORPH_TOPHAT, kernel)
opening = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)
cv2.imshow('11',opening)
img1 = img - opening
res = np.hstack((tophat, img1))
cv2.imshow('1', res)
# 黑帽 = 闭运算结果-原始输入
import numpy as np
img = cv2.imread('ellipse.png')
kernel = np.ones((5,5), np.uint8)
blackhat = cv2.morphologyEx(img, cv2.MORPH_BLACKHAT, kernel)
closeing = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)
img2 = closeing - img
res2 = np.hstack((blackhat, img2))
cv2.imshow('22',closeing)
cv2.imshow('2', res2)
cv2.waitKey(0)
cv2.destroyAllWindows()

梯度计算方法篇

十一、Sobel算子

opencv+python(二)_第1张图片

# 梯度计算方法
import numpy as np
img = cv2.imread('cat.jpg')
# img = cv2.imread('cat.jpg',cv2.IMREAD_GRAYSCALE)
img = cv2.resize(img, (300,300))
# Sobel算子  cv2.CV_64F的作用是保留负数形式
# dst=cv2.Sobel(src,ddepth,dx,dy,ksize)   ddepth:图像深度  dx和dy分别表示竖直和水平方向   ksize是Sobel算子的大小
sobelx = cv2.Sobel(img,cv2.CV_64F, 1, 0, ksize=3)  # 水平方向(右减左):白到黑是正数,黑到白是负数,所有负数会被截断成0
sobelx1 = cv2.convertScaleAbs(sobelx)   # 这一步作用是将保留负数的绝对值
sobely = cv2.Sobel(img,cv2.CV_64F,0,1,ksize = 3)
sobely1 = cv2.convertScaleAbs(sobely)
# 第一种方法:直接相加
res = sobelx1 + sobely1
# 第二种方法:用cv库中函数
sobelxy = cv2.addWeighted(sobelx1, 0.5, sobely1, 0.5, 0)
# 第三种方法:将dx,dy都设为1
sobelxy1 = cv2.Sobel(img, cv2.CV_64F,1,1,ksize = 3)
sobelxy1 = cv2.convertScaleAbs(sobelxy1)
# 复杂图像第二种效果最好,纯色黑白简单图像第一种效果最好
str = np.hstack((res, sobelxy, sobelxy1))
cv2.imshow('4', str)
cv2.waitKey(0)
cv2.destroyAllWindows()

十二、Scharr算子

opencv+python(二)_第2张图片

# Scharr算子
import numpy as np
img = cv2.imread('cat.jpg')
# img = cv2.imread('cat.jpg',cv2.IMREAD_GRAYSCALE)
img = cv2.resize(img, (300,300))
scharrx = cv2.Scharr(img, cv2.CV_64F, 1, 0)
scharry = cv2.Scharr(img, cv2.CV_64F, 0, 1)
scharrx = cv2.convertScaleAbs(scharrx)
scharry = cv2.convertScaleAbs(scharry)
scharrxy = cv2.addWeighted(scharrx, 0.5, scharry, 0.5, 0)
cv2.imshow('1', scharrxy)
cv2.waitKey(0)
cv2.destroyAllWindows()

 十三、Laplacian算子

opencv+python(二)_第3张图片

# Laplacian算子
import numpy as np
img = cv2.imread('cat.jpg')
# img = cv2.imread('cat.jpg',cv2.IMREAD_GRAYSCALE)
img = cv2.resize(img, (300,300))
laplacian = cv2.Laplacian(img, cv2.CV_64F)
laplacian = cv2.convertScaleAbs(laplacian)
cv2.imshow('4', laplacian)
cv2.waitKey(0)
cv2.destroyAllWindows()

三种不同算子的对比

# 三种不同算子的对比
import numpy as np
# img = cv2.imread('cat.jpg')
img = cv2.imread('car.jpg',cv2.IMREAD_GRAYSCALE)
img = cv2.resize(img, (300,300))
cv2.imshow('0', img)

sobelx = cv2.Sobel(img,cv2.CV_64F, 1, 0, ksize=3)  # 水平方向(右减左):白到黑是正数,黑到白是负数,所有负数会被截断成0
sobelx1 = cv2.convertScaleAbs(sobelx)   # 这一步作用是将保留负数的绝对值
sobely = cv2.Sobel(img,cv2.CV_64F,0,1,ksize = 3)
sobely1 = cv2.convertScaleAbs(sobely)
sobelxy = cv2.addWeighted(sobelx1, 0.5, sobely1, 0.5, 0)

scharrx = cv2.Scharr(img, cv2.CV_64F, 1, 0)
scharry = cv2.Scharr(img, cv2.CV_64F, 0, 1)
scharrx = cv2.convertScaleAbs(scharrx)
scharry = cv2.convertScaleAbs(scharry)
scharrxy = cv2.addWeighted(scharrx, 0.5, scharry, 0.5, 0)

laplacian = cv2.Laplacian(img, cv2.CV_64F)
laplacian = cv2.convertScaleAbs(laplacian)

str = np.hstack((sobelxy, scharrxy, laplacian))
cv2.imshow('1', str)
cv2.waitKey(0)
cv2.destroyAllWindows()

效果图如下:

你可能感兴趣的:(python,numpy,opencv)