Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用python实现OCR_Python_Ocr - Fatal编程技术网

用python实现OCR

用python实现OCR,python,ocr,Python,Ocr,嗨,我正试着用OCR识别这个数字样本 我制作数字数据库,我拍摄数字1、2、3的截图 稍后,识别我截图上的数字,并与数据库截图进行比较 代码工作得很好,但我有一个懒惰的问题,数字可能是0.00到999.99,所以我需要截图,我无法创建数字,所以我想我需要找到其他解决方案 我想如果我能打破截图之间。(100.99=100和99)我的数据库中只需要999个样本 所以你认为这是个好办法 新闻!!! 我继续搜索,终于用pytesseract找到了解决方案 几件事,我需要调整图像的高度,以最小25像素为10

嗨,我正试着用OCR识别这个数字样本

我制作数字数据库,我拍摄数字1、2、3的截图

稍后,识别我截图上的数字,并与数据库截图进行比较

代码工作得很好,但我有一个懒惰的问题,数字可能是0.00到999.99,所以我需要截图,我无法创建数字,所以我想我需要找到其他解决方案

我想如果我能打破截图之间。(100.99=100和99)我的数据库中只需要999个样本

所以你认为这是个好办法

新闻!!! 我继续搜索,终于用pytesseract找到了解决方案

几件事,我需要调整图像的高度,以最小25像素为100%的好结果

如果我用png格式保存图像不工作,但用jpg工作完善。 如果我用paint打开png图像并保存而不做任何更改,则任何代码都不能完美地处理png图像。我不能理解这一点

我真的需要使用png,因为我需要代码快速工作。 有没有办法用png格式解决这个问题

import pytesseract
from PIL import Image
x = pytesseract.image_to_string(Image.open('101.jpg'))
y = float(x)

print y
我搜索有关图像分割的代码,查找轮廓和连接的组件。 我找到这段代码是为了找到数字和点的区域

在数字0,1,6,8和点中发现1个区域,在其他区域中发现2个区域

我无法更改用于处理图像的代码(数字为白色背景黑色),因此我更改了图像的颜色,我看到了不可能的编辑代码来修复区域问题

我感谢你的帮助

我想我可能不需要更改代码,如果我能够保存不同图像中的每个区域,我就可以这样做

i=0
while i < len(regionfound)
   if height(region[i] = 13 #(max height)
     compare region with dabatabe image of numbers 0,1,6 and 8

   if height = 2
     region are dot

   if height = .....  

   i+=1

您是否尝试过先分割图像,然后对每个不同的字符应用识别算法,而不是一次对整个图像应用识别算法?您只需要处理0-9个字符(和点),而不是1000个样本。由于您的图像看起来已经是二进制的(黑色或白色),因此您只需按照OpenCV的connected_components或cvFindContours.Thaks的思路执行连接组件标记。我开始研究如何进行图像分割,但昨天我开始使用pytesseract,可能会更容易。
import sys
from PIL import Image, ImageDraw

class Region():
    def __init__(self, x, y):
        self._pixels = [(x, y)]
        self._min_x = x
        self._max_x = x
        self._min_y = y
        self._max_y = y

    def add(self, x, y):
        self._pixels.append((x, y))
        self._min_x = min(self._min_x, x)
        self._max_x = max(self._max_x, x)
        self._min_y = min(self._min_y, y)
        self._max_y = max(self._max_y, y)

    def box(self):
        return [(self._min_x, self._min_y), (self._max_x, self._max_y)]

def find_regions(im):
    width, height  = im.size
    regions = {}
    pixel_region = [[0 for y in range(height)] for x in range(width)]
    equivalences = {}
    n_regions = 0
    #first pass. find regions.
    for x in xrange(width):
        for y in xrange(height):
            #look for a black pixel
            if im.getpixel((x, y)) == (0, 0, 0, 255): #BLACK NUMBERS FOR WHITE NUMBER USE (255, 255, 255, 255)
                # get the region number from north or west
                # or create new region
                region_n = pixel_region[x-1][y] if x > 0 else 0
                region_w = pixel_region[x][y-1] if y > 0 else 0
                max_region = max(region_n, region_w)

                if max_region > 0:
                    #a neighbour already has a region
                    #new region is the smallest > 0
                    new_region = min(filter(lambda i: i > 0, (region_n, region_w)))
                    #update equivalences
                    if max_region > new_region:
                        if max_region in equivalences:
                            equivalences[max_region].add(new_region)
                        else:
                            equivalences[max_region] = set((new_region, ))
                else:
                    n_regions += 1
                    new_region = n_regions

                pixel_region[x][y] = new_region

    #Scan image again, assigning all equivalent regions the same region value.
    for x in xrange(width):
        for y in xrange(height):
                r = pixel_region[x][y]
                if r > 0:
                    while r in equivalences:
                        r = min(equivalences[r])

                    if not r in regions:
                        regions[r] = Region(x, y)
                    else:
                        regions[r].add(x, y)

    return list(regions.itervalues())

def main():
    im = Image.open(r"0.png")
    regions = find_regions(im)
    draw = ImageDraw.Draw(im)
    for r in regions:
        draw.rectangle(r.box(), outline=(255, 0, 0))
    del draw 
    #im.show()
    output = file("output.png", "wb")
    im.save(output)
    output.close()

if __name__ == "__main__":
    main()