[使用霍夫变换将板变换为水平

问题描述 投票:0回答:1

我正在尝试变换非水平的图像,因为它们可能会倾斜。

事实证明,当测试2张图像时,这张照片是水平的,而这张照片不是水平的。使用水平照片可以给我很好的效果,但是当尝试更改倾斜的第二张照片时,它并没有达到预期的效果。

[fist image]的工作原理如下,带有theta 1.6406095。目前,它看起来很糟糕,因为我正在尝试使两张照片在水平方向上看起来正确。

enter image description here

second image说theta只是1.9198622

enter image description here

我认为这是此行的错误:

lines= cv2.HoughLines(edges, 1, np.pi/90.0, 60, np.array([]))

我对此link with colab进行了一些模拟。

欢迎任何帮助。

python-3.x opencv google-colaboratory hough-transform houghlines
1个回答
0
投票

到目前为止,这就是我所得到的。

import cv2
import numpy as np

img=cv2.imread('test.jpg',1)
imgGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
imgBlur=cv2.GaussianBlur(imgGray,(5,5),0)
imgCanny=cv2.Canny(imgBlur,90,200)
contours,hierarchy =cv2.findContours(imgCanny,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)

rectCon=[]
for cont in contours:
    area=cv2.contourArea(cont)
    if area >100:
        #print(area) #prints all the area of the contours
        peri=cv2.arcLength(cont,True)
        approx=cv2.approxPolyDP(cont,0.01*peri,True)
        #print(len(approx)) #prints the how many corner points does the contours have
        if len(approx)==4:
            rectCon.append(cont)
            #print(len(rectCon))

rectCon=sorted(rectCon,key=cv2.contourArea,reverse=True) # Sort out the contours based on largest area to smallest


bigPeri=cv2.arcLength(rectCon[0],True)
cornerPoints=cv2.approxPolyDP(rectCon[0],0.01*peri,True)

# Reorder bigCornerPoints so I can prepare it for warp transform (bird eyes view)
cornerPoints=cornerPoints.reshape((4,2))
mynewpoints=np.zeros((4,1,2),np.int32)
add=cornerPoints.sum(1)

mynewpoints[0]=cornerPoints[np.argmin(add)]
mynewpoints[3]=cornerPoints[np.argmax(add)]
diff=np.diff(cornerPoints,axis=1)
mynewpoints[1]=cornerPoints[np.argmin(diff)]
mynewpoints[2]=cornerPoints[np.argmax(diff)]

# Draw my corner points 
#cv2.drawContours(img,mynewpoints,-1,(0,0,255),10)

##cv2.imshow('Corner Points in Red',img)
##print(mynewpoints)

# Bird Eye view of your region of interest
pt1=np.float32(mynewpoints) #What are your corner points
pt2=np.float32([[0,0],[300,0],[0,200],[300,200]]) 
matrix=cv2.getPerspectiveTransform(pt1,pt2) 
imgWarpPers=cv2.warpPerspective(img,matrix,(300,200)) 
cv2.imshow('Result',imgWarpPers)

Result

现在,您只需要解决倾斜问题(opencv有歪斜),然后使用一些阈值来检测字母,然后识别每个字母。

© www.soinside.com 2019 - 2024. All rights reserved.