Page 1 of 1

cutting borders

Posted: 2017-05-08T13:58:49-07:00
by arashbi
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.

Re: cutting borders

Posted: 2017-05-08T14:00:39-07:00
by snibgo
Sample image, please. And say what version IM you are using, on what platform.

Re: cutting borders

Posted: 2017-05-08T15:17:14-07:00
by fmw42
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

Posted: 2017-05-09T06:44:44-07:00
by arashbi
I think looking at this will make it clear http://imgur.com/Z3zfLFe

Re: cutting borders

Posted: 2017-05-09T07:18:55-07:00
by snibgo
And say what version IM you are using, on what platform.

Re: cutting borders

Posted: 2017-05-09T09:20:40-07:00
by fmw42
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.

Re: cutting borders

Posted: 2017-05-09T09:59:31-07:00
by fmw42
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.

Re: cutting borders

Posted: 2017-05-09T17:46:03-07:00
by fmw42
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:
Image

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
Image

Then if you know your border tolerances, you can chop and save off the remaining outer spots using:

Re: cutting borders

Posted: 2017-06-13T14:23:19-07:00
by arashbi
Wow, that is amazing!