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操作时间和日期之asctime()方法的使用
May 22 Python
Python编程之string相关操作实例详解
Jul 22 Python
浅谈python 线程池threadpool之实现
Nov 17 Python
Django的分页器实例(paginator)
Dec 01 Python
基于Django用户认证系统详解
Feb 21 Python
Python实现定制自动化业务流量报表周报功能【XlsxWriter模块】
Mar 11 Python
pandas DataFrame创建方法的方式
Aug 02 Python
python代码实现逻辑回归logistic原理
Aug 07 Python
python开头的coding设置方法
Aug 08 Python
python相对企业语言优势在哪
Jun 12 Python
python 如何停止一个死循环的线程
Nov 24 Python
Pygame Rect区域位置的使用(图文)
Nov 17 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访问MYSQL数据库封装类(附函数说明)
2010/12/04 PHP
php+Mysqli利用事务处理转账问题实例
2015/02/11 PHP
如何在旧的PHP系统中使用PHP 5.3之后的库
2015/12/02 PHP
Smarty环境配置与使用入门教程
2016/05/11 PHP
php is_executable判断给定文件名是否可执行实例
2016/09/26 PHP
PHP串行化与反串行化实例分析
2016/12/27 PHP
js 禁止选择功能实现代码(兼容IE/Firefox)
2010/04/23 Javascript
JQuery 1.3.2以上版本中出现pareseerror错误的解决方法
2011/01/11 Javascript
javascript实现文本域写入字符时限定字数
2014/02/12 Javascript
JavaScript生成SQL查询表单的方法
2015/08/13 Javascript
jquery实现可横向和竖向展开的动态下滑菜单效果
2015/08/24 Javascript
日常收藏的jquery技巧
2015/12/02 Javascript
使用BootStrap实现悬浮窗口的效果
2016/12/13 Javascript
webpack2.0配置postcss-loader的方法
2017/08/17 Javascript
vue 实现类似淘宝星级评分的示例
2018/03/01 Javascript
vue 标签属性数据绑定和拼接的实现方法
2018/05/17 Javascript
详解JavaScript中关于this指向的4种情况
2019/04/18 Javascript
基于JS开发微信网页录音功能的实例代码
2019/04/30 Javascript
小程序实现搜索界面 小程序实现推荐搜索列表效果
2019/05/18 Javascript
vue实现codemirror代码编辑器中的SQL代码格式化功能
2019/08/27 Javascript
vue抽出组件并传值实例
2020/07/31 Javascript
JavaScript中的执行环境和作用域链
2020/09/04 Javascript
浅谈Pandas中map, applymap and apply的区别
2018/04/10 Python
Python pandas实现excel工作表合并功能详解
2019/08/29 Python
Pycharm连接远程服务器过程图解
2020/04/30 Python
把富文本的回车转为br标签
2019/08/09 HTML / CSS
使用Html5中的cavas画一面国旗
2019/09/25 HTML / CSS
美国家具网站:Cymax
2016/09/17 全球购物
秘鲁购物网站:Linio秘鲁
2017/04/07 全球购物
请写出一段Python代码实现删除一个list里面的重复元素
2015/12/29 面试题
大学生的创业计划书就该这么写
2014/01/30 职场文书
2014年文学毕业生自我鉴定
2014/04/23 职场文书
酒店员工培训方案
2014/06/02 职场文书
水电维修专业推荐信
2014/09/06 职场文书
感恩祖国演讲稿
2014/09/09 职场文书
MySQL插入数据与查询数据
2022/03/25 MySQL