Yet more autocropping (removing dark borders)

Discuss digital image processing techniques and algorithms. We encourage its application to ImageMagick but you can discuss any software solutions here.
Post Reply
ozbigben
Posts: 27
Joined: 2012-03-25T02:15:27-07:00
Authentication code: 8675308

Yet more autocropping (removing dark borders)

Post by ozbigben »

Hi All

Having just finished a large project involving cropping and adjusting levels for 9000 images with Photoshop actions I decided it was worth looking at IM while the pain points were still fresh in our minds to see if there was a more efficient way. For now, I'm restricting things to WinXP/7 and DOS batch files for reasons that have nothing to do with which OS is better.

The images: Photographs, copied using a DSLR camera on a dark background
Photoshop action: Add dark border, magic wand select near 0,0, smooth selection to include noise (dust etc...), invert selection, expand selection to add some padding around the photograph and crop.

This worked pretty well with only a few under/over-cropped images. After some hunting around and exploring much further into DOS than I'd ever been before I managed to hack some of the examples together to do pretty much the same sort of thing. To get the numbers for the filtering in the first step I gradually added commands to produce the BW image that would be the "mask". Some of the numbers would change for our full resolution images but I don't think you could automatically assume the size of a noise particle (dust, hairs etc...) as a percentage of the image size.

Code: Select all

setlocal
FOR /F "tokens=*" %%i in ('convert %1 -virtual-pixel edge -auto-level -threshold 15%% -morphology smooth octagon:4^
 -morphology dilate octagon:2 -morphology dilate octagon:10 -bordercolor black -border 1x1 -trim -format "%%wx%%h%%O" info:') do SET IMTEST=%%i

convert %1   -bordercolor black -border 1x1 -crop %IMTEST% +repage %~n1_trim.tif
For the most part this works pretty well... possibly even better than the Photoshop action I had. The main image type that doesn't work so well is a contact sheet of negatives. The size of the smoothing filter was set to remove a bit of fluff that got caught on the background material, but this also excludesthe text on the edge of the film frame, and the thin white edge of the print itself

Image

I may look at some edge detection processing to see if it's possible to exaggerate the edge of the print without doing the same for a particle of the same width... any suggestions?

Cheers

Ben
ozbigben
Posts: 27
Joined: 2012-03-25T02:15:27-07:00
Authentication code: 8675308

Re: Yet more autocropping (removing dark borders)

Post by ozbigben »

One last test got an improvement. Apart from tweaking the threshold, I added a "-morphology close" with the idea of making the text in the margin of the frames become larger particles that wouldn't be entirely removed by the following smooth.

Code: Select all

FOR /F "tokens=*" %%i in ('convert %1 -virtual-pixel edge -auto-level -threshold 40%% -morphology Close Disk -morphology smooth octagon:4^
 -morphology dilate octagon:2 -morphology dilate octagon:10 -bordercolor black -border 1x1 -trim -format "%%wx%%h%%O" info:') do SET IMTEST=%%i
An edge detection may still be useful for the edge of the print (for preservation jobs we'd have to ensure that the entire print was present in the image), but in this case it was OK to crop out black areas of the print outside of the area covered by the negatives.
Post Reply