網(wǎng)站首頁 編程語言 正文
本文實例為大家分享了opencv實現(xiàn)圖像校正的具體代碼,供大家參考,具體內(nèi)容如下
1.引言:python實現(xiàn)傾斜圖像校正操作
2.思路流程:
(1)讀入,灰度化;
(2)高斯模糊;
(3)二值化圖像;
(4)閉開操作;
(5)獲取圖像頂點;
(6)旋轉(zhuǎn)校正
3.實現(xiàn)代碼:
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) ? ? ? ? ? ? ? ? ? ? # 高斯模糊去噪(設(shè)定卷積核大小影響效果)
? ? _, RedThresh = cv2.threshold(blurred, 165, 255, cv2.THRESH_BINARY) ?# 設(shè)定閾值165(閾值影響開閉運算效果)
? ? kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5)) ? ? ? ? ?# 定義矩形結(jié)構(gòu)元素
? ? 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] ? ? ? ? ?# 計算最大輪廓的旋轉(zhuǎn)包圍盒
? ? c = max(cnts, key=cv2.contourArea)
? ? rect = cv2.minAreaRect(c)
? ? # print(rect)
? ? angle = rect[2] ?# rect[2] 返回的是矩形的旋轉(zhuǎn)角度
? ? 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.效果展示:
原圖
標(biāo)框出圖
旋轉(zhuǎn)后的圖
原文鏈接:https://blog.csdn.net/weixin_44312422/article/details/124878729
相關(guān)推薦
- 2022-03-30 Android實現(xiàn)屏幕保持常亮功能_Android
- 2022-09-04 使用Python去除小數(shù)點后面多余的0問題_python
- 2022-04-08 從頭學(xué)習(xí)C語言之for語句和循環(huán)嵌套_C 語言
- 2022-04-22 wampserver You don‘t have permission to access / o
- 2022-09-25 edge或谷歌瀏覽器打開默認(rèn)是百度或其他,怎么修改成自己想要的頁面
- 2022-07-23 Python實現(xiàn)環(huán)形鏈表_python
- 2022-09-08 go語言中函數(shù)與方法介紹_Golang
- 2022-07-18 linux系統(tǒng)安全和應(yīng)用
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支