zoukankan      html  css  js  c++  java
  • cv2对图像进行旋转和放缩变换

    旋转:

    def get_image_rotation(image):
        #通用写法,即使传入的是三通道图片依然不会出错
        height, width = image.shape[:2]
        center = (width // 2, height // 2)
        rotation = random.randint(-20,20)
    
        #得到旋转矩阵,第一个参数为旋转中心,第二个参数为旋转角度,第三个参数为旋转之前原图像缩放比例
        M = cv2.getRotationMatrix2D(center, -rotation, 0.7)
        #进行仿射变换,第一个参数图像,第二个参数是旋转矩阵,第三个参数是变换之后的图像大小
        image_rotation = cv2.warpAffine(image, M, (width, height))
        return image_rotation

    缩放:

    #得到缩放后的图片
    def get_image_scale(image):
        height, width = image.shape[:2]
    
        width_random = random.randint(-3, 3)
        width_scale = 1.0 + width_random / 10.0
        height_random=random.randint(1,6)
        height_scale=1.0+height_random/10.0
    
        #print("width_scale:%f,height_scale:%f" %(width_scale,height_scale))
    
        image_resize = cv2.resize(image, (int(width_scale * width), int(height)), interpolation=cv2.INTER_CUBIC)
        height_resize,width_resize=image_resize.shape
        #print('image_binary height: %d width :%d' %(height,width))
        #print('image_scale height: %d  %d' %(height_resize,width_resize))
        return image_resize
  • 相关阅读:
    3、面向对象
    今日记事
    javascript 学习javascript高级程序设计
    数据库操作语言分类
    project遇到问题了。
    CentOS设置程序开机自启动的方法
    ECMAScript 基础 [完]
    CentOS 网络设置修改
    Spring Boot项目Circular view path问题解决
    数据库设计
  • 原文地址:https://www.cnblogs.com/shixisheng/p/9448901.html
Copyright © 2011-2022 走看看