Python:实现文本校正算法

2024-09-29 17:18:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试拉直灰度硬币图像上的文本(我对其使用了Canny边缘检测,因此它看起来类似于:https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQaaRPYtqZuy-o-zajZCWxmPDGBRbUJkTPmbMhIL9ex4gd-JNWjxA&s),并需要在python中实现以下等式:

给定圆方程p(x,y),假设p'(x',y')是原始图像的像素,p'(x',y')是矫直图像的新像素,计算公式如下:

x' = n * sin(z) + (w/2)
y' = n * cos(z) + (h/2)

受制于:π &书信电报;z<-π 和b<;n<;c级

其中w和h分别是原始图像的高度和宽度,b是原始图像的总列数的一半,c是包含字符的图像的外圆的行数。我以前用Python编程过,但这是我第一次操作像素

到目前为止,我正在遍历图像的所有像素,抓取每个像素,并尝试通过使用range(3,-3)为输出图像创建新的坐标来近似约束。我从来没有尝试过实现这样的方程,所以我在这里迷路了

import math
import numpy as np

from PIL import Image


im = Image.open('dilation.jpg')
im = np.array(im)

imageW = im.shape[0]
imageH = im.shape[1]
b = round(imageW / 2)

#Arbitrarily setting n, not sure how to properly implement
n = b + 1

#Create blank pixel map for straightened text image
new = np.zeros(im.shape)

#Loop through image to grab pixels
for x in range(imageW):
    for y in range(imageH):
        rgb = im[x,y]
        #Set new coordinates, not sure how to implement equation
        for i in range(3, -3,-1):
            new_x = round(n * math.sin(i) + (imageW / 2))
            new_y = round(n * math.cos(i) + (imageH / 2))
            #Place pixel at new coordinates in output map
            new[new_x, new_y] = rgb

#Construct output image from pixel map
new = Image.fromarray(new.astype('uint8'), 'RGB')

#Show output image
new.show()

Tags: in图像imageimportltnewfornp

热门问题