September 2016

Awards

Give this entry an award

Files

File Uploader Date
cards.png
cards
dholth 2016/09/08 13:12

Diary Entries

Scanning artwork with OpenCV

I wanted my game to have cards with hard-drawn artwork in my game, so I wrote an image processing script to cut a scanned image into parts. Now I can draw eight images on a piece of paper, scan it, and automatically split the interesting parts into separate images.

It works by extending the find_squares() example from OpenCV. First, use Inkscape to make a page with black squares on each of the four corners of the area you want to be scanned. Then draw a grid of lines to show where you should draw each individual picture, leaving some white space around the corner squares so as not to disturb the square detection algorithm. Print the page.

Now draw! Then, scan the page back in, and run the script on the scanned image.

The script finds all squares "of a certain size" relative to the total size of the scanned image, so it should be independent of the scanner resolution. It will find each of the four squares many times. Then it bins the squares into the four corners of the page based on whether that square is inside the general area expected. It finds the boundary of the page by finding the inside corner of each square (the point closest to the center of the page) and, for each corner, it takes the average of these points from the many times each corner square was detected to refine the page content boundary.

Now that we know the top left and bottom right points of our content area, it's a simple matter to slice it up into equal parts. My page is 2x4 images, each having a 3:2 aspect ratio. These will be processed further to remove the guide lines scanned from the paper and to scale them down to the size the game needs.

By using some simple image recognition I should get perfect image slices from my scan, even if the page was slightly misaligned on the scanner, independent of the resolution of the scan. If it was necessary to tolerate greater scanner misalignment, the script could be extended to rotate and warp the page by taking the other two corners into account. It would also be nice to print a unique 2D barcode onto each page so as to automatically organize the scanned artwork, automatically replacing the correct images if you re-scanned a page.

Add a comment