zoukankan      html  css  js  c++  java
  • 【python】opencv教程CV2模块——图片处理,剪切缩放旋转

    opencv教程CV2模块——图片处理,剪切缩放旋转

    import cv2
    import numpy as np
    
    # 读取照片
    img = cv2.imread('./imgg/1.jpeg')
    
    # 沿着横纵轴放大1.6倍,然后平移(-150,-240),最后沿原图大小截取,等效于裁剪并放大
    M_crop_pic = np.array([
        [1.6, 0, -150],
        [0, 1.6, -240]
    ], dtype=np.float32)
    
    img_pic = cv2.warpAffine(img, M_crop_pic, (960, 540))
    cv2.imwrite('./out/lanka_pic.jpg', img_pic)
    
    # x轴的剪切变换,角度15°
    theta = 15 * np.pi / 180
    M_shear = np.array([
        [1, np.tan(theta), 0],
        [0, 1, 0]
    ], dtype=np.float32)
    
    img_sheared = cv2.warpAffine(img, M_shear, (400, 600))
    cv2.imwrite('./out/lanka_safari_sheared.jpg', img_sheared)
    
    # 顺时针旋转,角度15°
    M_rotate = np.array([
        [np.cos(theta), -np.sin(theta), 0],
        [np.sin(theta), np.cos(theta), 0]
    ], dtype=np.float32)
    
    img_rotated = cv2.warpAffine(img, M_rotate, (400, 600))
    cv2.imwrite('./out/lanka_safari_rotated.jpg', img_rotated)
    
    # 某种变换,具体旋转+缩放+旋转组合可以通过SVD分解理解
    M = np.array([
        [1, 1.5, -400],
        [0.5, 2, -100]
    ], dtype=np.float32)
    
    img_transformed = cv2.warpAffine(img, M, (400, 600))
    cv2.imwrite('./out/lanka_safari_transformed.jpg', img_transformed)
    
  • 相关阅读:
    《梦断代码》随笔第1篇
    四则运算3
    1、软件工程结对开发之求一维数组中连续最大子数组之和
    四则运算2单元测试
    《梦断代码》随笔第0篇
    四则运算2完整版
    四则运算2设计思想
    软件工程第一个程序
    软件工程阅读计划
    电梯调度之需求分析
  • 原文地址:https://www.cnblogs.com/helenlee01/p/12707294.html
Copyright © 2011-2022 走看看