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 相关文章推荐
详解Python的迭代器、生成器以及相关的itertools包
Apr 02 Python
Python3读取zip文件信息的方法
May 22 Python
在Django的URLconf中使用命名组的方法
Jul 18 Python
机器学习10大经典算法详解
Dec 07 Python
python实现画一颗树和一片森林
Jun 25 Python
Python  unittest单元测试框架的使用
Sep 08 Python
postman模拟访问具有Session的post请求方法
Jul 15 Python
Django视图扩展类知识点详解
Oct 25 Python
使用Python获取当前工作目录和执行命令的位置
Mar 09 Python
python数据处理——对pandas进行数据变频或插值实例
Apr 22 Python
基于Python pyecharts实现多种图例代码解析
Aug 10 Python
详解python with 上下文管理器
Sep 02 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
PHP脚本的10个技巧(1)
2006/10/09 PHP
php 将字符串按大写字母分隔成字符串数组
2010/04/30 PHP
php连接数据库代码应用分析
2011/05/29 PHP
php读取EXCEL文件 php excelreader读取excel文件
2012/12/06 PHP
显示youtube视频缩略图和Vimeo视频缩略图代码分享
2014/02/13 PHP
php设计模式之策略模式实例分析【星际争霸游戏案例】
2020/03/26 PHP
Thinkphp集成抖音SDK的实现方法
2020/04/28 PHP
Alliance vs Liquid BO3 第二场2.13
2021/03/10 DOTA
JQuery Tips(2) 关于$()包装集你不知道的
2009/12/14 Javascript
javascript 鼠标拖动图标技术
2010/02/07 Javascript
JavaScript 实现简单的倒计时弹窗DEMO附图
2014/03/05 Javascript
JS+CSS实现经典的左侧竖向滑动菜单效果
2015/09/23 Javascript
JS实现的简洁二级导航菜单雏形效果
2015/10/13 Javascript
nodejs中使用HTTP分块响应和定时器示例代码
2017/03/19 NodeJs
深入探究AngularJs之$scope对象(作用域)
2017/07/20 Javascript
Vue监听事件实现计数点击依次增加的方法
2018/09/26 Javascript
python字典多条件排序方法实例
2014/06/30 Python
分析在Python中何种情况下需要使用断言
2015/04/01 Python
Python中用memcached来减少数据库查询次数的教程
2015/04/07 Python
python编程开发之类型转换convert实例分析
2015/11/13 Python
Python实现命令行通讯录实例教程
2016/08/18 Python
Python实现获取前100组勾股数的方法示例
2018/05/04 Python
Python实现随机创建电话号码的方法示例
2018/12/07 Python
对python实现二维函数高次拟合的示例详解
2018/12/29 Python
python的依赖管理的实现
2019/05/14 Python
django之对FileField字段的upload_to的设定方法
2019/07/28 Python
Python将二维列表list的数据输出(TXT,Excel)
2020/04/23 Python
h5使用canvas画布实现手势解锁
2019/01/04 HTML / CSS
Veronica Beard官网:在酷、经典和别致之间找到了平衡
2018/01/11 全球购物
Yahoo的PHP面试题
2014/05/26 面试题
介绍一下Mysql的存储引擎
2015/02/12 面试题
若通过ObjectOutputStream向一个文件中多次以追加方式写入object,为什么用ObjectInputStream读取这些object时会产生StreamCorruptedException?
2016/10/17 面试题
中式餐厅创业计划书范文
2014/01/23 职场文书
工商管理专业毕业生自我鉴定2014
2014/10/04 职场文书
税务干部个人整改措施思想汇报
2014/10/10 职场文书
邀请函范文
2015/02/02 职场文书