Fred's ImageMagick Scripts



    Licensing:

    Copyright © Fred Weinhaus

    My scripts are available free of charge for non-commercial (non-profit) use, ONLY.

    For use of my scripts in commercial (for-profit) environments or non-free applications, please contact me (Fred Weinhaus) for licensing arrangements. My email address is fmw at alink dot net.

    If you: 1) redistribute, 2) incorporate any of these scripts into other free applications or 3) reprogram them in another scripting language, then you must contact me for permission, especially if the result might be used in a commercial or for-profit environment.

    Usage, whether stated or not in the script, is restricted to the above licensing arrangements. It is also subject, in a subordinate manner, to the ImageMagick license, which can be found at: http://www.imagemagick.org/script/license.php

    Please read the Pointers For Use on my home page to properly install and customize my scripts.

NEG2POS


Converts from a (scanned film) negative image to a positive image.

Download Script

last modified: December 11, 2023



USAGE: neg2pos [-p position] [-C color] [-o offset] [-m method] [-r refinefuzz]
[-t trimfuzz ] [-a area] [-s stretch] [-l cliplow] [-h cliphigh]
[ -c crop] [-cb colorbalance] [-P percent] [-s showmask] infile outfile
USAGE: neg2pos [-help]

-p .... position ........ position to extract perforation region background
......................... color; choices are: northwest, northeast, southwest,
......................... southeast, corners, or x,y coord; default=northwest
-o .... offset .......... offset from position when position is not x,y coord;
......................... comma separate non-negative x,y pair of integers;
......................... default="1,1"
-C .... color ........... color of the perforations region; any valid IM opaque
......................... color may be use; default is to use the -p position and
......................... -o offset to get the color; if color is specified,
......................... the -p and -o arguments will be ignored.
-m .... method .......... method to use to refine perforation region
......................... background color and get crop bounds; choices are:
......................... floodfill, threshold or none; default=floodfill
-r .... refinefuzz ...... fuzz value for refining the perforation region
......................... background color; 0<=float<=100; default=5
-t .... trimfuzz ........ fuzz value for trimming of image region with or
......................... without crop; value or "none"; 0<=float<=100;
......................... default=10
-a .... area ............ connected components area threshold when
......................... method=threshold; integer>=0; default=10000
-s .... stretch ......... stretch image channels dynamic range; options are:
......................... none (n), together (t), or separate (s);
......................... default=separate
-l .... cliplow ......... clip percent on low end of histogram;
......................... float; 0<=cliplow<=100; default=1
-h .... cliphigh ........ clip percent on high end of histogram;
......................... float; 0<=cliphigh<=100; default=1
-c .... crop ............ crop the central image; yes or no; default=yes
-cb ... colorbalance .... color balance mode; choices are gray,
......................... white, both, auto or none; default=none
-P .... percent ......... percent threshold for colorbalance processing;
......................... 0<float<100; default=10
-S .... showmask ........ show the mask that delineates the perforation region
......................... from the interior image; choices are: view, save or
......................... none

PURPOSE: To convert from a (scanned film) negative image to a positive image.

DESCRIPTION: NEG2POS converts from a (scanned film) negative image to a positive image when the negative contains the exterior perforation region. Options include: stretching the image channels' dynamic range with without clipping, connected components processing to remove small regions before trimming or cropping and color balancing via white balance, gray balance or automatic.

ARGUMENTS:

-p position ... POSITION for extracting the perforation region background color. The choices are: northwest, northeast, southwest, southeast, corners, or an x,y coordinate. The default=northwest. The choice of corners gets the color at each corner and takes the average of the four.

-o offset ... OFFSET from the directional positions. Ignored for the case when the position is a coordinate pair. Values are a comma separated non-negative x,y pair of integers. The default="1,1".

-C color ... COLOR of the perforations region. Any valid IM opaque color may be use. The default is to use the -p position and -o offset to get the color. If color is specified, the -p and -o arguments will be ignored.

-m method ... METHOD to use to refine the perforation region background color and to get the crop bounds of the interior image. The choices are: floodfill (f), threshold (t) or none (n). The default=floodfill. Note that method=threshold needs my script innercrop. Also if color is specified, then refining will be disabled for -m floodfill, since there is no coordinate to seed the floodfill. It will work for -m threshold.

