欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python?Opencv基于透視變換的圖像矯正

 更新時(shí)間:2022年01月23日 10:14:20   作者:Python之魂  
這篇文章主要為大家詳細(xì)介紹了Python?Opencv基于透視變換的圖像矯正,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Python Opencv基于透視變換的圖像矯正,供大家參考,具體內(nèi)容如下

一、自動(dòng)獲取圖像頂點(diǎn)變換(獲取圖像輪廓頂點(diǎn)矯正)

圖像旋轉(zhuǎn)校正思路如下

1、以灰度圖讀入
2、腐蝕膨脹,閉合等操作
3、二值化圖像
4、獲取圖像頂點(diǎn)
5、透視矯正

#(基于透視的圖像矯正)
import cv2
import math
import numpy as np

def Img_Outline(input_dir):
? ? original_img = cv2.imread(input_dir)
? ? 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, gray_img, RedThresh, closed, opened


def findContours_img(original_img, opened):
? ? image, contours, hierarchy = cv2.findContours(opened, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
? ? c = sorted(contours, key=cv2.contourArea, reverse=True)[1] ? # 計(jì)算最大輪廓的旋轉(zhuǎn)包圍盒
? ? rect = cv2.minAreaRect(c) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?# 獲取包圍盒(中心點(diǎn),寬高,旋轉(zhuǎn)角度)
? ? box = np.int0(cv2.boxPoints(rect)) ? ? ? ? ? ? ? ? ? ? ? ? ? # box
? ? draw_img = cv2.drawContours(original_img.copy(), [box], -1, (0, 0, 255), 3)

? ? print("box[0]:", box[0])
? ? print("box[1]:", box[1])
? ? print("box[2]:", box[2])
? ? print("box[3]:", box[3])
? ? return box,draw_img

def Perspective_transform(box,original_img):
? ? # 獲取畫框?qū)捀?x=orignal_W,y=orignal_H)
? ? orignal_W = math.ceil(np.sqrt((box[3][1] - box[2][1])**2 + (box[3][0] - box[2][0])**2))
? ? orignal_H= math.ceil(np.sqrt((box[3][1] - box[0][1])**2 + (box[3][0] - box[0][0])**2))

? ? # 原圖中的四個(gè)頂點(diǎn),與變換矩陣
? ? pts1 = np.float32([box[0], box[1], box[2], box[3]])
? ? pts2 = np.float32([[int(orignal_W+1),int(orignal_H+1)], [0, int(orignal_H+1)], [0, 0], [int(orignal_W+1), 0]])

? ? # 生成透視變換矩陣;進(jìn)行透視變換
? ? M = cv2.getPerspectiveTransform(pts1, pts2)
? ? result_img = cv2.warpPerspective(original_img, M, (int(orignal_W+3),int(orignal_H+1)))

? ? return result_img

if __name__=="__main__":
? ? input_dir = "../staticimg/oldimg_04.jpg"
? ? original_img, gray_img, RedThresh, closed, opened = Img_Outline(input_dir)
? ? box, draw_img = findContours_img(original_img,opened)
? ? result_img = Perspective_transform(box,original_img)
? ? cv2.imshow("original", original_img)
? ? cv2.imshow("gray", gray_img)
? ? cv2.imshow("closed", closed)
? ? cv2.imshow("opened", opened)
? ? cv2.imshow("draw_img", draw_img)
? ? cv2.imshow("result_img", result_img)

? ? cv2.waitKey(0)
? ? cv2.destroyAllWindows()

直接變換

1、獲取圖像四個(gè)頂點(diǎn)
2、形成變換矩陣
3、透視變換

import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread('original_img.jpg')
H_rows, W_cols= img.shape[:2]
print(H_rows, W_cols)

# 原圖中書本的四個(gè)角點(diǎn)(左上、右上、左下、右下),與變換后矩陣位置
pts1 = np.float32([[161, 80], [449, 12], [1, 430], [480, 394]])
pts2 = np.float32([[0, 0],[W_cols,0],[0, H_rows],[H_rows,W_cols],])

# 生成透視變換矩陣;進(jìn)行透視變換
M = cv2.getPerspectiveTransform(pts1, pts2)
dst = cv2.warpPerspective(img, M, (500,470))

"""
注釋代碼同效
# img[:, :, ::-1]是將BGR轉(zhuǎn)化為RGB
# plt.subplot(121), plt.imshow(img[:, :, ::-1]), plt.title('input')
# plt.subplot(122), plt.imshow(dst[:, :, ::-1]), plt.title('output')
# plt.show
"""

cv2.imshow("original_img",img)
cv2.imshow("result",dst)
cv2.waitKey(0)
cv2.destroyAllWindows()

兩次透視變換

def get_warp_perspective(img, width, height, array_points, array_points_get, array_points_warp):
? ? middle_len = 268
? ? # rows, cols = img.shape[:2]
? ? # D_value1 = (middle_len - array_points_get[0][1])*2+((middle_len - array_points_get[0][1])//3)
? ? # D_value2 = (middle_len - array_points_get[1][1])*2+((middle_len - array_points_get[1][1])//3)
? ? D_value1 = 0
? ? D_value2 = 0
? ? # 原圖中的四個(gè)角點(diǎn)
? ? # pts1 = np.float32([[0, 249],[512, 253],[0, 512], [512, 512]])#重要的測(cè)試1和2
? ? pts1 = np.float32(array_points_get)#重要的測(cè)試1和2

? ? # pts2 = np.float32([[0, middle_len], [width, middle_len], [0, height], [width, height]])#重要的測(cè)試1和2
? ? # pts2 = np.float32([[0, middle_len],[0, height] , [width, height],[width, middle_len]])#重要的測(cè)試1和2
? ? pts2 = np.float32([[0, 0],[0, middle_len] , [width, middle_len],[width, 0]])#重要的測(cè)試1和2

? ? # 生成透視變換矩陣
? ? M = cv2.getPerspectiveTransform(pts1, pts2)
? ? # 進(jìn)行透視變換
? ? dst = cv2.warpPerspective(img, M, (width, height))
? ? # # 保存圖片,僅用于測(cè)試
? ? img_path = './cut_labels/cut_image_one.jpg'
? ? cv2.imwrite(img_path, dst)

? ? return warp_perspective(dst, width, height,array_points,array_points_warp,middle_len, D_value1, D_value2)


def warp_perspective(dst, width, height,array_points,array_points_warp,middle_len, D_value1, D_value2):
? ? # new_img_path = img_path
? ? # img = cv2.imread(new_img_path)
? ? # 原圖的保存地址
? ? # rows, cols = img.shape[:2]

? ? # 原圖中的四個(gè)角點(diǎn)
? ? # pts3 = np.float32([[0, 268], [0, 44], [512,35], [512, 268]])#重要測(cè)試1
? ? # pts3 = np.float32([[0, middle_len], [0, D_value1], [512,D_value2], [512, middle_len]])#重要測(cè)試1
? ? pts3 = np.float32([[0, 0], [0, height], [width, height], [width, 0]])
? ? # pts3 = np.float32([[0, middle_len], [0, D_value1], [512,D_value2], [512, middle_len]])#重要測(cè)試1
? ? # pts3 = np.float32([[0, 512], [0, array_points[1][1]], [512,512], [512, middle_len]])#重要測(cè)試1
? ? # 變換后的四個(gè)角點(diǎn)
? ? pts4 = np.float32([[0, 0], [0, height-D_value1], [width, height-D_value2], [width, 0]])#重要測(cè)試1
? ? # pts4 = np.float32([[0, 268], [0, 0], [512, 0], [512, 268]])#重要測(cè)試1
? ? # 生成透視變換矩陣
? ? M = cv2.getPerspectiveTransform(pts3, pts4)
? ? # 進(jìn)行透視變換
? ? dst_img = cv2.warpPerspective(dst, M, (width, height))
? ? # #保存最終圖片,僅用于測(cè)試
? ? print("++++++++++++++++")
? ? final_img_path = './cut_labels/cut_image_two.jpg'
? ? cv2.imwrite(final_img_path, dst_img)
? ? # 進(jìn)行透視變換
? ? return cv2.warpPerspective(dst_img, M, (width, height))
? ? # return output_warp_perspective(img, width, height, array_points, array_points_get, array_points_warp)

if __name__ ?== "__main__":
?? ?# 透視轉(zhuǎn)換
? ?? ? img = cv2.imread('../staticimg/oldimg_04.jpg')
? ? ?dst = get_warp_perspective(img, 512, 512, array_points=[[395.2, 75.0], [342, 517], [1000, 502], [900, 75]])
? ? ?cv2.imwrite('aaa2.jpg', dst)
? ? ?cv2.imshow('title', dst)
? ? ?cv2.waitKey(0)
? ? ?imgrectificate = imgRectificate(img, width, height, array_points)
? ? ?imgrectificate.warp_perspective()

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論