opencv+python實(shí)現(xiàn)圖像矯正
本文實(shí)例為大家分享了opencv+python實(shí)現(xiàn)圖像矯正的具體代碼,供大家參考,具體內(nèi)容如下
需求:將斜著拍攝的文本圖像進(jìn)行矯正
python代碼
import numpy as np
import cv2 as cv
def shape_correction(img):
? ? (height, width) = img.shape[:2]
? ? print(img.shape)
? ? img_gau = cv.GaussianBlur(img, (5, 5), 0)
? ? canny = cv.Canny(img_gau, 60, 200)
? ? # cv.imshow("g-canny", canny)
? ??
? ? kernel = cv.getStructuringElement(cv.MORPH_CROSS, (4,3))?
? ??
? ? dilated = cv.dilate(canny, kernel, iterations=8) ?
? ? # cv.imshow('img_dilated', dilated)
? ? # 尋找輪廓
? ? contours, hierarchy = cv.findContours(dilated, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_NONE)
? ? # print(len(contours), hierarchy, sep='\n')
? ? # 找到最外層面積最大的輪廓
? ? area = 0
? ? # print("area:{}".format(area))
? ? index = 0
? ? for i in range(len(contours)):
? ? ? ? x, y, w, h = cv.boundingRect(contours[i])
? ? ? ? # 排除非文本區(qū)域
? ? ? ? if w < 35 and h < 35:
? ? ? ? ? ? continue
? ? ? ? # 防止矩形區(qū)域過(guò)大不精準(zhǔn) ? ?
? ? ? ? if h > 0.99 * height or w > 0.99 * width:
? ? ? ? ? ? continue
? ? ? ? # draw rectangle around contour on original image
? ? ? ? # cv.rectangle(img, (x, y), (x + w, y + h), (255, 0, 255), 2)
? ? ? ? tmpArea = w * h
? ? ? ? if tmpArea >= area:
? ? ? ? ? ? area = tmpArea
? ? ? ? ? ? index = i
? ? # 得到最小外接矩形的(中心(x,y), (寬,高), 旋轉(zhuǎn)角度)
? ? rect = cv.minAreaRect(contours[index])
? ? # 畫出矩形框
? ? # box = cv.boxPoints(rect)
? ? # box = np.int0(box)
? ? # cv.drawContours(img, [box], 0, (0, 0, 255), 2)
? ? # cv.imshow('img', img)
? ? print("rect:{}".format(rect))
? ? angle = rect[-1]
? ? # print(angle)
? ? # 角度大于85度或小于5度不矯正
? ? if angle > 85 or angle < 5:
? ? ? ? angle = 0
? ? elif angle < 45:
? ? ? ? angle = angle - 0
? ? else:
? ? ? ? angle = angle - 90
? ? M = cv.getRotationMatrix2D(rect[0], angle, 1)
? ? rotated = cv.warpAffine(img, M, (width, height), flags=cv.INTER_CUBIC, borderValue=(255, 255, 255))
? ??
? ? cv.imshow('Rotated', rotated)
? ? return rotated
src = cv.imread('/res-normal.png', 0)
rotated = shape_correction(src)
cv.waitKey(0)算法流程

算法核心思想:
獲取圖像中的文本區(qū)域矩形輪廓,找到其中面積最大的,對(duì)其進(jìn)行最小外接矩形計(jì)算,得到最小外接矩形的旋轉(zhuǎn)角度,再根據(jù)旋轉(zhuǎn)角度進(jìn)行仿射變換。
測(cè)試效果

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python實(shí)現(xiàn)將MySQL數(shù)據(jù)庫(kù)表中的數(shù)據(jù)導(dǎo)出生成csv格式文件的方法
這篇文章主要介紹了Python實(shí)現(xiàn)將MySQL數(shù)據(jù)庫(kù)表中的數(shù)據(jù)導(dǎo)出生成csv格式文件的方法,涉及Python針對(duì)mysql數(shù)據(jù)庫(kù)的連接、查詢、csv格式數(shù)據(jù)文件的生成等相關(guān)操作技巧,需要的朋友可以參考下2018-01-01
python編寫一個(gè)會(huì)算賬的腳本的示例代碼
這篇文章主要介紹了python編寫一個(gè)會(huì)算賬的腳本,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06
python selenium 查找隱藏元素 自動(dòng)播放視頻功能
這篇文章主要介紹了python selenium 查找隱藏元素 自動(dòng)播放視頻功能,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-07-07
使用python連接mysql數(shù)據(jù)庫(kù)數(shù)據(jù)方式
這篇文章主要介紹了使用python連接mysql數(shù)據(jù)庫(kù)數(shù)據(jù)方式,住喲有兩種方式,具體內(nèi)容,需要的小伙伴可以參考下面文章內(nèi)容,希望對(duì)你有所幫助2022-03-03
Django集成MongoDB實(shí)現(xiàn)過(guò)程解析
這篇文章主要介紹了Django集成MongoDB實(shí)現(xiàn)過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-12-12
python3對(duì)接mysql數(shù)據(jù)庫(kù)實(shí)例詳解
這篇文章主要介紹了python3對(duì)接mysql數(shù)據(jù)庫(kù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
Windows下Anaconda和PyCharm的安裝與使用詳解
這篇文章主要介紹了Windows下Anaconda和PyCharm的安裝與使用詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04

