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)
DoAlert /T="Alert" 0,"ShrinkFactor must be greater than 0!"
return -1
endif
Duplicate /O image adjusted_image
print "ShrinkFactor:"+num2str(shrinkfactor)
print "====="
Variable height = dimsize(adjusted_image,0)
Variable width = dimsize(adjusted_image,1)
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
Redimension/N=(new_height,-1) adjusted_image
for(e=0;e<width;e+=1)
//print "e: "+num2str(e)
adjusted_image [new_height-1] [e] = adjusted_image [new_height-2] [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
Redimension/N=(-1,new_width) adjusted_image
for(d=0;d<height;d+=1)
//print "d: "+num2str(d)
adjusted_image [d] [new_width-1] = adjusted_image [d] [new_width-2]
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)
Wave adjusted_image
Duplicate/O/R=[k,k+ShrinkFactor-1][l,l+ShrinkFactor-1] adjusted_image,region // Extract region
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
Forum
Support
Gallery
Igor Pro 10
Learn More
Igor XOP Toolkit
Learn More
Igor NIDAQ Tools MX
Learn More
June 5, 2014 at 12:12 pm - Permalink
you could exchange for or and write these values to the shrunken image instead :)
(This is actually what I wrote the snippet for)
Besides, it was fun to code ^^
June 6, 2014 at 12:13 pm - Permalink
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.
June 6, 2014 at 12:27 pm - Permalink
June 6, 2014 at 12:31 pm - Permalink