Python之OpenCV 004 简单的旋转,翻转

Halcon中实现旋转和翻转(镜像)的函数分别为

rotate_image

mirror_image

在OpenCV也可以容易实现这个功能。

代码如下:

"""
PyQt AND OpenCV
By LiNYoUBiAo
2020/3/31 19:21
"""
import numpy as np
import cv2 as cv
import math

if __name__ == "__main__":
    imageSrc = cv.imread("c:/pic/lena.jpg", cv.IMREAD_COLOR)
    if imageSrc is None:
        print("Read image Error")
        exit(0)
    # 水平翻转
    imageFlip1 = cv.flip(imageSrc, 0)
    # 垂直翻转
    imageFlip2 = cv.flip(imageSrc, 1)
    # 转换坐标
    sinLen = math.sin(45)
    tanLen = math.atan(45)
    aLen = math.sqrt(0.5**2*2)-0.5
    srcTri = np.array([[0, 0], [0, 1], [1, 1]]).astype(np.float32)
    # dstTri = np.array([[0, 0], [sinLen, sinLen], [tanLen, 0]]).astype(np.float32)
    # dstTri = np.array([[0, 0], [-sinLen, sinLen], [0, tanLen]]).astype(np.float32)
    dstTri = np.array([[0.5, -aLen], [-aLen, 0.5], [0.5, 1+aLen]]).astype(np.float32)
    # 计算转换矩阵
    warp_mat = cv.getAffineTransform(srcTri, dstTri)
    warp_dst = cv.warpAffine(imageSrc, warp_mat, (imageSrc.shape[1], imageSrc.shape[0]))
    rot_mat = cv.getRotationMatrix2D((imageSrc.shape[1]//2, imageSrc.shape[0]//2), -45, 1)
    rot_dst = cv.warpAffine(imageSrc, rot_mat, (imageSrc.shape[1], imageSrc.shape[0]))
    cv.imshow("imageSrc", imageSrc)
    cv.imshow("imageFlip1", imageFlip1)
    cv.imshow("imageFlip2", imageFlip2)
    cv.imshow("warp_dst", warp_dst)
    cv.imshow("rot_dst", rot_dst)
    cv.waitKey(0)
    cv.destroyAllWindows()

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值