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 连连看连接算法
Nov 22 Python
Python黑魔法@property装饰器的使用技巧解析
Jun 16 Python
Python 描述符(Descriptor)入门
Nov 20 Python
Odoo中如何生成唯一不重复的序列号详解
Feb 10 Python
tensorflow实现加载mnist数据集
Sep 08 Python
Python实现FM算法解析
Jun 18 Python
python bluetooth蓝牙信息获取蓝牙设备类型的方法
Nov 29 Python
python turtle工具绘制四叶草的实例分享
Feb 14 Python
PyCharm MySQL可视化Database配置过程图解
Jun 09 Python
python和go语言的区别是什么
Jul 20 Python
如何在windows下安装配置python工具Ulipad
Oct 27 Python
 python中的元类metaclass详情
May 30 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
通过ICQ网关发送手机短信的PHP源程序
2006/10/09 PHP
php fckeditor 调用的函数
2009/06/21 PHP
Php无限级栏目分类读取的实现代码
2014/02/19 PHP
php实现批量上传数据到数据库(.csv格式)的案例
2017/06/18 PHP
PHP创建文件及写入数据(覆盖写入,追加写入)的方法详解
2019/02/15 PHP
JS.elementGetStyle(element, style)应用示例
2013/09/24 Javascript
让浏览器DOM元素最后加载的js方法
2014/07/29 Javascript
jquery简单实现带渐显效果的选项卡菜单代码
2015/09/01 Javascript
jquery获取url参数及url加参数的方法
2015/10/26 Javascript
Node.js实现兼容IE789的文件上传进度条
2016/09/02 Javascript
jQuery插件fullPage.js实现全屏滚动效果
2016/12/02 Javascript
微信小程序动态增加按钮组件
2018/09/14 Javascript
详解如何为你的angular app构建一个第三方库
2018/12/07 Javascript
浅谈vue加载优化策略
2019/03/19 Javascript
vue插槽slot的理解和使用方法
2019/04/03 Javascript
Vue Cli 3项目使用融云IM实现聊天功能的方法
2019/04/19 Javascript
Vue注册组件命名时不能用大写的原因浅析
2019/04/25 Javascript
[02:29]完美世界高校联赛上海赛区回顾
2015/12/15 DOTA
[01:07:13]TNC vs Pain 2018国际邀请赛小组赛BO2 第一场 8.17
2018/08/20 DOTA
在Django的模型和公用函数中使用惰性翻译对象
2015/07/27 Python
Python 的描述符 descriptor详解
2016/02/27 Python
pyspark 读取csv文件创建DataFrame的两种方法
2018/06/07 Python
解决Python 使用h5py加载文件,看不到keys()的问题
2019/02/08 Python
Django 迁移、操作数据库的方法
2019/08/02 Python
python批量读取文件名并写入txt文件中
2020/09/05 Python
python3实现用turtle模块画一棵随机樱花树
2019/11/21 Python
pyenv虚拟环境管理python多版本和软件库的方法
2019/12/26 Python
文件上传服务器-jupyter 中python解压及压缩方式
2020/04/22 Python
Spring http服务远程调用实现过程解析
2020/06/11 Python
工作失职造成投诉的检讨书范文
2014/10/05 职场文书
九寨沟导游词
2015/02/02 职场文书
民主评议党员个人自我评价
2015/03/03 职场文书
消费者理赔投诉书
2015/07/02 职场文书
2015年中秋晚会主持稿
2015/07/30 职场文书
在python中实现导入一个需要传参的模块
2021/05/12 Python
详解JAVA中的OPTIONAL
2021/06/14 Java/Android