从图像提取牛数 [英] Extract cow number from image

查看:246
本文介绍了从图像提取牛数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,我的妈妈必须通过这些类型的照片,以从图像中提取数字,并将其重命名为数字。




我试图使用OpenCV,Python,Tesseract来完成这个过程。我真的失去了试图用数字提取图像的一部分。我怎么能这样做?任何建议我真的是新的OpenCV。



我试图提取白色矩形板使用阈值和轮廓,但没有效果,因为我选择的thresh不'



编辑:



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

解决方案

我一直在看看这个,并在路上有几个灵感....


  1. Tesseract可以接受自定义字典,如果你再挖一点,似乎从v3.0,它接受命令

    可能没有必要找到具有数字的板 - 可能更容易用图像的各个切片来运行Tesseract多次,并且让它尝试本身,因为它是应该做的。


因此,我决定将图片的所有内容都改为黑色的25%白色。这样可以得到如下的预处理图片:







>



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



这里是40%的条带 - 动画的每一帧都传递给Tesseract:





这里是20%的条 - 每个框架的动画传递给Tesseract:





获得条带后,我们为Tesseract的甜蜜点调整它们的大小,并从噪音中清除它们。然后,我将它们传递给Tesseract,质量的识别,有点粗俗,通过计数找到的位数。最后,我用数字的数字排序输出 - 可能更多的数字可能更好...



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

 #!/ bin / bash 
image = $ {1-c1.jpg}

#使接近黑色的东西全黑,一切都变白。噪音中值
#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

#获取图像的高度 - h
h = $(识别-format%h$ {image})

#生成图像高度的40%,30%,20%和10%的条带
for pc in 40 30 20 10; do
#以像素为单位计算这个带的高度 - sh
((sh =(h * pc)/ 100))
#计算从图片顶部到底部顶部的偏移量omax
((omax = h-sh))
#计算步长,将有20个步骤
((step = omax / 20))

(从off = 0; off< $ omax; off + = $ step))do
t = $(printf%05d $ off)
#提取条带并将其调整为80像素高为tesseract
convert tmp _ $$。png -crop x $ {sh} +0 + $ {off} \
- resize x80 -median 3 -median 3 -median 3 \
-threshold 90%+ reage slice _ $ {pc} _ $ {t} .png

#通过tesseract运行切片,寻找只有数字
tesseract slice _ $ {pc} _ $ {t} .png临时数字安静

#现在尝试并评估输出质量:-) ...通过计数位数
digits = $(tr -cd[0-9]< temp.txt)
ndigits = $ {#digits}
[$ ndigits -gt 0]&& [$ ndigits -lt 6]&& echo $ ndigits:$ digits
done
done | sort -n

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

  2:11 
2:11
3:573
5: 33613< --- not bad

Cow 2755的输出 number是找到的数字位数)

  2:51 
3:071
3:191
3:517
4:2155< --- pretty close
4:2755< --- nailed that puppy :-)
4:2755< ---钉子那只小狗:-)
4:5212
5:12755 <---很接近

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

  3:554 
3:734
5:12732
5:31741< --- pretty close

酷问题 - 谢谢!


every now and then my mom has to shift through these type of photos to extract the number from the image and rename it to the number.

I'm trying to use OpenCV, Python, Tesseract to get the process done. I'm really lost trying to extract the portion of the image with the numbers. How could I do this? Any suggestions i'm really new at OpenCV.

I tried to extract the white rectangular board using thresholds and contours, but no avail because the RGB I choose for thresh doesn't always work and I don't know how to choose the contour.

EDIT:

Looking at this paper http://yoni.wexlers.org/papers/2010TextDetection.pdf . Looks prominisn

解决方案

I have been having another look at this, and had a couple of inspirations along the way....

  1. Tesseract can accept custom dictionaries, and if you dig a little more, it appears that from v3.0, it accepts the command-line parameter digits to make it recognise digits only - seems a useful idea for your needs.

  2. It may not be necessary to find the boards with the digits on - it may be easier to run Tesseract multiple times with various slices of the image and let it have a try itself as that is what it is supposed to do.

So, I decided to preprocess the image by changing everything that is within 25% of black to pure black, and everything else to pure white. That gives pre-processed images like this:

Next, I generate a series of images and pass them, one at a time to Tesseract. I decided to assume that the digits are probably between 40% to 10% of the image height, so I made a loop over strips 40, 30, 20 and 10% of the image height. I then slide the strip down the image from top to bottom in 20 steps passing each strip to Tesseract, till the strip is essentially across the bottom of the image.

Here are the 40% strips - each frame of the animation is passed to Tesseract:

Here are the 20% strips - each frame of the animation is passed to Tesseract:

Having got the strips, I resize them nicely for Tesseract's sweet spot and clean them up from noise etc. Then, I pass them into Tesseract and assess the quality of the recognition, somewhat crudely, by counting the number of digits it found. Finally, I sort the output by number of digits - presumably more digits is maybe better...

There are some rough edges and bits that you could dink around with, but it is a start!

#!/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

Output for Cow 618 (first number is the number of digits found)

2:11
2:11
3:573
5:33613    <--- not bad

Output for Cow 2755 (first number is the number of digits found)

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

Output for Cow 3174 (first number is the number of digits found)

3:554
3:734
5:12732
5:31741  <--- pretty close

Cool question - thank you!

这篇关于从图像提取牛数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