使用软件:pycharm,主要涉及到的操作包括开运算、闭运算、梯度运算、礼帽与黑帽运算等操作。
具体运行结果截图如下。
练习涉及到的方法主要是calcHist(images.channels,mask,histSize,ranges)
其中涉及到的参数和含义见下:
具体练习结果截图见下。
滴,彩色图像RGB统计,这张好像是狗狗的嗯:
滴!直方图均衡化对比:
傅里叶变换,是将一个时域非周期的连续信号,转换为一个在频域非周期的连续信号。
这里介绍一下高频和低频的区别,简单来讲高频就类似于变化剧烈的灰度分量,例如边界;低频的话就是变化缓慢的灰度分量,例如一片大海。
傅里叶变换中常见的包含俩种滤波器:
具体练习结果截图如下。
练习涉及到的方法主要是Sobel(src,ddepth,dx,dy,ksize),其中ddepth表示图像的深度,dx和dy分别表示水平和竖直方向,ksize是Sobel算子的大小。练习时分别对图像进行了水平方向和竖直方向的检测,改变算子大小对图像进行对比,还比较了不同算子之间的差异。
具体练习结果截图如下。
另外,针对canny算子进行了更深入的学习。
具体练习结果截图如下。
# -*- coding: utf-8 -*-
# @TIME : 2020/10/12 11:43
# @Author : Chen Shan
# @Email : [email protected]
# @File : imgPreprocessing4.py
# @Software : PyCharm
import cv2 #opencv 读取进来为BGR格式
import matplotlib.pyplot as plt
import numpy as np
# 定义图片显式的方法
def cv_show(name,img):
cv2.imshow(name,img)
cv2.waitKey(0)
cv2.destroyAllWindows()
img = cv2.imread('bluecat.jpg',0)
kernel = np.ones((3,3),np.uint8)
erosion = cv2.erode(img,kernel,iterations = 2)
dilation = cv2.dilate(img,kernel,iterations =1)
res = np.hstack((img,erosion))
res2 = np.hstack((img,dilation))
# cv_show('erosion',res)
# cv_show('dilation',res2)
mydog = cv2.imread('dog.jpg')
# cv_show('mydog',mydog)
kernel = np.ones((5,5),np.uint8)
erode_1 = cv2.erode(mydog,kernel,iterations = 1)
erode_2 = cv2.erode(mydog,kernel,iterations = 2)
erode_3 = cv2.erode(mydog,kernel,iterations = 3)
res3 = np.hstack((erode_1,erode_2,erode_3))
# cv_show('erode',res3)
dilate_1 = cv2.dilate(mydog,kernel,iterations = 1)
dilate_2 = cv2.dilate(mydog,kernel,iterations = 2)
dilate_3 = cv2.dilate(mydog,kernel,iterations = 3)
res4 = np.hstack((dilate_1,dilate_2,dilate_3))
# cv_show('dilate',res4)
# 开运算
opening = cv2.morphologyEx(mydog,cv2.MORPH_OPEN,kernel)
res5 = np.hstack((mydog,opening))
# cv_show('opening',res5)
# 闭运算
closing = cv2.morphologyEx(mydog,cv2.MORPH_CLOSE,kernel)
res6 = np.hstack((mydog,closing))
# cv_show('closing',res6)
# 梯度运算
dilated = cv2.dilate(img,kernel, iterations =1)
erosion = cv2.erode(img,kernel, iterations =1)
res7 = np.hstack((dilated, erosion, dilated-erosion))
# cv_show('Gradient', res7)
gradient = cv2.morphologyEx(img,cv2.MORPH_GRADIENT,kernel)
# cv_show('gradient',gradient)
# 礼帽与黑帽
tophat = cv2.morphologyEx(img, cv2.MORPH_TOPHAT, kernel)
res8 = np.hstack((img, tophat))
# cv_show('tophat', res8)
blackhat = cv2.morphologyEx(img, cv2.MORPH_BLACKHAT, kernel)
res9 = np.hstack((img, blackhat))
# cv_show('blackhat', res9)
# 直方图
gcat = cv2.imread('bluecat.jpg',cv2.IMREAD_GRAYSCALE)
hist = cv2.calcHist([gcat],[0],None,[256],[0,256])
# print(hist.shape)
hist = hist.flatten()
# print(hist.shape)
#print(np.arange(256))
#x = np.array([x for x in range(256)]);
#x = np.arange(256).reshape(256,1)
#x = np.transpose(x)
#print(x.shape)
#plt.plot(hist)
# plt.bar(np.arange(256),hist)
# plt.subplot(2,1,1),plt.imshow(gcat,'gray')
# plt.subplot(2,1,2),plt.hist(gcat.ravel(),256,[0,256]);
cat = cv2.imread('bluecat.jpg')
color = ('b','g','r')
# for i,col in enumerate(color):
# histr = cv2.calcHist([cat],[i],None,[256],[0,256])
# plt.plot(histr,color = col)
# plt.xlim([0,256])
# mask操作
# mask = np.zeros(img.shape[:2], np.uint8)
#
# mask[100:300, 100:400] = 255
# masked_img = cv2.bitwise_and(img,img,mask = mask)
#
# # 计算掩码区域和非掩码区域的直方图
# # 检查作为掩码的第三个参数
# hist_full = cv2.calcHist([img],[0],None,[256],[0,256])
# hist_mask = cv2.calcHist([img],[0],mask,[256],[0,256])
# plt.subplot(221), plt.imshow(img, 'gray')
# plt.subplot(222), plt.imshow(mask,'gray')
# plt.subplot(223), plt.imshow(masked_img, 'gray')
# plt.subplot(224), plt.plot(hist_full), plt.plot(hist_mask)
# plt.xlim([0,256])
# 直方图均衡化
# plt.hist(img.ravel(),256)
equ = cv2.equalizeHist(img)
# plt.hist(equ.ravel(),256)
# plt.show()
#均衡化对比
res10 = np.hstack((img,equ))
# cv_show('res',res10)
# 加上自适应均衡化的对比图
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))
cl1 = clahe.apply(img)
res11 = np.hstack((img,equ,cl1))
# cv_show('res11',res11)
# -*- coding: utf-8 -*-
# @TIME : 2020/10/12 15:49
# @Author : Chen Shan
# @Email : [email protected]
# @File : imgPreprocessing5.py
# @Software : PyCharm
import cv2
import numpy as np
from matplotlib import pyplot as plt
def cv_show(name,img):
cv2.imshow(name,img)
cv2.waitKey(0)
cv2.destroyAllWindows()
img = cv2.imread('bluecat.jpg',0)
img_float32 = np.float32(img)
dft = cv2.dft(img_float32,flags = cv2.DFT_COMPLEX_OUTPUT)
dft_shift = np.fft.fftshift(dft)
# 得到灰度图能表示的形式
magnitude_spectrum = 20*np.log(cv2.magnitude(dft_shift[:,:,0],dft_shift[:,:,1]))
plt.subplot(121),plt.imshow(img, cmap = 'gray')
plt.title('Input Image'), plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(magnitude_spectrum, cmap = 'gray')
plt.title('Magnitude Spectrum'), plt.xticks([]), plt.yticks([])
# plt.show()
rows, cols = img.shape
crow, ccol = np.int32(rows/2) , np.int32(cols/2)
# 首先创建一个掩码,中心正方形为1,其余全为零(低通滤波器)
mask = np.zeros((rows,cols,2),np.uint8)
mask[crow-30:crow+30, ccol-30:ccol+30] = 0
# 应用掩码和逆DFT
fshift = dft_shift*mask
f_ishift = np.fft.ifftshift(fshift)
img_back = cv2.idft(f_ishift)
img_back = cv2.magnitude(img_back[:,:,0],img_back[:,:,1])
plt.subplot(121),plt.imshow(img, cmap = 'gray')
plt.title('Input Image'), plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(img_back, cmap = 'gray')
plt.title('Magnitude Spectrum'), plt.xticks([]), plt.yticks([])
# plt.show()
# 图像梯度与边缘检测
cat = cv2.imread('bluecat.jpg',cv2.IMREAD_GRAYSCALE)
sobelx = cv2.Sobel(cat,cv2.CV_64F,1,0,ksize=3)
sobely= cv2.Sobel(img,cv2.CV_64F,0,1,ksize=3)
# plt.imshow(sobelx,cmap = 'gray')
# plt.imshow(sobely,cmap = 'gray')
# plt.show()
sobelx = cv2.convertScaleAbs(sobelx)
sobely = cv2.convertScaleAbs(sobely)
sobelxy = cv2.addWeighted(sobelx,0.5,sobely,0.5,0)
# cv_show('sobelxy',sobelxy)
sobelxy = cv2.Sobel(img,cv2.CV_64F,1,1,ksize=3)
sobelxy = cv2.convertScaleAbs(sobelxy)
# cv_show('sobelxy2',sobelxy)
# 不同算子的差异
sobelx = cv2.Sobel(img,cv2.CV_64F,1,0,ksize=3)
sobelx = cv2.convertScaleAbs(sobelx)
sobely = cv2.Sobel(img,cv2.CV_64F,0,1,ksize=3)
sobely = cv2.convertScaleAbs(sobely)
sobelxy = cv2.addWeighted(sobelx,0.5,sobely,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)
res = np.hstack((sobelxy, scharrxy, laplacian))
# cv_show('res',res)
# canny算子
v1 = cv2.Canny(img, 80, 150)
v2 = cv2.Canny(img, 50, 100)
res2 = np.hstack((v1,v2))
cv_show('res',res2)
抗拒写作业,不太好,但总归不是热爱的,
这是任务,完成即可
这是本分,做到就行
下次一定,886,ycz老师说下周可能会有一个小的案例了,不知道会不会短暂的改变我的看法,或者说加剧我的逆反心理,越发讨厌了(担心,但会尽量克制住)