Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to make a script that automatically corrects scanned documents?

I often scan handwritten documents to send to colleagues, and need to make corrections to the digital file once it's scanned. (For example, I change mistakes I made on the original document to white.)

I am thinking of some script which can do the following:

Take a color scan image (say a tiff) as input, and make simple corrections automatically based on colored corrections in the image.

For example take the simplest case: I write only black on white. There is an area where I made mistakes so I draw a red closed circle (with a pen on the actual sheet of paper) around that area. Then I scan the image (or usually many of them). Now I would like the script to erase each of these areas in all of the images so my mistakes disappear in the resulting image.

Any ideas how to realize this in a Linux environment, e.g. with Image Magick?


It looks like Gimp with script-fu could be the way to go it should be powerful enough. Can somebody give me a hint by pointing out the above example would look like in script-fu?

like image 767
highsciguy Avatar asked Apr 20 '12 17:04

highsciguy


1 Answers

I'm thinking in a solution based on ImageMagick. We would need the following steps:

  1. Find the color used to draw in the scanned document (for now on, called target color);
  2. Find its x and y coordinates in the image;
  3. Pass this position as a seed to Flood Fill algorithm.

We could use the following script based on functions of ImageMagick:

  1. Output all the unique colors in the picture. This will be used to find out which are the RGB components of the target color (command source).

    convert <image> -unique-colors -depth 8 txt:- > output.txt
    
  2. Output the coordinates of each color in a text file:

    convert <image> txt:- > coord.txt
    
  3. Find the x and y coordinates of the target color (command source). Suppose the target color obtained by step 1 was red:

    grep red coord.txt
    
  4. Finally, use x and y as a seed to floodfill to replace the circle region by your desired color (command source). In this case, I've used white to erase the region:

    convert <image> -fill white -fuzz 13% \
            -draw 'color <x>,<y> floodfill' <image_floodfill_output>
    

The -fuzz parameter will avoid that colors which were originally red and became corrupted due to noise also gets replaced.

This tutorial gives more information about floodfill function, such as how to replace the edge colors.

like image 143
Yamaneko Avatar answered Sep 30 '22 19:09

Yamaneko