Python+OpenCV+图片旋转并用原底色填充新四角的例子
作者:默盒 发布时间:2022-09-07 14:11:23
标签:Python,OpenCV,图片旋转
我就废话不多说了,直接上代码吧!
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
来源:https://www.cnblogs.com/ZhengPeng7/p/7390119.html


猜你喜欢
- 做项目的时候,一位同事导数据的时候,不小心把一个表中的数据全都搞重了,也就是说,这个表里所有的记录都有一条重复的。这个表的数据是千万级的,而
- 第一种:i=0sum=0a=0while i<102: if i>=1 and i%4==1: sum+=i eli
- 如下所示:# 创建一个空的 DataFramedf_empty = pd.DataFrame()#或者df_empty = pd.DataF
- 本文研究的主要是Django权限机制的相关内容,具体如下。1. Django权限机制概述权限机制能够约束用户行为,控制页面的显示内容,也能使
- 本文实例为大家分享了java正则表达式工具类的具体代码,供大家参考,具体内容如下import com.google.common.base.
- 1,目标环境Windows 7 64位2,材料(1)VC++2010 发布包(64位)(2)MySQL 5.6.36 Windows x64
- 因为有个老Yashica相机机身,前一阵忍不住想配几个标准镜头。到国内购物网站以及摄影器材交流论坛上看了看,发现不仅很少,价格不实在,而且品
- 大家好,我是朱小五。大家如果看过我的书《快学Python:自动化办公轻松实战》,会发现Python操作PDF文档内容,主要围绕PDF文档的内
- 有没有一种方法可以为Django项目中的每个应用程序创建多个自定义错误模板,我的意思是,在我的项目中,我有3个应用程序,每个应用程序将显示3
- 变量(Variable)可以看成一个小箱子,专门用来“盛装”程序中的数据。每个变量都拥有独一无二的名字,通过变量的名字就能找到变量中的数据。
- MySQL数据库的备份有很多工具可以使用,这两天写了一个使用C#调用MYSQL的mysqldump命令完成MySQL数据库的备份与恢复的小工
- 现在很多CMS系统因为安全原因会把后台编辑器里的上传功能给去除,但这样一来对实际使用过程造成了很多麻烦,今天我们以ASPCMS系统的FCKe
- 修改MySql Server安装目录下的 my.ini 文件,在mysqld节下加入下面一行set-variable=lower_case_
- 这里介绍使用python-Django框架来实现web端分页呈现数据,主要说明对应的views,urls,templates三个文件的编程逻
- 1 装饰器背景知识1.1 基本概念装饰器(Decorator)是 Python 中一种函数或类,用来修饰其他函数或类。装饰器可以改变被装饰函
- 在认证框架中还有其他的一些功能。 我们会在接下来的几个部分中进一步地了解它们。权限权限可以很方便地标识用户和用户组可以执行的操作。 它们被D
- 一、Socketserver实现FTP,文件上传、下载目录结构1、socketserver实现ftp文件上传下载,可以同时多用户登录、上传、
- Python线程与进程进程:进程是程序的一次执行,每个进程都有自己的地址空间、内存、数据栈以及其他记录其运行的辅助数据。线程:所有的线程运行
- 环境Win10Python3.6.6Django2.1.3中间件作用 中间件用于全局修改Django的输入或输出。中间件常见用途 缓存会话认
- [Q]怎么样查询特殊字符,如通配符%与_ [Q]如何插入单引号到数据库表中 [Q]怎样设置事务一致性 [Q]怎么样利用光标更新数据 [Q]怎