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实现从一组颜色中找出与给定颜色最接近颜色的方法
Mar 19 Python
Python中的with语句与上下文管理器学习总结
Jun 28 Python
python中import学习备忘笔记
Jan 24 Python
[原创]使用豆瓣提供的国内pypi源
Jul 02 Python
Python多进程原理与用法分析
Aug 21 Python
Python开发网站目录扫描器的实现
Feb 21 Python
提升Python效率之使用循环机制代替递归函数
Jul 23 Python
适合Python初学者的一些编程技巧
Feb 12 Python
django filter过滤器实现显示某个类型指定字段不同值方式
Jul 16 Python
详解python百行有效代码实现汉诺塔小游戏(简约版)
Oct 30 Python
只用40行Python代码就能写出pdf转word小工具
May 31 Python
Python软件包安装的三种常见方法
Jul 07 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 file_exists问题杂谈
2012/05/07 PHP
Yii框架登录流程分析
2014/12/03 PHP
PHP常用字符串操作函数实例总结(trim、nl2br、addcslashes、uudecode、md5等)
2016/01/09 PHP
PHP中FTP相关函数小结
2016/07/15 PHP
Yii2框架中使用PHPExcel导出Excel文件的示例
2017/08/09 PHP
centos7上编译安装php7以php-fpm方式连接apache
2018/11/08 PHP
xml分页+ajax请求数据源+dom取结果实例代码
2008/10/31 Javascript
js实现在文本框光标处添加字符的方法介绍
2012/11/24 Javascript
表单元素的submit()方法和onsubmit事件应用概述
2013/02/01 Javascript
自动刷新网页,自动刷新当前页面,JS调用
2013/06/24 Javascript
jquery实现动态菜单的实例代码
2013/11/28 Javascript
Jquery遍历checkbox获取选中项value值的方法
2014/02/13 Javascript
基于JavaScript实现定时跳转到指定页面
2016/01/01 Javascript
浅谈javascript的call()、apply()、bind()的用法
2016/02/21 Javascript
Js apply方法详解
2017/02/16 Javascript
微信小程序 定位到当前城市实现实例代码
2017/02/23 Javascript
Javascript实现一个简单的输入关键字添加标签效果实例
2017/06/01 Javascript
Vue实战之vue登录验证的实现代码
2017/10/31 Javascript
vue实现微信分享朋友圈,发送朋友的示例讲解
2018/02/10 Javascript
vue路由对不同界面进行传参及跳转的总结
2019/04/20 Javascript
微信端调取相册和摄像头功能,实现图片上传,并上传到服务器
2019/05/16 Javascript
PHP 502bad gateway原因及解决方案
2020/11/13 Javascript
[49:56]VG vs Optic 2018国际邀请赛小组赛BO2 第一场 8.19
2018/08/21 DOTA
[01:28]一分钟告诉你DOTA2 TI9不朽宝藏Ⅱ中有什么!
2019/07/09 DOTA
python判断windows隐藏文件的方法
2014/03/21 Python
python网络编程之数据传输UDP实例分析
2015/05/20 Python
Python实现删除当前目录下除当前脚本以外的文件和文件夹实例
2015/07/27 Python
python 多线程实现检测服务器在线情况
2015/11/25 Python
python利用socketserver实现并发套接字功能
2018/01/26 Python
美国新蛋IT数码商城:Newegg.com
2016/07/21 全球购物
承诺书的格式范文
2014/03/28 职场文书
敬业奉献模范事迹材料
2014/12/24 职场文书
房产公证书格式
2015/01/26 职场文书
商务信函英语问候语
2015/11/10 职场文书
财务会计个人原因辞职信
2019/06/21 职场文书
使用python+pygame开发消消乐游戏附完整源码
2021/06/10 Python