While hacking a simple project I'm stuck with a RMagick
problem for a day now. Since I am very new to RMagick
need suggestions on this.
I am trying to scan a user uploaded image and try to snap/select the image to specific region where actual content is. But there is a chance that user can take image with phone or other device and end up adding much noise to image.
Example image uploaded from user looks like this.
I tried to convert the image to a greyscale
like bellow
img.quantize(2, GRAYColorspace, false)
But which has so much of noise when converted (image). Could any one suggest how can I select only the section/rectangle which contains actual information from the uploaded image?
Bellow are the methods I'm trying to get my hands dirt with.
In ImageMagick command line, you can use -negate (invert colors) and -lat (local area threshold) to convert this to binary (black/white), which might be more what you want. There should be equivalent commands in RMagick.
convert t2.jpg -negate -lat 20x20+10% -negate result.png
ADDITION:
Note, there are a few dark spots in the resulting image above. You need to filter those using -connected-components before you can trim the image to the black box.
convert t2.jpg -negate -lat 20x20+10% -negate -type bilevel \
-define connected-components:area-threshold=10 \
-define connected-components:mean-color=true \
-connected-components 4 \
-bordercolor white -border 1x1 \
-fuzz 15% -trim +repage \
result.png