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中fileno()方法的使用
May 24 Python
Python输出9*9乘法表的方法
May 25 Python
Python获取本机所有网卡ip,掩码和广播地址实例代码
Jan 22 Python
pthon贪吃蛇游戏详细代码
Jan 27 Python
Python安装依赖(包)模块方法详解
Feb 14 Python
一文了解python 3 字符串格式化 F-string 用法
Mar 04 Python
Python Django2 model 查询介绍(条件、范围、模糊查询)
Mar 16 Python
Python学习之路之pycharm的第一个项目搭建过程
Jun 18 Python
降低python版本的操作方法
Sep 11 Python
pytorch学习教程之自定义数据集
Nov 10 Python
关于Numpy之repeat、tile的用法总结
Jun 02 Python
Python中的 No Module named ***问题及解决
Jul 23 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字符串过滤,转换函数代码
2012/05/01 PHP
YII路径的用法总结
2014/07/09 PHP
php使用date和strtotime函数输出指定日期的方法
2014/11/14 PHP
PHP基于yii框架实现生成ICO图标
2015/11/13 PHP
PHP实现简易用户登录系统
2020/07/10 PHP
javascript实现动态增加删除表格行(兼容IE/FF)
2007/04/02 Javascript
jQuery 源码分析笔记(2) 变量列表
2011/05/28 Javascript
JavaScript sub方法入门实例(把字符串显示为下标)
2014/10/17 Javascript
JavaScript实现算术平方根算法-代码超简单
2015/09/11 Javascript
jQuery实现的导航下拉菜单效果
2016/07/04 Javascript
vue使用vue-cli快速创建工程
2017/07/28 Javascript
Vue-router 类似Vuex实现组件化开发的示例
2017/09/15 Javascript
js保留两位小数方法总结
2018/01/31 Javascript
详解vue2.6插槽更新v-slot用法总结
2019/03/09 Javascript
详解JS取出两个数组中的不同或相同元素
2019/03/20 Javascript
vue-router 起步步骤详解
2019/03/26 Javascript
Vue中qs插件的使用详解
2020/02/07 Javascript
Node.js API详解之 dgram模块用法实例分析
2020/06/05 Javascript
Node.js path模块,获取文件后缀名操作
2020/11/07 Javascript
[01:27]DOTA2电竞之夜 今夜共饮庆功酒
2014/08/02 DOTA
python基础教程之lambda表达式使用方法
2014/02/12 Python
Python利用itchat对微信中好友数据实现简单分析的方法
2017/11/21 Python
Python模拟简单电梯调度算法示例
2018/08/20 Python
python实战串口助手_解决8串口多个发送的问题
2019/06/12 Python
pandas实现to_sql将DataFrame保存到数据库中
2019/07/03 Python
python实现在一个画布上画多个子图
2020/01/19 Python
Python图片处理模块PIL操作方法(pillow)
2020/04/07 Python
CSS3教程(1):什么是CSS3
2009/04/02 HTML / CSS
不可轻视HTML5!App三年内将被html5顶替彻底消失
2015/11/18 HTML / CSS
夜大毕业自我鉴定
2013/10/11 职场文书
保安拾金不昧表扬信
2014/01/15 职场文书
重大事项社会稳定风险评估方案
2014/06/15 职场文书
乡镇党的群众路线教育实践活动个人对照检查材料
2014/09/23 职场文书
2014年小学英语教师工作总
2014/12/03 职场文书
2015年加油站站长工作总结
2015/05/27 职场文书
2016继续教育研修日志
2015/11/13 职场文书