#! /bin/bash # Last edited on 2008-02-04 20:31:19 by stolfi usage="${0##*/} IMAGENAME" # Reads from the directory "IAB-2002/raw-sorted/{IMAGENAME}" the # files # # "{IMAGENAME}-s.jpg" image to be segmented (1/4 linear size) # "{IMAGENAME}.jpg" image to be cut (full-size) # "{IMAGENAME}.fctrs" fragment centers # "{IMAGENAME}.grpts" grid reference points # "{IMAGENAME}.grays" centers of gray/color calibration patches # # Produces a segmentation of it using the IFT method. if [ $# -ne 1 ]; then echo "usage: ${usage}"; exit 1; fi imgname="$1"; shift; tools="${STOLFIHOME}/projects/fragments/IAB-2002/tools" ( cd ${tools}/. ) tmpname="/tmp/$$" # Full image fuljpg="${imgname}.jpg" fulppm="${tmpname}.ppm" fulsize=2048 # Small version of image smajpg="${imgname}-s.jpg" smasize=512 # Input images for segmentation procedure # outpref="${imgname}-seg" outpref="${tmpname}-seg" segsize=512 # Extracted segments segpref=${tmpname}-seg # Image scaling factors segsmascale=`gawk -v x=${segsize} -v y=${smasize} 'BEGIN{printf "%.6f\n", x/y;}'` segfulscale=`gawk -v x=${segsize} -v y=${fulsize} 'BEGIN{printf "%.6f\n", x/y;}'` fulsegscale=`gawk -v x=${fulsize} -v y=${segsize} 'BEGIN{printf "%3d\n", x/y;}'` if [ $((fulsegscale * segsize)) -ne $((fulsize)) ]; then echo "incongruent seg: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=${segfulscale} -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=${segfulscale} -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=${segfulscale} -v labelStart=20 -v labelStep=1 \ >> ${seeds} fi convert ${smajpg} PPM:- \ | pnmnlfilt 0.5 1.0 \ | pnmscale ${segsmascale} \ | pnmnlfilt 0.5 1.0 \ | ppmtoyuv -weight 0 1 1 \ | ppmtoyuv -weight 1 1 1 -inverse \ > ${outpref}-yuv.ppm segmargin=1 fulmargin=$(( segmargin * fulsegscale )) cat ${outpref}-yuv.ppm \ | pnmift \ -seedPixels ${seeds} \ -radius 4 \ -costFunction maxdiff \ -output ${outpref} \ -boxes ${outpref}-boxes.txt -margin ${segmargin} \ -maxCostPixel 255 \ -maxCostValue 1000 \ || exit 1 display ${outpref}-yuv.ppm ${outpref}-*.pgm # Generate the fragment/patch masks segboxes=${tmpname}.segboxes # Reformat bounding boxes as single words: cat ${outpref}-boxes.txt \ | gawk '(NF==5){ printf "%03d,%d,%d,%d,%d\n", $1,$2,$3,$4,$5; }' \ > ${segboxes} echo "converting ${fuljpg} to ${fulppm}..." 1>&2 convert ${fuljpg} ${fulppm} printf "extracting segments:" 1>&2 for box in `cat ${segboxes}` ; do fld=( ${box//,/ } ) seg=${fld[0]}; hmin=${fld[1]}; vmin=${fld[2]}; hsz=${fld[3]}; vsz=${fld[4]} printf " %s" ${seg} 1>&2 if [ ${hsz} -eq 0 ]; then printf "-" ${seg} 1>&2 else # Expand box to full-scale image fulhmin=$(( hmin * fulsegscale )) fulvmin=$(( vmin * fulsegscale )) fulhsz=$(( hsz * fulsegscale )) fulvsz=$(( vsz * fulsegscale )) # Write full-scale segment box file printf " %5d %5d %5d %5d\n" \ ${fulhmin} ${fulvmin} ${fulhsz} ${fulvsz} \ > ${segpref}-${seg}.bbox # Extract a binary mask for the segment # Segment 1 (background) must be shrunk, others must be widened: mskexpand=${fulmargin} if [ ${seg} -eq 1 ]; then mskexpand="-${fulmargin}"; fi cat ${outpref}-labels.pgm \ | pgmselect -range ${seg} $seg} \ | pnmcut ${hmin} ${vmin} ${hsz} ${vsz} \ | ${tools}/scale-segment-mask ${fulsegscale} ${mskexpand} \ > ${tmpname}-${seg}-mask.pgm # Extract the segment from the full image, mask out the background: pnmcut ${fulhmin} ${fulvmin} ${fulhsz} ${fulvsz} ${fulppm} \ | pnmxarith -multiply ${tmpname}-${seg}-mask.pgm - \ | convert -quality 98 PPM:- ${segpref}-${seg}.jpg fi done echo "." 1>&2 display ${smajpg} ${segpref}-*.jpg /bin/rm ${outpref}-yuv.ppm ${outpref}-*.pgm ${seeds} ${segboxes} /bin/rm ${fulppm} /bin/rm ${tmpname}-${seg}-mask.pgm ${segpref}-*.jpg