首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从图像中提取奶牛编号

问从图像中提取奶牛编号
EN

Stack Overflow用户
提问于 2015-04-28 23:39:46
回答 5查看 4.8K关注 0票数 18

每隔一段时间,我妈妈就必须在这些类型的照片中切换,以便从图像中提取数字并将其重命名为数字。

​

我正在尝试使用OpenCV,Python,Tesseract来完成这个过程。我真的迷路了,试图提取图像中包含数字的部分。我怎么能这样做呢?有什么建议吗?我是OpenCV的新手。

编辑:

看看这篇论文,http://yoni.wexlers.org/papers/2010TextDetection.pdf。看起来很突出

EN

回答 5

Stack Overflow用户

发布于 2015-04-30 18:23:46

我一直在重新审视这一点,并在此过程中获得了一些灵感……

  1. Tesseract可以接受自定义字典,如果您深入研究一下,会发现从v3.0开始,它接受命令行参数digits,使其只能识别数字-这似乎是一个适合您需求的有用想法。
  2. 可能没有必要找到数字所在的板-对图像的不同切片多次运行Tesseract并让它自己尝试可能会更容易,因为这是它应该做的。

因此,我决定对图像进行预处理,将25%以内的所有黑色更改为纯黑色,其他所有更改为纯白色。它给出了像这样的预处理图像:

​

​

​

​

​

​

接下来,我生成一系列图像并将它们传递给Tesseract,一次一个。我决定假设数字可能在图像高度的40%到10%之间,所以我在图像高度的40%,30%,20%和10%的条带上做了一个循环。然后我从上到下分20步将条带滑下图像,将每个条带传递给Tesseract,直到条带基本上横跨图像的底部。

以下是40%的条带-动画的每一帧都被传递给Tesseract:

​

​

以下是20%的条带-动画的每一帧都被传递给Tesseract:

​

​

得到条带后,我为Tesseract的最佳位置调整了它们的大小,并清除了噪音等。然后,我将它们传递到Tesseract中,并通过计算它找到的位数来粗略地评估识别质量。最后,我按位数对输出进行排序--大概位数越多越好……

有一些粗糙的边缘和比特,你可以丁丁周围,但这是一个开始!

代码语言:javascript
复制
#!/bin/bash
image=${1-c1.jpg}

# Make everything that is nearly black go fully black, everything else goes white. Median for noise
# convert -delay 500 c1.jpg c2.jpg c3.jpg -normalize -fuzz 25% -fill black -opaque black -fuzz 0 -fill white +opaque black -median 9 out.gif
   convert "${image}" -normalize \
       -fuzz 25% -fill black -opaque black \
       -fuzz 0   -fill white +opaque black \
       -median 9 tmp_$$.png 

# Get height of image - h
h=$(identify -format "%h" "${image}")

# Generate strips that are 40%, 30%, 20% and 10% of image height
for pc in 40 30 20 10; do
   # Calculate height of this strip in pixels - sh
   ((sh=(h*pc)/100))
   # Calculate offset from top of picture to top of bottom strip - omax
   ((omax=h-sh))
   # Calculate step size, there will be 20 steps
   ((step=omax/20))

   # Cut strips sh pixels high from the picture starting at top and working down in 20 steps
   for (( off=0;off<$omax;off+=$step)) do
      t=$(printf "%05d" $off)
      # Extract strip and resize to 80 pixels tall for tesseract
      convert tmp_$$.png -crop x${sh}+0+${off}      \
          -resize x80 -median 3 -median 3 -median 3 \
          -threshold 90% +repage slice_${pc}_${t}.png

      # Run slice through tesseract, seeking only digits
      tesseract slice_${pc}_${t}.png temp digits quiet

      # Now try and assess quality of output :-) ... by counting number of digits
      digits=$(tr -cd "[0-9]" < temp.txt)
      ndigits=${#digits}
      [ $ndigits -gt 0 ] && [ $ndigits -lt 6 ] && echo $ndigits:$digits
   done
done | sort -n

Cow 618的输出(第一个数字是找到的位数)

代码语言:javascript
复制
2:11
2:11
3:573
5:33613    <--- not bad

Cow 2755的输出(第一个数字是找到的位数)

代码语言:javascript
复制
2:51
3:071
3:191
3:517
4:2155   <--- pretty close
4:2755   <--- nailed that puppy :-)
4:2755   <--- nailed that puppy :-)
4:5212
5:12755  <--- pretty close

