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

最新文章

  1. HDFS 架构解析
  2. Go - 项目收藏
  3. sys.dm_tran_locks,
  4. WebDriver基本API使用(基于Java)V1.0
  5. hdu 4302 优先队列
  6. 5分钟学习maven(根据英文文档整理)
  7. 九度OJ 1056--最大公约数 1439--Least Common Multiple 【辗转相除法】
  8. iOS,Android网络抓包教程之tcpdump
  9. centOS 7配置Apache + MySQL + PHP
  10. VirtualBox报错:不能为虚拟电脑XXX打开一个新任务
  11. Product(欧拉函数)
  12. 【redis】-- springboot集成redis及使用
  13. gitlab runner安装与使用
  14. 杂七杂八的JavaScript
  15. 2014年蓝桥杯省赛A组c++第2题(推公式)
  16. ASP.NET MVC3 Model的常用验证示例
  17. scala 建模
  18. codeforces 1053D 树形DP
  19. Centos7 防火墙关闭和启用iptables防火墙
  20. swift学习之-- UIAlertViewController -alert

热门文章

  1. matlab图像处理程序大集合
  2. Linux安装redis报错:jemalloc/jemalloc.h: No such file or directory踩坑
  3. Object level permissions support
  4. asctime_s asctime
  5. 【C++小知识】#define、enum、const的含义与用法
  6. JetBrains(IEDA、CLion、Pycharm) 学生获得免费使用资格
  7. scala中List、Array、ListBuffer、ArrayList、Set
  8. 深信服edr控制中心漏洞——验证码逻辑错误
  9. 虚拟局域网(VLAN)__语音VLAN
  10. 理解了这三点,才敢说自己会写Python代码