全文预览
熟悉图像的几何变换、仿射变换和透视变换
使用软件:pycharm,主要涉及到的图像操作包括改变RGB通道、调整图像大小、图像平移、图像旋转、图像仿射变换和透视变换等操作。
具体运行结果截图如下。
熟悉图像的平滑处理
涉及到的基本图像平滑处理包括图像的过滤(2D卷积)、图像模糊和高斯模糊等操作。
具体运行结果部分截图如下(从左到右依次是原图、图像模糊、高斯模糊和图像过滤)。
阈值处理
在图像处理中阈值化操作,从一副图像中利用阈值分割出我们需要的物体部分(当然这里的物体可以是一部分或者整体)。这样的图像分割方法是基于图像中物体与背景之间的灰度差异,而且此分割属于像素级的分割。
阈值化操作在图像处理中是一种常用的算法,比如图像的二值化就是一种最常见的一种阈值化操作。 这里使用cv.threshold()阈值处理方法和cv.adaptiveThreshold()自适应阈值方法来进行一定的图像阈值处理。
具体运行结果截图如下。
cv.threshold()阈值处理方法
cv.adaptiveThreshold()自适应阈值方法
练习代码(全)
imgPreprocessing.py
# -*- coding: utf-8 -*-
# @TIME : 2020/9/28 15:15
# @Author : Chen Shan
# @Email : [email protected]
# @File : imgPreprocessing.py
# @Software : PyCharm
import cv2
import matplotlib.pyplot as plt
import numpy as np
def cv_show(name,img):
cv2.imshow(name,img)
cv2.waitKey(0)
cv2.destroyAllWindows()
# img_np = np.fromfile('mycat.png',dtype = np.uint8)
# img = cv2.imdecode(img_np,-1)
img = cv2.imread('mycat.png')
cv_show('原图',img)
# OpenCV读取的图片从BGR转换为RGB
b, g, r =cv2.split(img)
img2 = cv2.merge([r,g,b])
cv_show('RGB',img2)
res = cv2.resize(img,None,fx=2, fy=2, interpolation = cv2.INTER_CUBIC)
height, width = img.shape[:2]
res = cv2.resize(img,(2*width, 2*height), interpolation = cv2.INTER_CUBIC)
cv_show('缩放',res)
# print(img.shape)
rows,cols,rgb = img.shape
M = np.float32([[1,0,100],[0,1,50]])
dst = cv2.warpAffine(img,M,(cols,rows))
cv_show('平移',dst)
rows,cols,rgb = img.shape
# # cols-1 和 rows-1 是坐标限制
M = cv2.getRotationMatrix2D(((cols-1)/2.0,(rows-1)/2.0),45,1)
dst = cv2.warpAffine(img,M,(cols,rows))
cv_show('旋转',dst)
# 仿射变换
rows,cols,ch = img.shape
pts1 = np.float32([[50,50],[200,50],[50,200]])
pts2 = np.float32([[10,100],[200,50],[100,250]])
M = cv2.getAffineTransform(pts1,pts2)
dst = cv2.warpAffine(img,M,(cols,rows))
plt.subplot(121),plt.imshow(img),plt.title('Input')
plt.subplot(122),plt.imshow(dst),plt.title('Output')
plt.show()
# 透视变换
rows,cols,ch = img.shape
pts1 = np.float32([[56,65],[368,52],[28,387],[389,390]])
pts2 = np.float32([[0,0],[300,0],[0,300],[300,300]])
M = cv2.getPerspectiveTransform(pts1,pts2)
dst = cv2.warpPerspective(img,M,(300,300))
plt.subplot(121),plt.imshow(img),plt.title('Input')
plt.subplot(122),plt.imshow(dst),plt.title('Output')
plt.show()
imgPreprocessing2.py
# -*- coding: utf-8 -*-
# @TIME : 2020/9/28 17:23
# @Author : Chen Shan
# @Email : [email protected]
# @File : imgPreprocessing2.py
# @Software : PyCharm
import cv2
import matplotlib.pyplot as plt
import numpy as np
img = cv2.imread('mydog.jpg')
# 2d卷积
# kernel = np.ones((5,5),np.float32)/25
# dst = cv2.filter2D(img,-1,kernel)
#
# plt.subplot(121),plt.imshow(img),plt.title('Original')
# plt.xticks([]), plt.yticks([])
#
# plt.subplot(122),plt.imshow(dst),plt.title('Averaging')
# plt.xticks([]), plt.yticks([])
# plt.show()
# 图像模糊
# blur = cv2.blur(img,(5,5))
#
# plt.subplot(121),plt.imshow(img),plt.title('Original')
# plt.xticks([]), plt.yticks([])
#
# plt.subplot(122),plt.imshow(blur),plt.title('Blurred')
# plt.xticks([]), plt.yticks([])
# plt.show()
# 高斯模糊
# blur = cv2.GaussianBlur(img,(5,5),0)
#
# plt.subplot(121),plt.imshow(img),plt.title('Original')
# plt.xticks([]), plt.yticks([])
#
# plt.subplot(122),plt.imshow(blur),plt.title('Gaussian')
# plt.xticks([]), plt.yticks([])
# plt.show()
imgPreprocessing3.py
# -*- coding: utf-8 -*-
# @TIME : 2020/9/28 17:51
# @Author : Chen Shan
# @Email : [email protected]
# @File : imgPreprocessing3.py
# @Software : PyCharm
import cv2
import matplotlib.pyplot as plt
img = cv2.imread('mydog.jpg')
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#plt.imshow(img_gray)
ret,thresh1 = cv2.threshold(img_gray,127,255,cv2.THRESH_BINARY)
ret,thresh2 = cv2.threshold(img_gray,127,255,cv2.THRESH_BINARY_INV)
ret,thresh3 = cv2.threshold(img_gray,127,255,cv2.THRESH_TRUNC)
ret,thresh4 = cv2.threshold(img_gray,127,255,cv2.THRESH_TOZERO)
ret,thresh5 = cv2.threshold(img_gray,127,255,cv2.THRESH_TOZERO_INV)
titles = ['original','Binary','Binary_INV','TRUNC','TOZERO','TOZERO_INV']
images = [img_gray, thresh1, thresh2, thresh3, thresh4, thresh5]
for i in range(6):
#print(i)
plt.subplot(2,3,i+1),plt.imshow(images[i],'gray')
plt.title(titles[i])
plt.xticks([]),plt.yticks([])
plt.show()
# 自适应阈值
img_gray = cv2.medianBlur(img_gray,5) #均值滤波
ret,th1 = cv2.threshold(img_gray,127,255,cv2.THRESH_BINARY)
th2 = cv2.adaptiveThreshold(img_gray,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,11,2)
th3 = cv2.adaptiveThreshold(img_gray,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,11,2)
titles = ['Original Image', 'Global Thresholding (v = 127)',
'Adaptive Mean Thresholding', 'Adaptive Gaussian Thresholding']
images = [img_gray, th1, th2, th3]
for i in range(4):
plt.subplot(2,2,i+1),plt.imshow(images[i],'gray')
plt.title(titles[i])
plt.xticks([]),plt.yticks([])
plt.show()
唧唧歪歪
开始上课睡觉模式,这样不好不好,去网络上重新找了一张史努比的照片,毕竟不能偏爱猫猫,你说是不,不过好像呢,显示的有点问题(说实在的没什么违和感,老师应该也不会在意的吧,我也就没改(哈哈哈哈哈好吧就是懒)效果还很很明显的呢
886,继续加油