Cow 3174的输出(第一个数字是找到的位数)

代码语言:javascript
复制
3:554
3:734
5:12732
5:31741  <--- pretty close

很酷的问题--谢谢!

票数 7
EN

Stack Overflow用户

发布于 2015-04-29 02:30:22

使用PIL (Python成像库),您可以轻松地加载图像并对其进行处理。使用grayscale conversion,您可以将RGB转换为灰度,这应该更容易检测级别。如果你想要阈值图像(以检测白板),有一个point() function,它可以让你映射颜色。

另一方面,您可以编写一个简单的程序,这样您就可以

  • 选择并显示图像
  • 标记电路板所在的区域
  • 裁剪图像
  • apply tesseract或其他,
  • 用检测到的数字保存图像

这应该会大大简化这个过程!使用TkInter、PyGTK、PyQt或其他一些窗口工具包编写这段代码应该相对容易。

编辑:我需要一个类似的程序来对图片进行分类-尽管不是OCRing。所以我最终决定这是一个很好的时机,并进行了第一次尝试(使用OCR!)。在试用之前对您的映像进行备份!快速手册:

  • 左上角:选择工作文件夹,如果文件夹中有图像,将显示图像列表。
  • 精选图像。选择带编号的图像区域。坐标将出现在左下角,程序将调用Tesseract。接受图像编辑-如有必要,将重命名dialog.
  • Click Ok to

-

  • 中的数字OCRd。

下面是pre-alpha程序:

代码语言:javascript
复制
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#  test_pil.py
#  
#  Copyright 2015 John Coppens <john@jcoppens.com>
#  
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#  
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#  
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#  MA 02110-1301, USA.
#  
#  

import pygtk
import gtk
import glob
import os.path as osp
from os import rename
import re
import subprocess as sp

temp_image = "/tmp/test_pil.png"
image_re = """\.(?:jpe?g|png|gif)$"""

class RecognizeDigits():
    def __init__(self):
        pass

    def process(self, img, x0, y0, x1, y1):
        """ Receive the gtk.Image, and the limits of the selected area (in
            window coordinates!)
            Call Tesseract on the area, and give the possibility to  edit the
            result.
            Returns None if NO is pressed, and the OCR'd (and edited) text if OK
        """
        pb = img.get_pixbuf().subpixbuf(x0, y0, x1-x0, y1-y0)
        pb.save(temp_image, "png")

        out = sp.check_output(("tesseract", temp_image, "stdout", "-psm 7", "digits"))
        out = out.replace(" ", "").strip()

        dlg = gtk.MessageDialog(type = gtk.MESSAGE_QUESTION,
                                flags = gtk.DIALOG_MODAL,
                                buttons = gtk.BUTTONS_YES_NO,
                                message_format = "The number read is:")
        entry = gtk.Entry()
        entry.set_text(out)
        dlg.get_message_area().pack_start(entry)
        entry.show()
        response = dlg.run()
        nr = entry.get_text()

        dlg.destroy()

        if response == gtk.RESPONSE_YES:
            return nr
        else:
            return None

class FileSelector(gtk.VBox):
    """ Provides a folder selector (at the top) and a list of files in the
        selected folder. On selecting a file, the FileSelector calls the
        function provided to the constructor (image_viewer)
    """
    def __init__(self, image_viewer):
        gtk.VBox.__init__(self)
        self.image_viewer = image_viewer

        fc = gtk.FileChooserButton('Select a folder')
        fc.set_action(gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER)
        fc.connect("selection-changed", self.on_file_set)
        self.pack_start(fc, expand = False, fill = True)

        self.tstore = gtk.ListStore(str)
        self.tview = gtk.TreeView(self.tstore)
        self.tsel = self.tview.get_selection()
        self.tsel.connect("changed", self.on_selection_changed)
        renderer = gtk.CellRendererText()
        col = gtk.TreeViewColumn(None, renderer, text = 0)
        self.tview.append_column(col)

        scrw = gtk.ScrolledWindow()
        scrw.add(self.tview)
        self.pack_start(scrw, expand = True, fill = True)

    def on_file_set(self, fcb):
        self.tstore.clear()
        self.imgdir = fcb.get_filename()
        for f in glob.glob(self.imgdir + "/*"):
            if re.search(image_re, f):
                self.tstore.append([osp.basename(f)])

    def on_selection_changed(self, sel):
        model, itr = sel.get_selected()
        if itr != None:
            base = model.get(itr, 0)
            fname = self.imgdir + "/" + base[0]
            self.image_viewer(fname)

