# Resolution Reduction (Image Processing)

// Function for resolution reduction (useful for transforming/handling large images)
// Original code written by Peter Rehbein, Goethe-University, Frankfurt, Germany
Function Shrink(image, shrinkfactor)

Wave image
Variable shrinkfactor

if(ShrinkFactor<=0)
return -1
endif

print "ShrinkFactor:"+num2str(shrinkfactor)
print "====="

print "Height (original):"+num2str(height)
print "Width (original):"+num2str(width)
print "====="
// Check whether image dimensions are dividable by shrink factor:

Variable check_height
Variable check_width

Variable new_height
Variable new_width

Variable u,v
Variable d,e

//Loop while height is not dividable by shrinkfactor and enlarge picture appropriately
//(New rows are filled with copied adjacent data)
check_height = mod(height,shrinkfactor)

if(check_height!=0)
print "Height not dividable by "+num2str(shrinkfactor)+"!"
print "Starting correction!"
print "====="
for(u=0;check_height!=0;u+=1)
print "Actual Height: "+num2str(height)
new_height = height+1
for(e=0;e<width;e+=1)
//print "e: "+num2str(e)
endfor
height = new_height
print "New Height: "+num2str(new_height)
check_height = mod(height,shrinkfactor)
endfor
endif
// end check

//Loop while width is not dividable by shrinkfactor and enlarge picture appropriately
//(New columns are filled with copied adjacent data)
check_width = mod(width,shrinkfactor)
if(check_width!=0)
print "Width not dividable by "+num2str(shrinkfactor)+"!"
print "Starting correction!"
print "====="
for(v=0;check_width!=0;v+=1)
print "Actual Width: "+num2str(width)
new_width = width+1
for(d=0;d<height;d+=1)
//print "d: "+num2str(d)
endfor
width = new_width
print "New Width: "+num2str(new_width)
check_width = mod(width,shrinkfactor)
endfor
endif
// end check

Variable sImage_Height = Height/ShrinkFactor
Variable sImage_Width = Width/ShrinkFactor
print "====="
print "SmallImage Height :"+num2str(sImage_Height)
print "SmallImage Width: "+num2str(sImage_Width)

Make /O /N=(sImage_Height, sImage_Width) sImage

Variable i,j,k,l

Variable avg

//print "====Report===="
for(i=0;i<sImage_Height;i+=1) //correct
for(j=0;j<sImage_Width;j+=1) //correct

k=i*ShrinkFactor
l=j*ShrinkFactor

//print "===="
//print "k:"+num2str(k)
//print "l:"+num2str(l)

WaveStats /Q Region // Determine average pixel value
avg = V_avg
//print "i: "+num2str(i)
//print "j: "+num2str(j)
//print "Average (region): "+num2str(avg)
//print "i: "+num2str(i)
//print "j: "+num2str(j)
//print "===="
sImage [i] [j] = avg // write average pixel value into smaller image
endfor
endfor

End
For example, in this part

WaveStats /Q Region // Determine average pixel value
avg = V_avg
sImage [i] [j] = avg // write average pixel value into smaller image

you could exchange `V_avg` for `V_min` or `V_max` and write these values to the shrunken image instead :)
(This is actually what I wrote the snippet for)

Besides, it was fun to code ^^
One more suggestion:
it is usually a good idea to avoid memory allocations. Your code uses Duplicate followed by WaveStats. You could skip the Duplicate completely if you used ImageStats instead of WaveStats. Also, using either WaveStats or ImageStats you should be able to see some performance improvement with a /M=1 flag.

A.G. Forum Support Gallery