图像旋转角度计算并旋转
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import cv2
import numpy as np
import timedef Rotate(img, angle=0.0,fill=0):"""旋转:param img:待旋转图像:param angle: 旋转角度:param fill:填充方式,默认0黑色填充:return: img: 旋转后的图像"""w, h = img.shape[:2]center = (int(w / 2), int(h / 2))rot = cv2.getRotationMatrix2D(center, angle, 1.0)img = cv2.warpAffine(img, rot, (h, w), borderValue=fill)return imgdef CalcAngle(img):h, w = img.shape[:2]x1, y1, x2, y2 = 0, 0, 0, 0angle = 0for i in range(h - 1):if img[i][int(w / 3)] == 0 and img[i - 1][int(w / 3)] != 0:# print("1",int(w/3),i)x1, y1 = int(w / 3), iif img[i][int(w * 2 / 3)] == 0 and img[i - 1][int(w * 2 / 3)] != 0:# print("2",int(w*2/3),i)x2, y2 = int(w * 2 / 3), iif x1 != 0 and y1 != 0 and x2 != 0 and y2 != 0:if x2 - x1 == 0 or y2 - y1 == 0:print(u"不需要旋转")return 0else:length = (y2 - y1) / (x2 - x1)angle = np.arctan(length) / 0.017453if angle < -45:angle = angle + 90elif angle > 45:angle = angle - 90else:passprint(u"旋转角度:", angle)return angle
starts = time.clock()img1=cv2.imread("box.jpg",0)
# img=Rotate(img1,2,255)
ret,img=cv2.threshold(img1,200,255,cv2.THRESH_BINARY)
# cv2.imshow("0",img)
img = cv2.Canny(img, 10, 255, apertureSize=3)
angle=CalcAngle(img)
img=Rotate(img1,angle)
ends = time.clock()
print("time", ends - starts, "秒")
cv2.imwrite("00.jpg",img)
# cv2.imshow("00",img)
cv2.waitKey(0)
cv2.destroyAllWindows()
使用两张测试图片如下:

对于lena的图像测试结果如下:


另一张测试图片结果如下:

