cutting borders
cutting borders
Imagine I took a picture of a printed paper on a table. I want to cut the table from around the picture and have of picture of just the writing. Lets say the table could be any color, even patterned; but we have a white margin around the paper. How can I remove the pieces of table from the print-out. I want to potentially run OCR on this print-outs, and want to get rid of the noise.
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: cutting borders
Sample image, please. And say what version IM you are using, on what platform.
snibgo's IM pages: im.snibgo.com
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: cutting borders
As snibgo says, please provide an example so we can show you how to process it. Basically turn all colors but white to black. Floodfill the interior of the white border with white. Negate (invert the white and black). Then use that to put into the alpha channel of the input image. That should keep the printed paper and make the outside area of the table transparent.
Re: cutting borders
I think looking at this will make it clear http://imgur.com/Z3zfLFe
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: cutting borders
And say what version IM you are using, on what platform.
snibgo's IM pages: im.snibgo.com
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: cutting borders
Your receipt does not have a pure white border around it. So since it is grayish and your background is grayish, it will be nearly impossible or impossible to get a good separation. I know of no universal solution to this problem when the background and foreground are too similar in color.
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: cutting borders
See products like https://www.google.com/url?sa=t&rct=j&q ... RFbF0m1PVQ that allow you to draw some crude swaths inside and outside and then it will remove the background.
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: cutting borders
I can process this image to achieve the following on a Unix-like system (Mac OSX Sierra) and IM 6.9.8.4 Q16.
Basically, I process the image to make it black and white, then blur it more vertically than horizontally so that the black text merges together. Then I do a connected components labelling to extract the bounding box of the largest black region (assuming the center black area is larger than the outer black area). I crop the input and reprocess to make it binary.
Input:
Then if you know your border tolerances, you can chop and save off the remaining outer spots using:
Basically, I process the image to make it black and white, then blur it more vertically than horizontally so that the black text merges together. Then I do a connected components labelling to extract the bounding box of the largest black region (assuming the center black area is larger than the outer black area). I crop the input and reprocess to make it binary.
Input:
Code: Select all
infile="Z3zfLFeg.jpg"
declare `convert "$infile" -format "center=%[fx:w/2],%[fx:h/2]" info:`
OLD_IFS=$IFS
IFS=$'\n'
obj_array=(`convert "$infile" -negate -lat 20x20+10% \
-morphology convolve blur:0x10+90 -morphology convolve blur:0x2 \
-auto-level -threshold 0 -negate -type bilevel \
-define connected-components:verbose=true -connected-components 8 \
null: | tail -n +2 | sed 's/^[ ]*//'`)
IFS=$OLD_IFS
for ((i=0; i<${#obj_array[*]}; i++)); do
echo "${obj_array[$i]}"
bbox=`echo "${obj_array[$i]}" | cut -d\ -f 2`
color=`echo "${obj_array[$i]}" | cut -d\ -f 5`
echo "color=$color; bbox=$bbox;"
[ "$color" = "gray(0)" ] && break
done
convert $infile -crop $bbox +repage \
-negate -lat 20x20+10% -negate result.png
Then if you know your border tolerances, you can chop and save off the remaining outer spots using:
Re: cutting borders
Wow, that is amazing!