class Status(gtk.Table):
    """ Small status window which shows the coordinates for of the area
        selected in the image
    """
    def __init__(self):
        gtk.Table.__init__(self)

        self.attach(gtk.Label("X"), 1, 2, 0, 1, yoptions = gtk.FILL)
        self.attach(gtk.Label("Y"), 2, 3, 0, 1, yoptions = gtk.FILL)
        self.attach(gtk.Label("Top left:"), 0, 1, 1, 2, yoptions = gtk.FILL)
        self.attach(gtk.Label("Bottom right:"), 0, 1, 2, 3, yoptions = gtk.FILL)

        self.entries = {}
        for coord in (("x0", 1, 2, 1, 2), ("y0", 2, 3, 1, 2),
                      ("x1", 1, 2, 2, 3), ("y1", 2, 3, 2, 3)):
            self.entries[coord[0]] = gtk.Entry()
            self.entries[coord[0]].set_width_chars(6)
            self.attach(self.entries[coord[0]],
                                     coord[1], coord[2], coord[3], coord[4],
                                     yoptions = gtk.FILL)

    def set_top_left(self, x0, y0):
        self.x0 = x0
        self.y0 = y0
        self.entries["x0"].set_text(str(x0))
        self.entries["y0"].set_text(str(y0))

    def set_bottom_right(self, x1, y1):
        self.x1 = x1
        self.y1 = y1
        self.entries["x1"].set_text(str(x1))
        self.entries["y1"].set_text(str(y1))

class ImageViewer(gtk.ScrolledWindow):
    """ Provides a scrollwindow to move the image around. It also detects
        button press and release events (left button), will call status
        to update the coordinates, and will call task on button release
    """
    def __init__(self, status, task = None):
        gtk.ScrolledWindow.__init__(self)

        self.task = task
        self.status = status
        self.drawing = False
        self.prev_rect = None

        self.viewport = gtk.Viewport()
        self.viewport.connect("button-press-event", self.on_button_pressed)
        self.viewport.connect("button-release-event", self.on_button_released)
        self.viewport.set_events(gtk.gdk.BUTTON_PRESS_MASK | \
                                 gtk.gdk.BUTTON_RELEASE_MASK)

        self.img = gtk.Image()
        self.viewport.add(self.img)
        self.add(self.viewport)

    def set_image(self, fname):
        self.imagename = fname
        self.img.set_from_file(fname)

    def on_button_pressed(self, viewport, event):
        if event.button == 1:       # Left button: Select rectangle start
            #self.x0, self.y0 = self.translate_coordinates(self.img, int(event.x), int(event.y))
            self.x0, self.y0 = int(event.x), int(event.y)
            self.status.set_top_left(self.x0, self.y0)
            self.drawing = True

    def on_button_released(self, viewport, event):
        if event.button == 1:       # Right button: Select rectangle end
            #self.x1, self.y1 = self.translate_coordinates(self.img, int(event.x), int(event.y))
            self.x1, self.y1 = int(event.x), int(event.y)
            self.status.set_bottom_right(self.x1, self.y1)
            if self.task != None:
                res = self.task().process(self.img, self.x0, self.y0, self.x1, self.y1)

                if res == None: return

                newname = osp.split(self.imagename)[0] + '/' + res + ".jpeg"
                rename(self.imagename, newname)
                print "Renaming ", self.imagename, newname

