Python+OpenCV 图像处理系列(9)—— 图像的翻转和缩放插值

1. 图像的翻转

图像翻转 (Image Flip),图像翻转的本质像素映射,OpenCV 支持三种图像翻转方式:

  • X轴翻转,flipcode = 0
  • Y轴翻转, flipcode = 1
  • XY轴翻转, flipcode = -1

cv2.flip(src, flipcode) 携带两个参数,第一个是图像在内存中的映射,第二个是翻转码。

import cv2
import numpy as np

src = cv2.imread("./img/003.jpg")
cv2.imshow("origin", src)

dst1 = cv2.flip(src, 0)
cv2.imshow("x-flip", dst1)

dst2 = cv2.flip(src, 1)
cv2.imshow("y-flip", dst2)

dst3 = cv2.flip(src, -1)
cv2.imshow("xy-flip", dst3)

h, w, ch = src.shape
dst = np.zeros(src.shape, src.dtype)
for row in range(h):
    for col in range(w):
        b, g, r = src[row, col]
        dst[row, w - col - 1] = [b, g, r]
cv2.imshow("custom-y-flip", dst)

cv2.waitKey(0)
cv2.destroyAllWindows()

效果图
原始图片
翻转之后的图片

2. 图像的插值

图像插值(Image Interpolation), 最常见四种插值算法

  • INTER_NEAREST = 0
  • INTER_LINEAR = 1
  • INTER_CUBIC = 2
  • INTER_LANCZOS4 = 4

相关的应用场景有几何变换、透视变换、插值计算新像素,resize, 如果 size 有值,使用 size 做放缩插值,否则根据 fx 与 fy 卷积。


import cv2

src = cv2.imread("./img/003.jpg")
cv2.imshow("origin", src)

h, w = src.shape[:2]
print(h, w)
dst = cv2.resize(src, (w*2, h*2), fx=0.75, fy=0.75, interpolation=cv2.INTER_NEAREST)
cv2.imshow("INTER_NEAREST", dst)

dst = cv2.resize(src, (w*2, h*2), interpolation=cv2.INTER_LINEAR)
cv2.imshow("INTER_LINEAR", dst)

dst = cv2.resize(src, (w*2, h*2), interpolation=cv2.INTER_CUBIC)
cv2.imshow("INTER_CUBIC", dst)

dst = cv2.resize(src, (w*2, h*2), interpolation=cv2.INTER_LANCZOS4)
cv2.imshow("INTER_LANCZOS4", dst)

cv2.waitKey(0)
cv2.destroyAllWindows()

效果图

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值