[SOLVED]crop rectangles with text

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
kmsgli
Posts: 5
Joined: 2018-12-28T06:18:52-07:00
Authentication code: 1152

[SOLVED]crop rectangles with text

Post by kmsgli »

Hello, I am working on a program to OCR fundraiser slips in an attempt to save time. The issue I am having is each slip is not the exact same size so when i set coordinates to crop the three info blocks its not always exactly correct. I can change the slip any way I like so I am thinking if I place the information inside rectangles I can crop the rectangles but cant find a good solution.

Maybe fill the background color to black and crop only the white areas of the photo? Or maybe there is a better solution I am not aware of.

Any help is appreciated.

thanks.

Image
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: crop rectangles with text

Post by fmw42 »

If this is a standard form, I would suggest putting registration crosses in the corners. You can then locate each registration mark and warp the images based upon the registration mark coordinates to match. See -distort Affine. Otherwise, trim the images to the ends of the two dotted lines and use the corners of the trimmed images as control points for the registration.
kmsgli
Posts: 5
Joined: 2018-12-28T06:18:52-07:00
Authentication code: 1152

Re: crop rectangles with text

Post by kmsgli »

Thanks for your input.

How can I crop based on say a line across the very bottom of the slip? That seems to be the simplest solution?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: crop rectangles with text

Post by fmw42 »

Sorry, but I am not sure where you want to crop. Please clarify or make a drawing showing what you want to crop?
kmsgli
Posts: 5
Joined: 2018-12-28T06:18:52-07:00
Authentication code: 1152

Re: crop rectangles with text

Post by kmsgli »

So I am going to adjust my slip to look like this:
Image

How can I get the image cropped to just the outer rectangle as that would give me the consistency I need to crop the different text items separately with a great deal of consistency.

The regular trim function does not work properly as its a scanned image and from what I read scanned images are rather dirty with dead pixels.

This script from this section of the manual https://www.imagemagick.org/Usage/crop/#trim_noisy
works but the issue is my program runs multiple images and this does not work using the Linux alias for all files in a folder (*.png)

Code: Select all

convert noisy.jpg -crop \
    `convert noisy.jpg -virtual-pixel edge -blur 0x15 -fuzz 15% \
             -trim -format '%wx%h%O' info:`   +repage   noisy_trimmed_2.jpg
Is there a simpler way to clean the dead space so I can trim to the outer box that can be implemented into a larger program?

All I really need is the difference in the scans to go away as some people cut the slip off in different places making for a different amounts of dead space around the text. The rectangle around the area I need will give me a reference point but I need a way to trim to the outer rectangle every time.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: crop rectangles with text

Post by fmw42 »

Since you have noise in your image, you have to filter it first. You have done one way. There is really no easier way that I know. In Imagemagick 6, you need two convert commands. In Imagemagick 7, you could do it in one. Another way is to replace your blur with -statistic median or -connected-components. For example, try

Code: Select all

cropvals=`convert image.png -statistic median 5x5 -fuzz 15% -format "%@" info:`
convert image.png -crop $cropvals image_crop.png

Code: Select all

cropvals=`convert image.png -threshold 0 -type bilevel \
-define connected-components:area-threshold=25 \
-define connected-components:mean-color=true \
-connected-components 4 \
-fuzz 15% -format "%@" info:`
convert image.png -crop $cropvals image_crop.png
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: crop rectangles with text

Post by fmw42 »

Another way is to scale the image down to 1 column, threshold and trim to get the hh and yoffset. Then repeat to scale to 1 row to get the ww and xoffset. Then use those values to crop the image. Here is the command to get the image down to one column and then scale up to 50 columns to see what I am doing. You can actually keep 50 columns (or rows) and trim, since you are only getting the hh and yoffset. So the width does not matter.

Code: Select all

convert image.png -scale 1x! -scale 50x! -negate -threshold 2% -negate image1_rows.png
With a large enough threshold (about 10%), you can remove the bottom black short line below the dotted lines.
kmsgli
Posts: 5
Joined: 2018-12-28T06:18:52-07:00
Authentication code: 1152

Re: crop rectangles with text

Post by kmsgli »

fmw42, Thanks for your input I really appreciate it, I will play with those examples you gave and see what works best withing the program I am building.

In searching around today I found an alternate solution but I am not actually sure if it should be done this way

I found -kuwahara first then -trim with a high -fuzz percent accomplishes a crop exactly as I want. I don't notice any real text degradation with such a high fuzz factor. Also this method expands my mulitpage tif file for me while running it through the kuwahara filter then from there I can run a crop to the outer rectangle of all the .png files in the folder.

Code: Select all

convert input.tif -kuwahara 2 multi-output.png
convert *.png -fuzz 90% -trim +repage multi-output.png
what do you think about this?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: crop rectangles with text

Post by fmw42 »

Kuwahara is a noise smoothing filter. But if using too large a value, it will smooth your image as in the examples at https://imagemagick.org/discourse-serve ... =4&t=26480. For small arguments it will remove your spot noise that is limiting the trim. Since this works for you, it looks like a good way to do all the pages at one time. The high fuzz values is needed since you have nearly binary data, but still have noise, though it is smoothed to brighter values. Anyway, that certainly seems like a good way, since it seems to work. But you can do that all in one line.

Code: Select all

convert input.tif -kuwahara 2 -fuzz 90% -trim +repage multi-output_%02d.png
kmsgli
Posts: 5
Joined: 2018-12-28T06:18:52-07:00
Authentication code: 1152

Re: crop rectangles with text

Post by kmsgli »

fmw42 wrote: 2018-12-29T12:55:57-07:00 Kuwahara is a noise smoothing filter. But if using too large a value, it will smooth your image as in the examples at https://imagemagick.org/discourse-serve ... =4&t=26480. For small arguments it will remove your spot noise that is limiting the trim. Since this works for you, it looks like a good way to do all the pages at one time. The high fuzz values is needed since you have nearly binary data, but still have noise, though it is smoothed to brighter values. Anyway, that certainly seems like a good way, since it seems to work. But you can do that all in one line.

Code: Select all

convert input.tif -kuwahara 2 -fuzz 90% -trim +repage multi-output_%02d.png
Thanks again for all your help, I will mark this thread as solved in the title.

ImageMagick FTW!
Post Reply