OpenCV图像处理基础(变换和去噪)

61 阅读6分钟

原始图像

#将图像转换为灰度图像

show_img_1 = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

plt.imshow(show_img_1,cmap=plt.cm.gray)

#打印图像形状

print(show_img_1.shape)

plt.show()

灰度图像

图像变换


图像变换在深度学习中常用于图像增强。

仿射变换

仿射变换可以进行缩放、旋转、平移、裁剪和翻转。总计而言,仿射变换会保留图中直线的平直性和平行性。表示成矩阵形式:

[ x ^ y ^ 1 ] = [ a 1 a 2 t 1 a 3 a 4 t 2 0 0 1 ] × [ x y 1 ] \begin{bmatrix} \hat x \\ \hat y \\ 1 \end{bmatrix}=\begin{bmatrix} a_1 & a_2 & t_1 \\ a_3 & a_4 & t_2 \\ 0 & 0 & 1 \end{bmatrix} \times \begin{bmatrix} x \\ y \\ 1 \end{bmatrix} ⎣⎡​x^y^​1​⎦⎤​=⎣⎡​a1​a3​0​a2​a4​0​t1​t2​1​⎦⎤​×⎣⎡​xy1​⎦⎤​

其中 [ a 1 a 2 a 3 a 4 ] \begin{bmatrix} a_1 & a_2\\ a_3 & a_4 \end{bmatrix} [a1​a3​​a2​a4​​]表示缩放和旋转变化, [ t 1 t 2 ] \begin{bmatrix} t_1 \\ t_2 \end{bmatrix} [t1​t2​​]表示平移变化。

在OpenCV中可以使用WarpAffine函数进行仿射变换。

wrapAffine(src, M, dsize, flags, borderMode, borderValue)

参数:

src: 源图像

M: 仿射变换矩阵

dsize: 输出图像大小

flags: 插值方式

borderMode: 边界像素模式

borderValue: 边界填充值

同时为了更方便的使用,使用函数getRotationMatrix2D可以方便生成变换矩阵:

getRotationMatrix2D(center, angle, scale)

参数:

center: 旋转中心

angle: 旋转角度

scale: 缩放系数

图像缩放

show_img_3 = cv2.resize(img, (200,200))

plt.imshow(show_img_3)

plt.show()

缩放

rows, cols, _ = img.shape

matrix = cv2.getRotationMatrix2D((cols/2,rows/2),0,0.5)

show_img_4 = cv2.warpAffine(img,matrix,(cols,rows))

plt.imshow(show_img_4)

plt.show()

图片缩放二

图像旋转

matrix = cv2.getRotationMatrix2D((cols/2,rows/2),90,1)

show_img_5 = cv2.warpAffine(img,matrix,(cols,rows))

matrix = cv2.getRotationMatrix2D((cols/2,rows/2),180,1)

show_img_6 = cv2.warpAffine(img,matrix,(cols,rows))

matrix = cv2.getRotationMatrix2D((cols/2,rows/2),270,1)

show_img_7 = cv2.warpAffine(img,matrix,(cols,rows))

plt.subplot(131)

plt.imshow(show_img_5)

plt.subplot(132)

plt.imshow(show_img_6)

plt.subplot(133)

plt.imshow(show_img_7)

plt.show()

图片翻转

图像平移

import numpy as np

matrix = np.float32([[1,0,25],[0,1,25]])

show_img_8 = cv2.warpAffine(img,matrix,(cols,rows))

matrix = np.float32([[1,0,50],[0,1,50]])

show_img_9 = cv2.warpAffine(img,matrix,(cols,rows))

matrix = np.float32([[1,0,100],[0,1,0]])

show_img_10 = cv2.warpAffine(img,matrix,(cols,rows))

plt.subplot(131)

plt.imshow(show_img_8)

plt.subplot(132)

plt.imshow(show_img_9)

plt.subplot(133)

plt.imshow(show_img_10)

plt.show()

图片平移

图像裁剪

#[左上角x轴坐标:右下角x轴坐标,左上角y轴坐标:右下角y轴坐标]

show_img_11 = img[50:150,50:150]

plt.imshow(show_img_11)

plt.show()

图像裁剪

图像翻转

srcPoints = np.float32([[0,0],[0,150],[200,200]])

canvasPoints = np.float32([[0,0],[0,150],[150,150]])

matrix = cv2.getRotationMatrix2D((0,0),0,0.5)

show_img_12 = cv2.warpAffine(img,matrix,(cols,rows))