class MainWindow(gtk.Window):
    def __init__(self):
        gtk.Window.__init__(self)
        self.connect("delete-event", self.on_delete_event)
        self.set_size_request(600, 300)

        grid = gtk.Table()

        # Image selector
        files = FileSelector(self.update_image)
        grid.attach(files, 0, 1, 0, 1,
                           yoptions = gtk.FILL | gtk.EXPAND, xoptions = gtk.FILL)

        # Some status information
        self.status = Status()
        grid.attach(self.status, 0, 1, 1, 2,
                                 yoptions = gtk.FILL, xoptions = gtk.FILL)

        # The image viewer
        self.viewer = ImageViewer(self.status, RecognizeDigits)
        grid.attach(self.viewer, 1, 2, 0, 2)
        self.add(grid)

        self.show_all()

    def update_image(self, fname):
        self.viewer.set_image(fname)

    def on_delete_event(self, wdg, data):
        gtk.main_quit()

    def run(self):
        gtk.mainloop()

def main():
    mw = MainWindow()
    mw.run()
    return 0

if __name__ == '__main__':
    main()
票数 5
EN

Stack Overflow用户

发布于 2017-10-28 11:50:23

在opencv的帮助下,我想出了一个非常简单的解决方案。

  1. 调整图像大小,以便按轮廓修剪异常值(更易于测量区域)

std::string const img_name = "cow_00";Mat input = imread("../forum_quest/data/“+ img_name + ".jpg");cout< 1000){ cv::resize(input,input,{1000,(int)(1000.0/input.cols * input.rows)},0.25,0.25);}

  • 将区域裁剪到1/3的顶部

//假设文本始终位于图像垫前景的1/3上方;输入(Rect( 0,0,input.cols,input.rows/3)).copyTo(crop_region);

  • Extract crop_region

cv::Mat fore_ground_extract(cv::Mat const &input) {向量bgr;split(input,bgr);//按照安德鲁的建议在蓝色通道上处理,因为//更容易去除植被Mat text_region = bgr;medianBlur(text_region,text_region,5);threshold(text_region,text_region,0,255,cv::THRESH_OTSU);//进一步去除小噪声,不需要的边框常量erode_kernel = getStructuringElement(MORPH_ELLIPSE,{11,11});侵蚀( text_region,text_region,erode_kernel);常量dilate_kernel = getStructuringElement(MORPH_ELLIPSE,{7,7});dilate(text_region,text_region,dilate_kernel);//将文本由黑变白,更容易提取为轮廓bitwise_not(text_region,text_region);返回text_region;}

​

​

​

​

​

​

  1. 提取等高线,如果等高线精度较低,可以使用ERFilter提取文本

std::vector> get_text_contours(cv::Mat const &input) {//查找候选文本的轮廓,使用//某些轮廓属性去除离群值//如果此解决方案的精度是低向量轮廓,请尝试ERFilter;findContours(input,contours,RETR_EXTERNAL,CHAIN_APPROX_SIMPLE);auto outlier = cpoints const &cp { auto const rect = cv::boundingRect(cp);return rect.width > rect.height && (rect.area() < 900 || rect.area() >= 10000);};auto it =std::remove_if(std::begin(轮廓),std::end(轮廓),outlier);contours.erase(it,std::end(轮廓));sort(std::begin(轮廓),std::end(轮廓),cpoints const &lhs, cpoints const &rhs { return cv::boundingRect(lhs).x < cv::boundingRect(rhs).x;});返回轮廓;}

  • 创建字符分类器并遍历候选文本

字符串常量词汇表= "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";//必须与分类器输出类具有相同的顺序: ocr = text::loadOCRHMMClassifierCNN("OCRBeamSearch_CNN_model_data.xml.gz");向量out_classes;向量out_confidences;Ptr (size_t i= 0;i< text_contours.size();++i){标量常量颜色=标量( rng.uniform(0,255),rng.uniform(0, 255),rng.uniform(0,255) );drawContours(text_mask,text_contours,static_cast(i),color,2);auto const text_loc = boundingRect(text_contoursi);//裁剪区域经过场景图像矩形(crop_region,text_loc,color,2)训练,准确率最高;ocr->eval(crop_region(text_loc),out_classes,out_confidences);cout << "OCR output = \"“<< vocabulary[out_classes] << "\”信心十足“<< out_confidences << std::endl;putText(crop_region,string(1,vocabulary[out_classes]),Point(text_loc.x,text_loc.y - 5),FONT_HERSHEY_SIMPLEX,2,标量(255,0,0),2);imshow("text_mask",text_mask);imshow("crop_region",crop_region(text_loc));waitKey();}

结果:

​

​

完整的源代码放在github上

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29923827

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档