1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > python图像处理第七课--图像缩放 翻转 旋转 平移--小白的成长历程

python图像处理第七课--图像缩放 翻转 旋转 平移--小白的成长历程

时间:2019-12-01 15:53:47

相关推荐

python图像处理第七课--图像缩放 翻转 旋转 平移--小白的成长历程

#目的为记录在自己运行时存在的问题及解决方法,本文基于CSDN社区的Eastmount大佬的课程,通过学习,其中也增加了自己的考量和问题的解决。

图像缩放

图像缩放由 resize() 函数实现,如下所示,其中src表示原图,(1)中,括号内数据代表缩放后图片尺寸;(2)中fx和fy分别代表长和高方向的缩放比例。

(1)result = cv2.resize(src, (160,160))

(2)result = cv2.resize(src, None, fx=0.5, fy=0.5)

tip:代码result = cv2.resize(img, (512, 384))中,列数为512,行数为384。

# encoding:utf-8import cv2import numpy as npimport matplotlib.pyplot as plt# 读取图片img = cv2.imread("C:/Users/CLH/Desktop/test1.JPG")print(img.shape)#图像缩放result = cv2.resize(img, (512,384))print(result.shape)# 显示图像cv2.imshow("img",img)cv2.imshow("result", result)# 等待显示cv2.waitKey(0)cv2.destroyAllWindows()

结果如下:

(768, 1024, 3)

(384, 512, 3)

tip:其中fx设置的为长度(列数),fy设置的为高度(行数)

# encoding:utf-8import cv2import numpy as npimport matplotlib.pyplot as plt# 读取图片img = cv2.imread("C:/Users/CLH/Desktop/test1.JPG")print(img.shape)#图像缩放result = cv2.resize(img, None, fx=0.5, fy=0.2)print(result.shape)# 显示图像cv2.imshow("img",img)cv2.imshow("result", result)# 等待显示cv2.waitKey(0)cv2.destroyAllWindows()

结果如下:

(768, 1024, 3)

(154, 512, 3)

2. 图像旋转

图像旋转主要调用getRotationMatrix2D() 函数和warpAffine() 函数实现,绕图像的中心旋转,具体如下:

#参数分别为:旋转中心、旋转度数、比例尺

M = cv2.getRotationMatrix2D((cols/2, rows/2), 30, 1)

#参数分别为:原始图像、定义好的旋转矩阵M、原始图像列、行

rotated = cv2.warpAffine(src, M, (cols, rows))

# encoding:utf-8import cv2import numpy as npimport matplotlib.pyplot as plt# 读取图片img = cv2.imread("C:/Users/CLH/Desktop/test1.JPG")rows, cols, channel = img.shape#绕图像中心旋转#参数:旋转中心,旋转度数,比例尺M = cv2.getRotationMatrix2D((cols/2, rows/2), 60, 1)#参数:原始图像,旋转参数,原始图像宽高rotated = cv2.warpAffine(img, M, (cols, rows))# 显示图像cv2.imshow("rotated", rotated)# 等待显示cv2.waitKey(0)cv2.destroyAllWindows()

3. 图像翻转

图像翻转在OpenCV中调用函数flip() 实现,函数如下:

dst = cv2.flip(src, flipCode)

其中src表示原始图像,flipCode表示翻转方向,如果flipCode为0,则以X轴为对称轴翻转,如果fliipCode>0,则以Y轴为对称轴翻转,如果flipCode<0,则在X轴、Y轴方向同时翻转。

# encoding:utf-8import cv2import numpy as npimport matplotlib.pyplot as plt# 读取图片img = cv2.imread("C:/Users/CLH/Desktop/test1.JPG")src = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)#图像翻转#0,以X轴为对称轴翻转 >0,以Y轴为对称轴翻转 <0,以X、Y轴翻转img1 = cv2.flip(src, 0)img2 = cv2.flip(src, 1)img3 = cv2.flip(src, -1)#显示图形titles = ['Source', 'Image1', 'Image2', 'Image3']images = [src, img1, img2, img3]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()

4. 图像平移

图像平移首先定义平移矩阵M,再调用warpAffine()函数实现平移,核心函数如下:

M = np.float32([[1, 0, x], [0, 1, y]])shifted = cv2.warpAffine(image, M, (image.shape[1], image.shape[0]))

#平移矩阵M中,[1,0,x]表示在X轴方向上平移,平移量为x,[0,1,y]表示在Y轴方向上平移,平移量为y。

#值得注意的是,在Y轴方向上,其坐标为从上向下排列,故y=100时,代表图片下移100距离。

代码如下:

# encoding:utf-8import cv2import numpy as npimport matplotlib.pyplot as plt# 读取图片img = cv2.imread("C:/Users/CLH/Desktop/test1.JPG")image = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)#图像平移 下、上、右、左平移M = np.float32([[1, 0, 0], [0, 1, 100]])img1 = cv2.warpAffine(image, M, (image.shape[1], image.shape[0]))M = np.float32([[1, 0, 0], [0, 1, -100]])img2 = cv2.warpAffine(image, M, (image.shape[1], image.shape[0]))M = np.float32([[1, 0, 100], [0, 1, 0]])img3 = cv2.warpAffine(image, M, (image.shape[1], image.shape[0]))M = np.float32([[1, 0, -100], [0, 1, 0]])img4 = cv2.warpAffine(image, M, (image.shape[1], image.shape[0]))#显示图形titles = [ 'Image1', 'Image2', 'Image3', 'Image4']images = [img1, img2, img3, img4]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()

参考:

[Python图像处理] 六.图像缩放、图像旋转、图像翻转与图像平移_杨秀璋的专栏-CSDN博客 /Eastmount/article/details/82454335

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。