Removing borders from film scans

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
k0syak
Posts: 2
Joined: 2015-08-27T11:57:41-07:00
Authentication code: 1151

Removing borders from film scans

Post by k0syak »

Hello.
I'm working on a batch to auto-process camera scanned film. The workflow is: shot the film placed on a lightbox > convert RAW files to TIFF with DCRAW > process with IM.
The input for IM is 16-bit TIFF images. I start by using '-negate' (when needed) and '-deskew'. Here's what it looks like at this stage:
Image
Now I need to somehow remove pieces of the film holder (near-white lines on top and bottom), areas of exposed film between frames (dark vertical bars on both sides) and in some cases also a piece of a neighboring shot (rightmost part of image).
IM Version: ImageMagick 6.9.1--6 Q16 x64 2015-06-20 on Win7 64bit.
Any ideas?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Removing borders from film scans

Post by fmw42 »

That is probably not a simple process.

Your image has nice straight line separations between the image, the red on the sides and white on the top and bottom. So, one idea is run Canny edge detection followed by Hough Line detection. Then look for intersections of the long straight lines and crop accordingly.

see
viewtopic.php?f=4&t=25405
viewtopic.php?f=4&t=25476
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Removing borders from film scans

Post by snibgo »

The image edges contain useful data. They are the clearest part of the film, so represent the blackest black. You can use this to set the black level of the image, in terms of both lightness and colour balance.

Compare the results of:

Code: Select all

convert full_DSC01696.jpg -auto-level a2.png
(Set the black lightness.)

Code: Select all

convert full_DSC01696.jpg -channel RGB -auto-level +channel a2.png
(Set the black lightness and colour.)

Better yet, this shows how the illumination varies between corners.
snibgo's IM pages: im.snibgo.com
k0syak
Posts: 2
Joined: 2015-08-27T11:57:41-07:00
Authentication code: 1151

Re: Removing borders from film scans

Post by k0syak »

First of all, thanks for the replies!
Will try to comprehend Canny/Hogh detection, but it looks like the right thing to use.
About the processing, ATM it goes like this:

1) -negate/deskew/trim/repage.

2) Take a crop from the center of the image that doesn't contain any border parts (film/holder/neighbor). The crop size and offset in percents are 88x96+7+2.3 (works so far for most shots, but obviously not ideal).

3) Process both the crop and the full image using the following command line:

Code: Select all

CONVERT <fname_in> -set colorspace srgb -colorspace rgb -auto-level -gamma <EV_correct> -sigmoidal-contrast 5x50% -set colorspace rgb -colorspace srgb <fname_out>
The '-gamma <EV_correct>' is used to reduce exposure for all images, since the original shots are exposed very brightly. <EV_correct> is currently set to 0.75 (which works mostly fine, but could be made adaptive).

4) Superimpose the crop on the full-size image. This often results in a visible difference (the full-size has lower contrast due to auto-level being affected by bright/dark borders), on this example it's not as bad but still noticeable:
Image
(vertical bar ~140 pixels from the left);
. As the default +channel setting is 'RGB,sync', the 'sync' ensures that the color channels will are modified together by the same gamma value, preserving colors, and ignoring transparency.
Then '-channel RGB' will modify each channel separately? Gotta try this ASAP :)
Thanks again!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Removing borders from film scans

Post by fmw42 »

I do not know how universal this will be for all your images, but give this a try.

It basically deskews, then sets near white (within 10%) to white, your interframe color (and colors within 30% fuzz) on the left and right sides to white and then everything else to black. Then it trims, shaves 15 pixels all around, then gets a subsequent trim crop coordinates. It then modifies the crop values to account for the shave and uses those to crop your image. You can adjust the fuzz values and shave amounts to suit.

Code: Select all

cropvals=$(convert full_DSC01696.jpg -deskew 40% \
-fuzz 5% -fill white -opaque white \
-fuzz 30% -fill white -opaque "rgb(100,70,110)" \
-fuzz 30% -fill white -opaque "rgb(155.135.135)" \
-fuzz 0 -fill black +opaque white \
-fuzz 30% -trim \
-shave 15x15 \
-fuzz 30% -format "%@" info:)

ww=`echo $cropvals | tr -cs "[0-9]" " " | cut -d\  -f1`
hh=`echo $cropvals | tr -cs "[0-9]" " " | cut -d\  -f2`
xoff=`echo $cropvals | tr -cs "[0-9]" " " | cut -d\  -f3`
yoff=`echo $cropvals | tr -cs "[0-9]" " " | cut -d\  -f4`
ww=$((ww-2*15))
hh=$((hh-2*15))
xoff=$((xoff+15))
yoff=$((yoff+15))

convert full_DSC01696.jpg -crop ${ww}x${hh}+${xoff}+${yoff} +repage full_DSC01696_cropped.png
olegk
Posts: 37
Joined: 2013-08-16T11:31:10-07:00
Authentication code: 6789

Re: Removing borders from film scans

Post by olegk »

Hi, I'm a brother of the original poster, and we are working on this project together. Hope he won't object my intervention :).
I'd like to specify the question a bit:
The main reason for wanting to get rid of the border is to let consequent autolevel/autogamma/etc. do its job. E.g. we are looking for a way to make measurement for autolevel from the inner part of the frame and then apply autolevel to the full image. This way the extreme colors of the border won't mislead autolevel. Crop is a nice-to-have feature, but with a lesser priority.
Is the above doable?

Thank you in advance,
Oleg.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Removing borders from film scans

Post by fmw42 »

-auto-level will take into account the white and red borders. So you will need to crop the image, then save its min and max statistics as variables by parsing identify -verbose, or directly from string formats at http://www.imagemagick.org/script/escape.php, or from fx: escapes using the fx equivalent operators at http://www.imagemagick.org/script/fx.php). Then use the min and max variables in -level to process your whole image.
Post Reply