plt.subplot(121)

plt.imshow(show_img_12)

matrix = cv2.getAffineTransform(np.array(srcPoints),np.array(canvasPoints))

show_img_13 = cv2.warpAffine(img,matrix,(cols,rows))

plt.subplot(122)

plt.imshow(show_img_13)

plt.show()

图片翻转

亮度与对比度变换

通过调整像素值来改变图像亮度和对比度:

f ( x ^ ) = α f ( x ) + β f(\hat x)=\alpha f(x) + \beta f(x^)=αf(x)+β

α \alpha α用于调整对比度, β \beta β用于调整亮度。

#调整亮度

show_img_14 = np.uint8(np.clip((img+20),0,255))

#调整对比度

show_img_15 = np.uint8(np.clip((1.5*img),0,254))

plt.subplot(121)

plt.imshow(show_img_14)

plt.subplot(122)

plt.imshow(show_img_15)

plt.show()

亮度与对比度变换

图像去噪


常见噪声包括高斯噪声和椒盐噪声。

高斯噪声

噪声的概率密度符合高斯分布的图片噪声称为高斯噪声。

def gasuss_noise(image, mean=0, var=0.001):

'''

添加高斯噪声

mean : 均值

var : 方差

'''

image = np.array(image/255, dtype=float)

noise = np.random.normal(mean, var ** 0.5, image.shape)

out = image + noise

if out.min() < 0:

low_clip = -1.

else:

low_clip = 0.

out = np.clip(out, low_clip, 1.0)

out = np.uint8(out*255)

#cv.imshow("gasuss", out)

return out

show_img_16 = gasuss_noise(img,var=0.01)

show_img_17 = gasuss_noise(img,var=0.04)

plt.subplot(121)

plt.imshow(show_img_16)

plt.subplot(122)

plt.imshow(show_img_17)

plt.show()

高斯噪声

椒盐噪声

椒盐噪声是指图片中包含白色的盐噪声和黑色的胡椒噪声。

def sp_noise(image,prob):

'''

添加椒盐噪声

prob:噪声比例

'''

output = np.zeros(image.shape,np.uint8)

thres = 1 - prob

for i in range(image.shape[0]):

for j in range(image.shape[1]):

rdn = random.random()

if rdn < prob:

output[i][j] = 0

elif rdn > thres:

output[i][j] = 255

else:

output[i][j] = image[i][j]

return output

show_img_18 = sp_noise(img,0.01)

show_img_19 = sp_noise(img,0.05)

plt.subplot(121)

plt.imshow(show_img_18)

plt.subplot(122)

plt.imshow(show_img_19)

plt.show()

椒盐噪声

中值和均值滤波

中值滤波是使用邻域内所有像素的中位数替换中心像素,可以有效去除图像中的高斯噪声。而均值是使用邻域内所有像素的平均值替换中心像素。

show_img_20 = gasuss_noise(img,var=0.01) 学好 Python 不论是就业还是做副业赚钱都不错,但要学会 Python 还是要有一个学习规划。最后大家分享一份全套的 Python 学习资料,给那些想学习 Python 的小伙伴们一点帮助!

一、Python所有方向的学习路线

Python所有方向路线就是把Python常用的技术点做整理,形成各个领域的知识点汇总,它的用处就在于,你可以按照上面的知识点去找对应的学习资源,保证自己学得较为全面。

二、学习软件

工欲善其事必先利其器。学习Python常用的开发软件都在这里了,给大家节省了很多时间。

三、全套PDF电子书

书籍的好处就在于权威和体系健全,刚开始学习的时候你可以只看视频或者听某个人讲课,但等你学完之后,你觉得你掌握了,这时候建议还是得去看一下书籍,看权威技术书籍也是每个程序员必经之路。

四、入门学习视频

我们在看视频学习的时候,不能光动眼动脑不动手,比较科学的学习方法是在理解之后运用它们,这时候练手项目就很适合了。

五、实战案例

光学理论是没用的,要学会跟着一起敲,要动手实操,才能将自己的所学运用到实际当中去,这时候可以搞点实战案例来学习。

六、面试资料

我们学习Python必然是为了找到高薪的工作,下面这些面试题是来自阿里、腾讯、字节等一线互联网大厂最新的面试资料,并且有阿里大佬给出了权威的解答,刷完这一套面试资料相信大家都能找到满意的工作。

了解详情:docs.qq.com/doc/DSnl3ZG…