Python+OpenCV+图片旋转并用原底色填充新四角
2024-08-24 05:06:02
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
最新文章
- HDFS 架构解析
- Go - 项目收藏
- sys.dm_tran_locks,
- WebDriver基本API使用(基于Java)V1.0
- hdu 4302 优先队列
- 5分钟学习maven(根据英文文档整理)
- 九度OJ 1056--最大公约数 1439--Least Common Multiple 【辗转相除法】
- iOS,Android网络抓包教程之tcpdump
- centOS 7配置Apache + MySQL + PHP
- VirtualBox报错:不能为虚拟电脑XXX打开一个新任务
- Product(欧拉函数)
- 【redis】-- springboot集成redis及使用
- gitlab runner安装与使用
- 杂七杂八的JavaScript
- 2014年蓝桥杯省赛A组c++第2题(推公式)
- ASP.NET MVC3 Model的常用验证示例
- scala 建模
- codeforces 1053D 树形DP
- Centos7 防火墙关闭和启用iptables防火墙
- swift学习之-- UIAlertViewController -alert
热门文章
- matlab图像处理程序大集合
- Linux安装redis报错:jemalloc/jemalloc.h: No such file or directory踩坑
- Object level permissions support
- asctime_s asctime
- 【C++小知识】#define、enum、const的含义与用法
- JetBrains(IEDA、CLion、Pycharm) 学生获得免费使用资格
- scala中List、Array、ListBuffer、ArrayList、Set
- 深信服edr控制中心漏洞——验证码逻辑错误
- 虚拟局域网(VLAN)__语音VLAN
- 理解了这三点,才敢说自己会写Python代码