How would I go about finding the bounding box or window for the region of whitespace surrounding the numbers in the image below?:
Original image:
Height: 762 pixels Width: 1014 pixels
Goal:
Something like: {x-bound:[x-upper,x-lower], y-bound:[y-upper,y-lower]}
so I can crop to the text and input into tesseract or some OCR.
Attempts:
I had thought of slicing the image into hard coded chunk sizes and analysing at random, but i think it would be too slow.
Example code using pyplot
adapted from (Using python and PIL how can I grab a block of text in an image?):
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
im = Image.open('/home/jmunsch/Pictures/Aet62.png')
p = np.array(im)
p = p[:,:,0:3]
p = 255 - p
lx,ly,lz = p.shape
plt.plot(p.sum(axis=1))
plt.plot(p.sum(axis=0))
#I was thinking something like this
#The image is a 3-dimensional ndarray [[x],[y],[color?]]
#Set each value below an axes mean to 0
[item = 0 for item in p[axis=0] if item < p.mean(axis=0)]
# and then some type of enumerated groupby for each axes
#finding the mean index for each groupby(0) on axes
plt.plot(p[mean_index1:mean_index2,mean_index3:mean_index4])
Based on the graphs each of the valleys would indicate a place to bound.
- The first graph shows where lines of text would be
- The second graph shows where characters would be
Plot example plt.plot(p.sum(axis=1))
:
Plot example output plt.plot(p.sum(axis=0))
:
Related posts/docs:
- Trim whitespace using PIL
- Using python and PIL how can I grab a block of text in an image?
- Use Python / PIL or similar to shrink whitespace
- Crop the image using PIL in python
- Rectangular bounding box around blobs in a monochrome image using python
- How can I improve my paw detection?
- http://scipy-lectures.github.io/advanced/image_processing/
- http://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.html
I think you can use Morphology functions in
scipy.ndimage
, here is an example:the output is: