Python+OpenCV+图片旋转并用原底色填充新四角的例子


Posted in Python onDecember 12, 2019

我就废话不多说了,直接上代码吧!

import cv2
from math import fabs, sin, cos, radians
import numpy as np
from scipy.stats import mode


def get_img_rot_broa(img, degree=45, filled_color=-1):
 """
 Desciption:
  Get img rotated a certain degree,
 and use some color to fill 4 corners of the new img.
 """

 # 获取旋转后4角的填充色
 if filled_color == -1:
 filled_color = mode([img[0, 0], img[0, -1],
    img[-1, 0], img[-1, -1]]).mode[0]
 if np.array(filled_color).shape[0] == 2:
 if isinstance(filled_color, int):
  filled_color = (filled_color, filled_color, filled_color)
 else:
 filled_color = tuple([int(i) for i in filled_color])

 height, width = img.shape[:2]

 # 旋转后的尺寸
 height_new = int(width * fabs(sin(radians(degree))) +
   height * fabs(cos(radians(degree))))
 width_new = int(height * fabs(sin(radians(degree))) +
   width * fabs(cos(radians(degree))))

 mat_rotation = cv2.getRotationMatrix2D((width / 2, height / 2), degree, 1)

 mat_rotation[0, 2] += (width_new - width) / 2
 mat_rotation[1, 2] += (height_new - height) / 2

 # Pay attention to the type of elements of filler_color, which should be
 # the int in pure python, instead of those in numpy.
 img_rotated = cv2.warpAffine(img, mat_rotation, (width_new, height_new),
     borderValue=filled_color)
 # 填充四个角
 mask = np.zeros((height_new + 2, width_new + 2), np.uint8)
 mask[:] = 0
 seed_points = [(0, 0), (0, height_new - 1), (width_new - 1, 0),
   (width_new - 1, height_new - 1)]
 for i in seed_points:
 cv2.floodFill(img_rotated, mask, i, filled_color)

 return img_rotated

以上这篇Python+OpenCV+图片旋转并用原底色填充新四角的例子就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
pycharm中连接mysql数据库的步骤详解
May 02 Python
K-means聚类算法介绍与利用python实现的代码示例
Nov 13 Python
用TensorFlow实现戴明回归算法的示例
May 02 Python
Python决策树之基于信息增益的特征选择示例
Jun 25 Python
Django migrations 默认目录修改的方法教程
Sep 28 Python
python实现简单的单变量线性回归方法
Nov 08 Python
python 不同方式读取文件速度不同的实例
Nov 09 Python
Flask中endpoint的理解(小结)
Dec 11 Python
python3中calendar返回某一时间点实例讲解
Nov 18 Python
python 逐步回归算法
Apr 06 Python
解决hive中导入text文件遇到的坑
Apr 07 Python
python 判断文件或文件夹是否存在
Mar 18 Python
Python+OpenCV 实现图片无损旋转90°且无黑边
Dec 12 #Python
使用python去除图片白色像素的实例
Dec 12 #Python
用Python去除图像的黑色或白色背景实例
Dec 12 #Python
python 实现将小图片放到另一个较大的白色或黑色背景图片中
Dec 12 #Python
flask的orm框架SQLAlchemy查询实现解析
Dec 12 #Python
python实现批量处理将图片粘贴到另一张图片上并保存
Dec 12 #Python
Python FtpLib模块应用操作详解
Dec 12 #Python
You might like
分页显示Oracle数据库记录的类之一
2006/10/09 PHP
深入PHP5中的魔术方法详解
2013/06/17 PHP
php文件后缀不强制为.php的实操方法
2019/09/18 PHP
PHP设计模式(七)组合模式Composite实例详解【结构型】
2020/05/02 PHP
通过javascript的匿名函数来分析几段简单有趣的代码
2010/06/29 Javascript
JavaScript 原型链学习总结
2010/10/29 Javascript
Jquery中LigerUi的弹出编辑框(实现方法)
2013/07/09 Javascript
jQuery实现倒计时按钮功能代码分享
2014/09/03 Javascript
jquery遍历table的tr获取td的值实现方法
2016/05/19 Javascript
jQuery表单验证简单示例
2016/10/17 Javascript
Angular2 多级注入器详解及实例
2016/10/30 Javascript
BootStrap表单验证 FormValidation 调整反馈图标位置的实例代码
2017/05/17 Javascript
react native实现往服务器上传网络图片的实例
2017/08/07 Javascript
微信小程序url传参写变量的方法
2018/08/09 Javascript
vue 子组件watch监听不到prop的解决
2020/08/09 Javascript
python压缩文件夹内所有文件为zip文件的方法
2015/06/20 Python
简单解决Python文件中文编码问题
2015/11/22 Python
python中闭包Closure函数作为返回值的方法示例
2017/12/17 Python
python使用mysql的两种使用方式
2018/03/07 Python
终端命令查看TensorFlow版本号及路径的方法
2018/06/13 Python
Python OpenCV利用笔记本摄像头实现人脸检测
2020/08/20 Python
python中numpy.empty()函数实例讲解
2021/02/05 Python
HTML5之SVG 2D入门7—SVG元素的重用与引用
2013/01/30 HTML / CSS
微信小程序canvas实现水平、垂直居中效果
2020/02/05 HTML / CSS
美国知名保健品网站:LuckyVitamin(支持中文)
2017/08/09 全球购物
Shell编程面试题
2016/05/29 面试题
分别介绍一下Session Bean和Entity Bean
2015/03/13 面试题
超市5.1促销活动
2014/01/15 职场文书
公司向个人借款协议书范本
2014/10/09 职场文书
2014年售后服务工作总结
2014/11/18 职场文书
家庭暴力离婚起诉书
2015/05/18 职场文书
2016年感恩节寄语
2015/12/07 职场文书
交通事故协议书范本
2016/03/19 职场文书
从贫穷到富有,是知识技能和学习力的差别
2019/08/20 职场文书
浅谈resultMap的用法及关联结果集映射
2021/06/30 Java/Android
el-table-column 内容不自动换行的解决方法
2022/08/14 Vue.js