網站首頁 編程語言 正文
本文實例為大家分享了opencv實現圖像校正的具體代碼,供大家參考,具體內容如下
1.引言:python實現傾斜圖像校正操作
2.思路流程:
(1)讀入,灰度化;
(2)高斯模糊;
(3)二值化圖像;
(4)閉開操作;
(5)獲取圖像頂點;
(6)旋轉校正
3.實現代碼:
import cv2
import numpy as np
import imutils
import time
def Img_Outline(img_path):
? ? original_img = cv2.imread(img_path)
? ? gray_img = cv2.cvtColor(original_img, cv2.COLOR_BGR2GRAY)
? ? blurred = cv2.GaussianBlur(gray_img, (9, 9), 0) ? ? ? ? ? ? ? ? ? ? # 高斯模糊去噪(設定卷積核大小影響效果)
? ? _, RedThresh = cv2.threshold(blurred, 165, 255, cv2.THRESH_BINARY) ?# 設定閾值165(閾值影響開閉運算效果)
? ? kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5)) ? ? ? ? ?# 定義矩形結構元素
? ? closed = cv2.morphologyEx(RedThresh, cv2.MORPH_CLOSE, kernel) ? ? ? # 閉運算(鏈接塊)
? ? opened = cv2.morphologyEx(closed, cv2.MORPH_OPEN, kernel) ? ? ? ? ? # 開運算(去噪點)
? ? return original_img, opened
def findContours_img(original_img, opened):
? ? contours = cv2.findContours(opened, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
? ? cnts = imutils.grab_contours(contours)
? ? # print(cnts)
? ? # c = sorted(cnts, key=cv2.contourArea, reverse=True)[0] ? ? ? ? ?# 計算最大輪廓的旋轉包圍盒
? ? c = max(cnts, key=cv2.contourArea)
? ? rect = cv2.minAreaRect(c)
? ? # print(rect)
? ? angle = rect[2] ?# rect[2] 返回的是矩形的旋轉角度
? ? print("angle", angle)
? ? if angle == 90.0:
? ? ? ? return original_img, original_img
? ? else:
? ? ? ? box = np.int0(cv2.boxPoints(rect))
? ? ? ? draw_img = cv2.drawContours(original_img.copy(), [box], -1, (0, 0, 255), 3)
? ? ? ? rows, cols = original_img.shape[:2]
? ? ? ? M = cv2.getRotationMatrix2D((cols / 2, rows / 2), angle, 1)
? ? ? ? result_img = cv2.warpAffine(original_img, M, (cols, rows))
? ? ? ? return result_img,draw_img
if __name__ == "__main__":
? ? img_path = './result.jpg'
? ? start_time = time.time()
? ? original_img, opened = Img_Outline(img_path)
? ? result_img,draw_img = findContours_img(original_img,opened)
? ? print('消耗的時間為:',(time.time() - start_time))
? ? cv2.imshow("original_img", original_img)
? ? cv2.imshow("draw_img", draw_img)
? ? cv2.imshow("result_img", result_img)
? ? cv2.waitKey(0)
? ? cv2.destroyAllWindows()
4.效果展示:
原圖
標框出圖
旋轉后的圖
原文鏈接:https://blog.csdn.net/weixin_44312422/article/details/124878729
相關推薦
- 2023-02-15 PyQt5頁面跳轉問題及解決方式_python
- 2022-03-28 用python實現超強的加密軟件_python
- 2022-08-14 在WPF中使用多線程更新UI_C#教程
- 2021-12-02 C/C++?Qt數據庫SqlRelationalTable關聯表詳解_C 語言
- 2022-10-26 jQuery中DOM?屬性使用實例詳解下篇_jquery
- 2022-08-05 Redis實現分布式鎖的五種方法詳解_Redis
- 2022-11-17 淺析golang的依賴注入_Golang
- 2022-10-08 ASP.NET泛型一之泛型簡介與基本語法_實用技巧
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支