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

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

  1. 图像缩放
    图像缩放由 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-8
import cv2
import numpy as np
import 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-8
import cv2
import numpy as np
import 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-8
import cv2
import numpy as np
import 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-8
import cv2
import numpy as np
import 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-8
import cv2
import numpy as np
import 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博客 https://blog.csdn.net/Eastmount/article/details/82454335

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值