opencv實(shí)現(xiàn)圖像校正
本文實(shí)例為大家分享了opencv實(shí)現(xiàn)圖像校正的具體代碼,供大家參考,具體內(nèi)容如下
1.引言:python實(shí)現(xiàn)傾斜圖像校正操作
2.思路流程:
(1)讀入,灰度化;
(2)高斯模糊;
(3)二值化圖像;
(4)閉開操作;
(5)獲取圖像頂點(diǎn);
(6)旋轉(zhuǎn)校正
3.實(shí)現(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(閾值影響開閉運(yùn)算效果) ? ? kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5)) ? ? ? ? ?# 定義矩形結(jié)構(gòu)元素 ? ? closed = cv2.morphologyEx(RedThresh, cv2.MORPH_CLOSE, kernel) ? ? ? # 閉運(yùn)算(鏈接塊) ? ? opened = cv2.morphologyEx(closed, cv2.MORPH_OPEN, kernel) ? ? ? ? ? # 開運(yùn)算(去噪點(diǎn)) ? ? 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] ? ? ? ? ?# 計(jì)算最大輪廓的旋轉(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)后的圖
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
基于Python實(shí)現(xiàn)智能停車場車牌識別計(jì)費(fèi)系統(tǒng)
這篇文章主要為大家介紹了如何利用Python實(shí)現(xiàn)一個智能停車場車牌識別計(jì)費(fèi)系統(tǒng),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以動手嘗試一下2022-04-04python實(shí)現(xiàn)支持目錄FTP上傳下載文件的方法
這篇文章主要介紹了python實(shí)現(xiàn)支持目錄FTP上傳下載文件的方法,適用于windows及Linux平臺FTP傳輸文件及文件夾,需要的朋友可以參考下2015-06-06tensorflow實(shí)現(xiàn)簡單的卷積網(wǎng)絡(luò)
這篇文章主要為大家詳細(xì)介紹了tensorflow實(shí)現(xiàn)簡單的卷積網(wǎng)絡(luò),使用的數(shù)據(jù)集是MNIST,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-05-05Python設(shè)計(jì)模式結(jié)構(gòu)型享元模式
這篇文章主要介紹了Python享元模式,享元模式即Flyweight Pattern,指運(yùn)用共享技術(shù)有效地支持大量細(xì)粒度的對象,下面和小編一起進(jìn)入文章了解更多詳細(xì)內(nèi)容吧2022-02-02Python中自定義函方法與參數(shù)具有默認(rèn)值的函數(shù)
這篇文章主要介紹了Python中自定義函方法與參數(shù)具有默認(rèn)值的函數(shù),在Python編程中,可以使用已經(jīng)定義好的函數(shù),也可以自定義函數(shù)實(shí)現(xiàn)某些特殊的功能,更多相關(guān)資料,請需要的人參考下面文章內(nèi)容2022-02-02