图像的腐蚀膨胀及图像大小保存处理
import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt
ime = "D:/.bmp"
nume = "E:/.png"
img = cv.imread(ime)
kernel = np.ones((5, 5), np.uint8)
erosion = cv.erode(img, kernel)
fig,axes=plt.subplots(nrows=1,ncols=1,figsize=(10,10),dpi=100)
plt.imshow(erosion)
plt.xticks([])
plt.yticks([])
plt.savefig(nume,bbox_inches='tight',dpi=300,pad_inches=0.0)
plt.show()
边缘检测
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
img = cv.imread("./15.bmp",0)
x = cv.Sobel(img, cv.CV_16S, 1, 0, ksize = -1)
y = cv.Sobel(img, cv.CV_16S, 0, 1, ksize = -1)
Scale_absX = cv.convertScaleAbs(x)
Scale_absY = cv.convertScaleAbs(y)
result = cv.addWeighted(Scale_absX, 0.5, Scale_absY, 0.5, 0)
plt.figure(figsize=(10,8),dpi=100)
plt.subplot(121),plt.imshow(img,cmap=plt.cm.gray),plt.title('原图')
plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(result,cmap = plt.cm.gray),plt.title('Sobel滤波后结果')
plt.xticks([]), plt.yticks([])
plt.show()
计算sobel算子的部分中将ksize设为-1,就是利用Scharr进行边缘检测。
x = cv.Sobel(img, cv.CV_16S, 1, 0, ksize = -1)
y = cv.Sobel(img, cv.CV_16S, 0, 1, ksize = -1)
实现Canny检测
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
img = cv.imread('./15.bmp',0)
lowThreshold = 0
max_lowThreshold = 100
canny = cv.Canny(img, lowThreshold, max_lowThreshold)
plt.figure(figsize=(10,8),dpi=100)
plt.subplot(121),plt.imshow(img,cmap=plt.cm.gray),plt.title('原图')
plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(canny,cmap = plt.cm.gray),plt.title('Canny检测后结果')
plt.xticks([]), plt.yticks([])
plt.show()