-r refinefuzz ... REFINEFUZZ is the fuzz value for refining the perforation region background color. Values are 0<=float<=100. The default=5. The refinefuzz value is often much smaller for method=threshold than for method=floodfill

-t trimfuzz ... TRIMFUZZ is the fuzz value for the trimming of the image region with or without final cropping. The choices are a numerical value in the range 0<=float<=100 or "none". The default=10.

-a area ... AREA is the connected components area threshold when the method=threshold. Values are integers>=0. The default=10000.

-s stretch ... STRETCH the image channels' dynamic range. The options are: none (n), together (t) or separate (s). The default=separate.

-l cliplow ... CLIPLOW is the cumulative percent at the low end of the histogram whose graylevel will be autostretched to full black. Values are floats between 0 and 100. If cliplow=0, then the autostretch will locate the minimum value in the channel histogram. The default=1.

-h cliphigh ... CLIPHIGH is the cumulative percent at the high end of the histogram whose graylevel will be autostretched to full white. Values are floats between 0 and 100. If cliplow=0, then the autostretch will locate the maximum value in the channel histogram. The default=1.

-c crop ... CROP the central image, if possible depending upon whether color is specified and the refining method used. Choices are yes or no. The default=yes.

-cb colorbalance ... COLORBALANCE mode. The choices are none, gray, white, both, or auto. The default=auto. Both will be an equal blend of gray and white. Auto will try to choose the better of gray or white depending upon which one's percent selected pixels' rmse value is the smallest.

-P percent ... PERCENT is the percent threshold for detecting gray/white in the image for auto color balance. Values are 0<floats<100. The default=10.

-S showmask ... SHOWMASK presents the mask image that delineates the perforation region from the interior image. The choices are: view (v), save (s) or none (n). The default=none. If the value is save, then the mask will be the input file name with "_mask.gif" appended.

REQUIREMENTS: Note that method=threshold needs my script innercrop.

REFERENCES AND TUTORIALS: http://www.instructables.com/id/How-to-Convert-Digitized-Film-Negatives-into-Quali/?ALLSTEPS http://www.computer-darkroom.com/tutorials/tutorial_6_1.htm http://photo.stackexchange.com/questions/23942/does-a-filter-exist-to-color-correct-color-negatives-when-copying-them-with-a-ds https://www.youtube.com/watch?v=B2gLT0SWzpQ http://petapixel.com/2012/05/18/how-to-scan-film-negatives-with-a-dslr/ https://www.youtube.com/watch?v=iMO50AlGyrw

CAVEAT: No guarantee that this script will work on all platforms, nor that trapping of inconsistent parameters is complete and foolproof. Use At Your Own Risk.


EXAMPLES


Example 1 - Method: Floodfill
(note: arrow head in perforation area has been erased)

Original
(source)

Arguments:
-p southeast -m floodfill -r 5 -cb none -S save



Example 2 - Method: Threshold
(note: arrow head is in perforation area)

Original
(source)

Arguments:
-p southeast -m threshold -r 0.5 -cb none -S save



Example 3

Original
(source)

Arguments:
-p 20,20 -m floodfill -r 10 -cb none -S save



Example 4

Original
(source)

Arguments:
-p southeast -m floodfill -r 5 -l 3 -h 1 -c no -cb white -S save



Example 5

Original
(source)

Arguments:
-p northwest -m floodfill -r 5 -c no -cb none -S save

Arguments:
-p northwest -m floodfill -r 5 -c no -cb gray -S none

Arguments:
-p northwest -m floodfill -r 5 -c no -cb white -S none

Arguments:
-p northwest -m floodfill -r 5 -c no -cb auto -S none

Arguments:
-p northwest -m floodfill -r 5 -c no -cb both -S none



What the script does is as follows for the case of method=floodfill

  • Gets the color from the desired position
  • Does a fuzzy floodfill at the position using the found color
    to create a mask that separates the perforation area (black)
    and the image area (white) and also gets the average color
    from the perforation area using the mask
  • Uses connected components labeling to get the bounding box of
    the largest white region assuming it is the image area
  • Crops the image at those coordinates
  • Divides the image by the color
  • Trims the color corrected negative
  • Negates the trimmed image
  • Applies a stretch of the dynamic range
  • Optionally applies color balancing