#! /bin/bash # Last edited on 2008-02-04 20:31:37 by stolfi usage="${0##*/} IMG" # Reads from the directory "IAB-2002/raw-sorted" the # files # # "{IMG}-s.jpg" image to be segmented (1/4 linear size) # "{IMG}.fctrs" fragment centers # "{IMG}.grpts" grid reference points # "{IMG}.grays" centers of gray/color calibration patches # # Produces a segmentation of the image "{IMG}-s.jpg" into # regions, using the IFT method. # # Looks for {N+1} regions where {N} is the number of fragments listed # in the file "{IMG}.fctrs". Region 1 is the background, including all # calibration objects. Regions 20 onwards are the fragments, in the # order listed in "{IMG}.fctrs". # # Writes the following files: # # "{IMG}-s-lab.pgm" pixel classification image (1/4 linear size). # "{IMG}-s.boxes" bounding boxes of regions in "{IMG}-s-lab.pgm". # # In the classificaton image "{IMG}-s-lab.pgm", the value of a pixel is # the ID number of the region to which the label has been assigned. # # The file "{IMG}-s.boxes" has one line for each region. # Each line contains fields {LABEL HMIN VMIN HSIZE VSIZE} where # {LABEL} is the region's ID number, and {HMIN VMIN HSIZE VSIZE} # is a bounding box for the region pixels in "{IMG}-s.jpg", # as in "pnmcut". If the region is non-empty, the bounding box # is the smallest enclosing rectangle, plus some safety margin. # If the region is empty, the last four fields are {0 0 0 0}. if [ $# -ne 1 ]; then echo "usage: ${usage}"; exit 1; fi imgname="$1"; shift; tools="${STOLFIHOME}/projects/fragments/IAB-2002/tools" ( cd ${tools}/. ) # Full image size (for scaling points) fulsize=2048 # Small version of image smajpg="${imgname}-s.jpg" smasize=512 # Small image with fragment centers and labels (display only) smapts="${imgname}-n.jpg" # Working images for segmentation procedure tmpname="/tmp/$$" # Output files smalabs="${imgname}-s-lab.pgm" smaboxes="${imgname}.boxes" smafrags="${imgname}-s-frags.jpg" smaground="${imgname}-s-ground.jpg" # Image scaling factors smafulscale=`gawk -v x=${smasize} -v y=${fulsize} 'BEGIN{printf "%.6f\n", x/y;}'` fulsmascale=`gawk -v x=${fulsize} -v y=${smasize} 'BEGIN{printf "%3d\n", x/y;}'` if [ $((fulsmascale * smasize)) -ne $((fulsize)) ]; then echo "incongruent sma:ful sizes"; exit 1 fi # Seed file seeds="${tmpname}.seeds" /bin/rm -f ${seeds} /bin/touch ${seeds} grpts="${imgname}.grpts" if [ -r ${grpts} ]; then echo "seedifying grid refpoints..." 1>&2 cat ${grpts} \ | ${tools}/refpoints-to-seeds \ -v scale=${smafulscale} -v labelStart=1 -v labelStep=0 \ >> ${seeds} fi grays="${imgname}.grays" if [ -r ${grays} ]; then echo "seedifying gray/color patch centers..." 1>&2 cat ${grays} \ | ${tools}/refpoints-to-seeds \ -v scale=${smafulscale} -v labelStart=1 -v labelStep=0 \ >> ${seeds} fi fctrs="${imgname}.fctrs" if [ -r ${fctrs} ]; then echo "seedifying fragment centers..." 1>&2 cat ${fctrs} \ | ${tools}/refpoints-to-seeds \ -v scale=${smafulscale} -v labelStart=20 -v labelStep=1 \ >> ${seeds} fi # Create chroma-only PPM image for segmentation smappm="${tmpname}-s.ppm" smayuv="${tmpname}-yuv.ppm" echo "converting ${smajpg} to ppm format..." # Hack to correct white balance convert ${smajpg} PPM:- \ | ppmtoyuv \ -rgbscale 0.741 0.696 0.685 \ | ppmtoyuv \ -inverse \ > ${smappm} echo "converting image to Yuv space..." cat ${smappm} \ | convert -median 2 -gaussian 5x2 PPM:- PPM:- \ | ppmtoyuv -bias 0.005 \ -yuvscale 0 1 1 \ | ppmtoyuv -bias 0.005 \ -yuvscale 1 1 1 \ -inverse \ > ${smayuv} segmargin=1 echo "segmenting Yuv image..." cat ${smayuv} \ | pnmift \ -seedPixels ${seeds} \ -radius 2.3 \ -costFunction maxdiff \ -output ${tmpname} \ -boxes ${tmpname}.boxes -margin ${segmargin} \ -maxCostPixel 255 \ -maxCostValue 1000 \ || exit 1 # Make verification images: echo "creating fragment-only image..." cat ${tmpname}-labels.pgm \ | pgmselect -range 20 255 \ | pnmxarith -multiply ${smappm} - \ | convert -quality 95 PPM:- JPG:- \ > ${tmpname}-frags.jpg echo "creating background-only image..." cat ${tmpname}-labels.pgm \ | pgmselect -range 1 1 \ | pnmxarith -multiply ${smappm} - \ | convert -quality 95 PPM:- JPG:- \ > ${tmpname}-ground.jpg display \ ${smajpg} ${smapts} ${smayuv} \ ${tmpname}-frags.jpg ${tmpname}-ground.jpg \ ${tmpname}-*.pgm # Save interesting images: mv ${tmpname}-labels.pgm ${smalabs} mv ${tmpname}.boxes ${smaboxes} mv ${tmpname}-frags.jpg ${smafrags} mv ${tmpname}-ground.jpg ${smaground} /bin/rm ${smayuv} ${seeds} ${segboxes} /bin/rm ${tmpname}-*.pgm ${tmpname}-*.ppm