ocr透視變換

2021-10-07 08:23:12 字數 4259 閱讀 1478

123

4567

891011

1213

1415

1617

1819

2021

2223

2425

2627

2829

3031

3233

3435

3637

3839

4041

4243

4445

4647

4849

5051

5253

5455

5657

5859

6061

6263

6465

6667

6869

7071

7273

7475

7677

7879

8081

8283

8485

8687

8889

9091

9293

9495

9697

9899

100101

102103

104105

106107

108109

110111

112113

114115

116117

118119

120121

122123

124125

126127

128

# 匯入工具包

import numpy as np

import argparse

import cv2

# 設定引數

# 一共4個座標點

rect = np.zeros((4, 2), dtype = "float32")

# 按順序找到對應座標0123分別是 左上,右上,右下,左下

# 計算左上,右下

s = pts.sum(axis = 1)

rect[0] = pts[np.argmin(s)]

rect[2] = pts[np.argmax(s)]

# 計算右上和左下

diff = np.diff(pts, axis = 1)

rect[1] = pts[np.argmin(diff)]

rect[3] = pts[np.argmax(diff)]

return rect

def four_point_transform(image, pts):

# 獲取輸入座標點

rect = order_points(pts)

(tl, tr, br, bl) = rect

# 計算輸入的w和h值

widtha = np.sqrt(((br[0] - bl[0]) ** 2) + ((br[1] - bl[1]) ** 2))

widthb = np.sqrt(((tr[0] - tl[0]) ** 2) + ((tr[1] - tl[1]) ** 2))

maxwidth = max(int(widtha), int(widthb))

heighta = np.sqrt(((tr[0] - br[0]) ** 2) + ((tr[1] - br[1]) ** 2))

heightb = np.sqrt(((tl[0] - bl[0]) ** 2) + ((tl[1] - bl[1]) ** 2))

maxheight = max(int(heighta), int(heightb))

# 變換後對應座標位置

dst = np.array([

[0, 0],

[maxwidth - 1, 0],

[maxwidth - 1, maxheight - 1],

[0, maxheight - 1]], dtype = "float32")

# 計算變換矩陣

m = cv2.getperspectivetransform(rect, dst)

warped = cv2.warpperspective(image, m, (maxwidth, maxheight))

# 返回變換後結果

return warped

def resize(image, width=none, height=none, inter=cv2.inter_area):

dim = none

(h, w) = image.shape[:2]

if width is none and height is none:

return image

if width is none:

r = height / float(h)

dim = (int(w * r), height)

else:

r = width / float(w)

dim = (width, int(h * r))

resized = cv2.resize(image, dim, interpolation=inter)

return resized

# 讀取輸入

image = cv2.imread(args["image"])

#座標也會相同變化

ratio = image.shape[0] / 500.0

orig = image.copy()

image = resize(orig, height = 500)

# 預處理

gray = cv2.cvtcolor(image, cv2.color_bgr2gray)

gray = cv2.gaussianblur(gray, (5, 5), 0)

edged = cv2.canny(gray, 75, 200)

# 展示預處理結果

print("step 1: 邊緣檢測")

cv2.imshow("image", image)

cv2.imshow("edged", edged)

cv2.waitkey(0)

cv2.destroyallwindows()

# 輪廓檢測

cnts = sorted(cnts, key = cv2.contourarea, reverse = true)[:5]

# 遍歷輪廓

for c in cnts:

# 計算輪廓近似

peri = cv2.arclength(c, true)

# c表示輸入的點集

# epsilon表示從原始輪廓到近似輪廓的最大距離,它是乙個準確度引數

# true表示封閉的

# 4個點的時候就拿出來

break

# 展示結果

print("step 2: 獲取輪廓")

cv2.drawcontours(image, [screencnt], -1, (0, 255, 0), 2)

cv2.imshow("outline", image)

cv2.waitkey(0)

cv2.destroyallwindows()

# 透視變換

warped = four_point_transform(orig, screencnt.reshape(4, 2) * ratio)

# 二值處理

# 展示結果

print("step 3: 變換")

cv2.imshow("original", resize(orig, height = 650))

cv2.imshow("scanned", resize(ref, height = 650))

cv2.waitkey(0)

opencv透視變換

1 目前效果最好的乙個 但還是紙牌能檢測出來 2 下面將我修改執行成功的 貼出,至於優化,提高識別四邊形準確度,還需要繼續研究。透視變換,檢測四邊形,有時候容易檢測不出 但目前效果最好的就是該程式 include stdafx.h include core core.hpp include incl...

透視變換(高階)

透視變換比仿射變換更普遍。它們不一定保持線條之間的 平行性 但是因為它們更普遍,也更實用,幾乎所有在日常影象中遇到的變換都是透視變換。有沒有想過為什麼兩條軌道似乎在遠處會相遇?這是因為您的眼睛中的對影象好比做了透視變換,透視變換不一定保持平行線平行。如果你站在上面觀察圖1.1中的鐵軌,他們似乎根本不...

Bitmap透視變換

import flash.geom.var photo sprite new sprite addchild photo img為繼承於bitmapdata的類 var bitmapsource new img 400,300 var showline true var inbitmapwidth ...