也可以使用下面代码进行测试:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import cv2
import time
import numpy as npdef Location(img, tmp, threshold_value=120, dilate=3, resize_multiple=16):"""图像定位:param img: 输入原图:param tmp: 定位匹配模板:param threshold_value: 图像阈值:param dilate: 膨胀值:param resize_multiple:缩小倍率:return: rect:矩形坐标点,从右上xy到右下xy,四个值"""h, w = img.shape[:2]hy, wx = tmp.shape[:2]img = cv2.resize(img, (int(w * 1 / resize_multiple), int(h * 1 / resize_multiple)), interpolation=cv2.INTER_AREA)kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))img = cv2.erode(img, kernel, iterations=dilate)w, h = img.shape[:2]for i in range(w):for j in range(h):if img[i][j] >= threshold_value:img[i][j] = 255else:img[i][j] = 0res = cv2.matchTemplate(img, tmp, cv2.TM_SQDIFF_NORMED)min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)top_left = min_loc# bottom_right = ((top_left[0] + wx) * resize_multiple, (top_left[1] + hy) * resize_multiple)# top_left = (top_left[0] * resize_multiple, top_left[1] * resize_multiple)rect = [top_left[0] * resize_multiple, top_left[1] * resize_multiple, (top_left[0] + wx) * resize_multiple,(top_left[1] + hy) * resize_multiple]return rectdef RotateAngle(img, threshold_value=120, dilate=3,linenum=6):"""计算图像旋转角度:param img: 输入图像:param threshold_value: 阈值分割:param dilate: 膨胀值:return: angle: 旋转角度"""ret,img=cv2.threshold(img,threshold_value,255,cv2.THRESH_BINARY)img_w, img_h = img.shape[:2]# kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 2))# img = cv2.erode(img, kernel, iterations=dilate)line_widthsize = int(img_w)line_lensize = int(img_h / linenum)edges = cv2.Canny(img, 10, 255, apertureSize=3)try:lines = cv2.HoughLinesP(edges, 1, np.pi / 180, line_lensize, minLineLength=int(line_widthsize / 2),maxLineGap=line_widthsize)for line in lines[0]:# print("角度测量的直线坐标", line)x1, y1, x2, y2 = lineif x2 - x1 == 0 or y2 - y1 == 0:print(u"不需要旋转")return 0else:length = (y2 - y1) / (x2 - x1)angle = np.arctan(length) / 0.017453if angle < -45:angle = angle + 90elif angle > 45:angle = angle - 90else:passprint(u"旋转角度:", angle)return angleexcept:return 0def Rotate(img, angle=0.0):"""旋转:param img:待旋转图像:param angle: 旋转角度:return: img: 旋转后的图像"""w, h = img.shape[:2]center = (int(w / 2), int(h / 2))rot = cv2.getRotationMatrix2D(center, angle, 1.0)img = cv2.warpAffine(img, rot, (h, w), borderValue=255)return imgdef GetObject_Location(img, tmp, threshold_value=120, dilate=3, resize_multiple=16):"""旋转:param img:图像:param tmp: 模板:param threshold_value:阈值:param dilate: 膨胀值:param resize_multiple:缩放倍数:return:"""rect = Location(img, tmp, threshold_value, dilate, resize_multiple)imgout = img[rect[1]:rect[3], rect[0]:rect[2]]angle = RotateAngle(imgout, threshold_value, dilate, resize_multiple, linenum=6)img = Rotate(imgout, angle)return imgdef SaveTemple(img, file_name=".\\data\\Temple1.jpg", threshold_value=200, dilate=3, resize_multiple=16):"""模板生成存储:param img: 输入图像:param file_name: 模板保存地址:param threshold_value: 阈值分割:param dilate: 膨胀值:return: img: 保存模板图片到本地"""h, w = img.shape[:2]img = cv2.resize(img, (int(w * 1 / resize_multiple), int(h * 1 / resize_multiple)), interpolation=cv2.INTER_AREA)img_w, img_h = img.shape[:2]print(img_w, img_h)# 创建标准模板imgout = np.zeros((img_w + 4, img_h + 4, 1), np.uint8)# 图像初始化白色for i in range(img_w + 4):for j in range(img_h + 4):imgout[i][j] = 255# 图像二值化for i in range(img_w):for j in range(img_h):if img[i][j] >= threshold_value:img[i][j] = 255else:img[i][j] = 0kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))img = cv2.erode(img, kernel, iterations=dilate)for i in range(img_w):for j in range(img_h):if img[i][j] >= threshold_value:passelse:imgout[i + 2][j + 2] = 0cv2.imwrite(file_name, imgout)"""一次切割,根据投影切割"""def FirstCutting(img, Cvalue, Cerode, LineNum, LineNum1):(_, thresh) = cv2.threshold(img, Cvalue, 255, cv2.THRESH_BINARY)kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))outimg = cv2.erode(thresh, kernel, iterations=Cerode)height, width = outimg.shape[:2]z = [0] * heightv = [0] * widthhfg = [[0 for col in range(2)] for row in range(height)]lfg = [[0 for col1 in range(2)] for row1 in range(width)]Box = []linea = 0BlackNumber = 0for y in range(height):for x in range(width):cp = outimg[y][x]if cp == 0:linea = linea + 1BlackNumber += 1else:continuez[y] = linealinea = 0inline, start, lineNumber = 1, 0, 0for i in range(0, height):if inline == 1 and z[i] >= LineNum:start = iinline = 0elif (i - start > 3) and z[i] < LineNum and inline == 0:inline = 1hfg[lineNumber][0] = start - 2 # 保存行的分割位置起始位置hfg[lineNumber][1] = i + 2 # 保存行的分割终点位置lineNumber = lineNumber + 1lineb = 0for p in range(0, lineNumber):for x in range(0, width):for y in range(hfg[p][0], hfg[p][1]):cp1 = outimg[y][x]if cp1 == 0:lineb = lineb + 1else:continuev[x] = lineblineb = 0incol, start1, lineNumber1 = 1, 0, 0z1 = hfg[p][0]z2 = hfg[p][1]for i1 in range(0, width):if incol == 1 and v[i1] >= LineNum1:start1 = i1incol = 0elif (i1 - start1 > 3) and v[i1] < LineNum1 and incol == 0:incol = 1lfg[lineNumber1][0] = start1 - 3lfg[lineNumber1][1] = i1 + 3l1 = start1 - 3l2 = i1 + 3tmp = [l1, z1, l2, z2]Box.append(tmp)lineNumber1 = lineNumber1 + 1# outimg=cv2.rectangle(outimg,(l1,z1),(l2,z2),(0,255,0),1)return Box, BlackNumber, outimgdef Threshold(img, threshold, KernelValue=3, KernelValue1=(1, 1)):"""根据阈值框选:param img:输入待处理的图像:param threshold:阈值:param KernelValue:卷积核:return:outimg:输出处理后的图像"""w, h = img.shape[:2]for i in range(w):for j in range(h):"""通过设置阈值,来控制喷码花的程度"""if img[i][j] >= threshold:img[i][j] = 255else:img[i][j] = 0kernel = cv2.getStructuringElement(cv2.MORPH_RECT, KernelValue1)outimg = cv2.erode(img, kernel, iterations=KernelValue)outimg = cv2.dilate(outimg, kernel, iterations=KernelValue)return outimg"""根据投影计算出来的坐标进行数组切割"""starts = time.clock()
img = cv2.imread("lena.jpg", 0)
# img=Rotate(img,2)
angle=RotateAngle(img,200)
print(angle)
img=Rotate(img,angle)
cv2.imwrite("00.jpg",img)
ends = time.clock()
print("time", ends - starts, "秒")# img=cv2.imread("formal.bmp",0)
# SaveTemple(img)
lena结果如下:

