cutting borders

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
arashbi
Posts: 3
Joined: 2017-05-08T13:43:39-07:00
Authentication code: 1151

cutting borders

Post 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.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: cutting borders

Post by snibgo »

Sample image, please. And say what version IM you are using, on what platform.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: cutting borders

Post 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.
arashbi
Posts: 3
Joined: 2017-05-08T13:43:39-07:00
Authentication code: 1151

Re: cutting borders

Post by arashbi »

I think looking at this will make it clear http://imgur.com/Z3zfLFe
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: cutting borders

Post by snibgo »

And say what version IM you are using, on what platform.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: cutting borders

Post 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.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: cutting borders

Post 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.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: cutting borders

Post 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:
arashbi
Posts: 3
Joined: 2017-05-08T13:43:39-07:00
Authentication code: 1151

Re: cutting borders

Post by arashbi »

Wow, that is amazing!
Post Reply