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遍历指定文件及文件夹的方法
May 09 Python
Python中每次处理一个字符的5种方法
May 21 Python
详解python中requirements.txt的一切
Mar 03 Python
Python使用progressbar模块实现的显示进度条功能
May 31 Python
python 检查文件mime类型的方法
Dec 08 Python
django框架防止XSS注入的方法分析
Jun 21 Python
基于Python获取照片的GPS位置信息
Jan 20 Python
基于python3生成标签云代码解析
Feb 18 Python
Python基于pandas爬取网页表格数据
May 11 Python
使用python matploblib库绘制准确率,损失率折线图
Jun 16 Python
python 制作网站筛选工具(附源码)
Jan 21 Python
python 算法题——快乐数的多种解法
May 27 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 多个submit提交表单 处理方法
2009/07/07 PHP
php实现文件下载简单示例(代码实现文件下载)
2014/03/10 PHP
PHP采用XML-RPC构造Web Service实例教程
2014/07/16 PHP
在PHP程序中使用Rust扩展的方法
2015/07/03 PHP
Windows下wamp php单元测试工具PHPUnit安装及生成日志文件配置方法
2018/05/28 PHP
laravel实现上传图片,并且制作缩略图,按照日期存放的代码
2019/10/16 PHP
用Javscript实现表单复选框的全选功能
2007/05/25 Javascript
Firefox window.close()的使用注意事项
2009/04/11 Javascript
js 与或运算符 || && 妙用
2009/12/09 Javascript
JavaScript Event学习第七章 事件属性
2010/02/07 Javascript
jQueryUI如何自定义组件实现代码
2010/11/14 Javascript
在iframe里的页面编写js,实现在父窗口上创建动画效果展开和收缩的div(不变动iframe父窗口代码)
2011/12/20 Javascript
如何使用jQuery来处理图片坏链具体实现步骤
2013/05/02 Javascript
网页右下角弹出窗体实现代码
2014/06/05 Javascript
js从数组中删除指定值(不是指定位置)的元素实现代码
2016/09/13 Javascript
Bootstrap进度条学习使用
2017/02/09 Javascript
利用vue + element实现表格分页和前端搜索的方法
2017/12/25 Javascript
vue二级菜单导航点击选中事件的方法
2018/09/12 Javascript
微信小程序实现提交input信息到后台的方法示例
2019/01/19 Javascript
[57:31]DOTA2-DPC中国联赛 正赛 SAG vs CDEC BO3 第一场 2月1日
2021/03/11 DOTA
Python实现将DOC文档转换为PDF的方法
2015/07/25 Python
对Python中list的倒序索引和切片实例讲解
2018/11/15 Python
Django组件之cookie与session的使用方法
2019/01/10 Python
Python3.5内置模块之shelve模块、xml模块、configparser模块、hashlib、hmac模块用法分析
2019/04/27 Python
python项目对接钉钉SDK的实现
2019/07/15 Python
Python Django 简单分页的实现代码解析
2019/08/21 Python
Python之——生成动态路由轨迹图的实例
2019/11/22 Python
Python3.7将普通图片(png)转换为SVG图片格式(网站logo图标)动起来
2020/04/21 Python
Python错误的处理方法
2020/06/23 Python
使用CSS3中的calc()属性来以算式表达尺寸数值
2016/06/06 HTML / CSS
美国折扣网站:jClub
2017/08/07 全球购物
美国首屈一指的高品质珠宝设计师和零售商:Allurez
2018/01/23 全球购物
正隆泰信息技术有限公司上机题
2012/06/14 面试题
仓库管理专业个人的自我评价
2013/12/30 职场文书
《罗布泊,消逝的仙湖》教学反思
2014/03/01 职场文书
2014年单位工作总结范文
2014/11/27 职场文书