美女图片测试结果:

说明:以上代码仅仅是讲解介绍了图像旋转的计算及矫正原理,实际上准确度受不同图像的影响较大,不过里面使用的相关图像变换的函数值得借鉴参考学习。
相关文章:
图像旋转角度计算并旋转
#!/usr/bin/python3 # -*- coding: utf-8 -*- import cv2 import numpy as np import timedef Rotate(img, angle0.0,fill0):"""旋转:param img:待旋转图像:param angle: 旋转角度:param fill:填充方式,默认0黑色填充:return: img: 旋转后…...
通过curl访问k8s集群获取证书或token的方式
K8S安全控制框架主要由下面3个阶段进行控制,每一个阶段都支持插件方式,通过API Server配置来启用插件。 1. Authentication(认证) 2. Authorization(授权) 3. Admission Control(准入控制&#…...
苍穹外卖-前端部分(持续更新中)
d 第二种:cmd中输入 vue ui进入图形化界面选择npm,vue2进行创建 先将创建的Vue框架导入Vsocde开发工具 然后ctrshiftp 输入npm 点击serve将项目启动 下这种写法跨域会报错: 解决方法:...
windows用mingw(g++)编译opencv,opencv_contrib,并install安装
windows下用mingw编译opencv貌似不支持cuda,选cuda会报错,我无法解决,所以没选cuda,下面两种编译方式支持。 如要用msvc编译opencv,参考我另外一篇文章 https://blog.csdn.net/weixin_44733606/article/details/1357…...
JDWP 协议及实现
JDWP 的协议细节并通过实际调试中的例子展开揭示 JDWP 的实现机制,JDWP 是 Java Debug Wire Protocol 的缩写,它定义了调试器(debugger)和被调试的 Java 虚拟机(target vm)之间的通信协议。 JDWP 协议介绍 这里首先要说明一下 debugger 和 target vm。Target vm 中运行…...
利用ADS建立MIPI D-PHY链路仿真流程
根据MIPI D-PHY v1.2规范中对于互连电气参数的定义,本次仿真实例中,需要重点关注如下的设计参数: 1. 差分信号的插入损耗Sddij和回拨损耗Sddii; 2. 模式转换损耗Sdcxx、Scdxx; 3. 数据线与时钟线之间的串扰耦合(远、近端)。 设计者还可以结合CTS中的补充…...
【大根堆】【C++算法】871 最低加油次数
作者推荐 【动态规划】【map】【C算法】1289. 下降路径最小和 II 本文涉及知识点 大根堆 优先队列 LeetCode:871最低加油次数 汽车从起点出发驶向目的地,该目的地位于出发位置东面 target 英里处。 沿途有加油站,用数组 stations 表示。其中 statio…...
SpringBoot的自动装配原理
一、SpringBootConfiguration注解的作用 SpringBootApplication注解是SpringBoot项目的核心注解,加在启动引导类上。点击进去可以发现SpringBootApplication注解是一个组合注解。其中SpringBootConfiguration和EnableAutoConfiguration是由Spring提供的,剩下的注解是由JDK提供的…...
嵌入式驱动开发需要会哪些技能?
嵌入式驱动开发是指在嵌入式系统中编写驱动程序,实现设备与计算机之间的通信。嵌入式驱动开发是指编写设备驱动程序,实现设备与计算机之间的通信。以下是一些嵌入式驱动开发的具体操作方法: 1)了解硬件设备结构:在进行嵌入式驱动…...
Leetcode:二分搜索树层次遍历
题目: 给你二叉树的根节点 root ,返回其节点值的 层序遍历 。 (即逐层地,从左到右访问所有节点)。 示例: 示例 1: 输入:root [3,9,20,null,null,15,7] 输出:[[3],[9,…...
【fabric.js】toDataURL 性能问题、优化
必要解释:最好看完。。省流版的话,toDataURL 的 multiplier参数不要设置超过500; 情景:在做某些功能的时候涉及到图形的预览,预览的时候是导出为40*40 像素的图片,当碰到某些图形非常小的时候,…...
基于Grafana+Prometheus搭建可视化监控系统实践
基本介绍 Grafana:一个监控仪表系统,可以根据提供的监控数据,生产可视化仪表盘,同时也具有告警通知功能。这里的监控数据来源,目前主要以Prometheus为主(也支持其它数据源),每次展现…...
选择排序(堆排序和topK问题)
选择排序 每一次从待排序的数据元素中选出最小(或最大)的一个元素,存放在序列的起始位置,直到全部待排序的数据元素排完 。 如果我们用扑克牌来举例,那么选择排序就像是提前已经把所有牌都摸完了,而再进行牌…...
webpack tree shaking 摇树原理
Tree-shaking 是指在打包过程中通过静态分析,识别并删除未使用的代码,以减小最终输出文件的大小。Webpack 通过内置的 UglifyJS 插件或者 Terser 插件来实现 Tree-shaking。下面是简要的 webpack Tree-shaking 的原理: 标记未使用的代码&…...
开源模型应用落地-业务整合篇(三)
一、前言 在之前的两篇文章中,我们学习了如何构建基本的即时消息(IM)功能。今天,我们将进一步将IM模块与AI服务进行连接,实现用户提问并由模型进行回答,最后将结果展示在用户界面上。 二、术语 2.1. Spring Boot 是一个用于快速构建基于Spring框架的Java应用程序的开源框…...
js打地鼠
文章目录 1实现效果2代码实现 1实现效果 游戏难度:简单,一般,困难,噩梦(控制setInterval的time参数) 按钮功能:结束(可以通过修改gameScore的值来修改判定结束的分数)&am…...
计算机网络体系架构认知--网络协议栈
文章目录 一.计算机网络分层架构各协议层和计算机系统的联系从整体上理解计算机网络通信计算机网络通信的本质 二.Mac地址,IP地址和进程端口号三.局域网通信与跨局域网通信局域网通信跨局域网通信全球互联的通信脉络 四.网络编程概述 一.计算机网络分层架构 实现计算机长距离网…...
Ubuntu 22.04 安装tomcat
tomcat是常用的Java服务容器,这篇文章我们就来讲讲如何安装它。 更新软件包 首先是更新软件包,这是最常规的操作 sudo apt update 然后是开始安装,不多一会就可以安装好了 sudo apt install tomcat9 然后看一下状态 sudo systemctl status tomcat9 发现虽然启动了,但…...
记录:Ubuntu 18.04 X86 上通过CMake 指定编译器工具链交叉编译。
最好是通过 cmake 命令行来设置,要不然你只有在 CMakeFiles.txt 里面自己写判断语句了。 要用 cmake 交叉编译,必须设置连接器,要不然会使用当前系统的 ld,就是 /usr/bin/ld。 但是其它平台是不会ld上的,elf格式都不…...
requests,js逆向练习
自上而下排除jquery源码,点进去utils 发现第一次请求是getTime 再次运行此断点才是登录,这个时候密码已经被加密了 查看上级js页面,发现加密函数 进去看函数加密过程 得到结果RSA python代码 import base64 import jsonimport requests f…...
【Linux】shell脚本忽略错误继续执行
在 shell 脚本中,可以使用 set -e 命令来设置脚本在遇到错误时退出执行。如果你希望脚本忽略错误并继续执行,可以在脚本开头添加 set e 命令来取消该设置。 举例1 #!/bin/bash# 取消 set -e 的设置 set e# 执行命令,并忽略错误 rm somefile…...
三维GIS开发cesium智慧地铁教程(5)Cesium相机控制
一、环境搭建 <script src"../cesium1.99/Build/Cesium/Cesium.js"></script> <link rel"stylesheet" href"../cesium1.99/Build/Cesium/Widgets/widgets.css"> 关键配置点: 路径验证:确保相对路径.…...
8k长序列建模,蛋白质语言模型Prot42仅利用目标蛋白序列即可生成高亲和力结合剂
蛋白质结合剂(如抗体、抑制肽)在疾病诊断、成像分析及靶向药物递送等关键场景中发挥着不可替代的作用。传统上,高特异性蛋白质结合剂的开发高度依赖噬菌体展示、定向进化等实验技术,但这类方法普遍面临资源消耗巨大、研发周期冗长…...
前端导出带有合并单元格的列表
// 导出async function exportExcel(fileName "共识调整.xlsx") {// 所有数据const exportData await getAllMainData();// 表头内容let fitstTitleList [];const secondTitleList [];allColumns.value.forEach(column > {if (!column.children) {fitstTitleL…...
用docker来安装部署freeswitch记录
今天刚才测试一个callcenter的项目,所以尝试安装freeswitch 1、使用轩辕镜像 - 中国开发者首选的专业 Docker 镜像加速服务平台 编辑下面/etc/docker/daemon.json文件为 {"registry-mirrors": ["https://docker.xuanyuan.me"] }同时可以进入轩…...
【笔记】WSL 中 Rust 安装与测试完整记录
#工作记录 WSL 中 Rust 安装与测试完整记录 1. 运行环境 系统:Ubuntu 24.04 LTS (WSL2)架构:x86_64 (GNU/Linux)Rust 版本:rustc 1.87.0 (2025-05-09)Cargo 版本:cargo 1.87.0 (2025-05-06) 2. 安装 Rust 2.1 使用 Rust 官方安…...
Netty从入门到进阶(二)
二、Netty入门 1. 概述 1.1 Netty是什么 Netty is an asynchronous event-driven network application framework for rapid development of maintainable high performance protocol servers & clients. Netty是一个异步的、基于事件驱动的网络应用框架,用于…...
华为OD最新机试真题-数组组成的最小数字-OD统一考试(B卷)
题目描述 给定一个整型数组,请从该数组中选择3个元素 组成最小数字并输出 (如果数组长度小于3,则选择数组中所有元素来组成最小数字)。 输入描述 行用半角逗号分割的字符串记录的整型数组,0<数组长度<= 100,0<整数的取值范围<= 10000。 输出描述 由3个元素组成…...
【若依】框架项目部署笔记
参考【SpringBoot】【Vue】项目部署_no main manifest attribute, in springboot-0.0.1-sn-CSDN博客 多一个redis安装 准备工作: 压缩包下载:http://download.redis.io/releases 1. 上传压缩包,并进入压缩包所在目录,解压到目标…...
【51单片机】4. 模块化编程与LCD1602Debug
1. 什么是模块化编程 传统编程会将所有函数放在main.c中,如果使用的模块多,一个文件内会有很多代码,不利于组织和管理 模块化编程则是将各个模块的代码放在不同的.c文件里,在.h文件里提供外部可调用函数声明,其他.c文…...
