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实现实例
Apr 26 Python
python使用nntp读取新闻组内容的方法
May 08 Python
Python实现购物车购物小程序
Apr 18 Python
解决pandas无法在pycharm中使用plot()方法显示图像的问题
May 24 Python
使用pytorch进行图像的顺序读取方法
Jul 27 Python
python3利用tcp实现文件夹远程传输
Jul 28 Python
python3+PyQt5 创建多线程网络应用-TCP客户端和TCP服务器实例
Jun 17 Python
tensorflow之tf.record实现存浮点数数组
Feb 17 Python
python标准库OS模块函数列表与实例全解
Mar 10 Python
浅谈python opencv对图像颜色通道进行加减操作溢出
Jun 03 Python
python右对齐的实例方法
Jul 05 Python
python3字符串输出常见面试题总结
Dec 01 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数组生成XML格式数据的封装类实例
2016/11/10 PHP
JScript内置对象Array中元素的删除方法
2007/03/08 Javascript
弹出广告特效代码(一个IP只弹出一次)
2007/05/11 Javascript
比较简单实用的使用正则三种版本的js去空格处理方法
2007/11/18 Javascript
javascript 常用关键字列表集合
2007/12/04 Javascript
Javascript 表单之间的数据传递代码
2008/12/04 Javascript
jquery实用代码片段集合
2010/08/12 Javascript
MooBox 基于Mootools的对话框插件
2012/01/20 Javascript
JavaScript面向对象编写购物车功能
2016/08/19 Javascript
jquery select2的使用心得(推荐)
2016/12/04 Javascript
jQuery插件select2利用ajax高效查询大数据列表(可搜索、可分页)
2017/05/19 jQuery
AngularJS基于provider实现全局变量的读取和赋值方法
2017/06/28 Javascript
JavaScript canvas实现围绕旋转动画
2017/11/18 Javascript
微信小程序实现下拉刷新和轮播图效果
2017/11/21 Javascript
HTML5+JS+JQuery+ECharts实现异步加载问题
2017/12/16 jQuery
NodeJs项目中关闭ESLint的方法
2018/08/09 NodeJs
小程序测试后台服务的方法(ngrok)
2019/03/08 Javascript
微信小程序之onLaunch与onload异步问题详解
2019/03/28 Javascript
vue2.0自定义指令示例代码详解
2019/04/25 Javascript
JS禁用右键、禁用Ctrl+u、禁用Ctrl+s、禁用F12的实现代码
2020/12/01 Javascript
Python实现将绝对URL替换成相对URL的方法
2015/06/28 Python
PyChar学习教程之自定义文件与代码模板详解
2017/07/17 Python
Python使用当前时间、随机数产生一个唯一数字的方法
2017/09/18 Python
python框架flask表单实现详解
2019/11/04 Python
Python time库基本使用方法分析
2019/12/13 Python
python可迭代对象去重实例
2020/05/15 Python
python 实现PIL模块在图片画线写字
2020/05/16 Python
在pycharm中使用matplotlib.pyplot 绘图时报错的解决
2020/06/01 Python
纯css3实现思维导图样式示例
2018/11/01 HTML / CSS
HTML5 visibilityState属性详细介绍和使用实例
2014/05/03 HTML / CSS
浅析HTML5中header标签的用法
2016/06/24 HTML / CSS
如何开发安全的AJAX应用
2014/03/26 面试题
2014新年寄语
2014/01/20 职场文书
企业管理培训感言
2014/01/27 职场文书
超市活动计划书
2014/04/24 职场文书
Go语言基础知识点介绍
2021/07/04 Golang