#! /bin/csh -f # Last edited on 2001-11-02 19:53:18 by stolfi set usage = "$0 r g b alpha < INFILE.ppm > OUTFILE.ppm" # Removes highlights from plastic bags in IAB photos # The (r,g,b) is the estimated mean color of the # ceramic shards if ( $#argv != 4 ) then echo "usage: ${usage}"; exit 1 endif set red = "$1"; shift set grn = "$1"; shift set blu = "$1"; shift set alpha = "$1"; shift set temp = "/tmp/$$" # Compute the coefficients of a vector that is # orthogonal to (r,g,b) and coplanar with # (r,g,b) # The coefficients are such that the resulting range is [0-1]. gawk -v r="${red}" -v g="${grn}" -v b="${blu}" \ ' BEGIN{ \ printf "rgb = %s %s %s\n", r, g, b > "/dev/stderr"; \ v = (r*(r-g) + b*(b-g))/(r + g + b); \ u = g + v - r; \ w = g + v - b; \ s = (u > 0 ? u : 0) + (v > 0 ? v : 0) + (w > 0 ? w : 0); \ u = u/s; v = v/s; w = w/s; \ printf "uvw = %s %s %s\n", u, v, w > "/dev/stderr"; \ printf "%5.3f %5.3f %5.3f", u, v, w; \ } \ ' \ > ${temp}.coefs set coefs = ( ` cat ${temp}.coefs ` ) gawk -v m="${coefs}" 'BEGIN{printf "coefs = %s\n", m > "/dev/stderr";}' # Get separate components: cat > ${temp}.ppm ppmtorgb3 ${temp}.ppm mv ${temp}.red ${temp}-1.pgm mv ${temp}.grn ${temp}-2.pgm mv ${temp}.blu ${temp}-3.pgm # Extract the highlights image by combining the # Three channels with given coefficients. # Clips negative values. /bin/rm -f ${temp}-hi.pgm foreach ch ( 1 2 3 ) set coef = ${coefs[$ch]} if ( "/${coef}" !~ /-* ) then if ( ! ( -r ${temp}-hi.pgm ) ) then mv ${temp}-${ch}.pgm ${temp}-hi.pgm else pnmxarith \ -mix 1.0 ${coef} ${temp}-hi.pgm ${temp}-${ch}.pgm \ > ${temp}-tt.pgm mv ${temp}-tt.pgm ${temp}-hi.pgm endif endif end foreach ch ( 1 2 3 ) set coef = ${coefs[$ch]} if ( "/${coef}" =~ /-* ) then pnmxarith \ -mix 1.0 ${coef} ${temp}-hi.pgm ${temp}-${ch}.pgm \ > ${temp}-tt.pgm mv ${temp}-tt.pgm ${temp}-hi.pgm endif end xv ${temp}-hi.pgm # Subtract some fraction "alpha" of the highlights. pnmxarith -mix 1.0 -${alpha} ${temp}.ppm ${temp}-hi.pgm