/*------------------------------------------------------------------------ Hit ctrl-enter to execute. Description: Digital Micrograph Script BKS.s Version 1.1 Subtract Slowly-Varying Background from an Image This removes slowly varing background from an image by successive reduction and bilinear interpolation scaling Original Version 25-Mar-94 by J. R. Minter (Eastman Kodak) based upon suggestions by M. Leber (Gatan) Revision History Updated to test for image size & remove (-) pixels 26-Mar-94 JRM Simplified and annotated by Paul Thomas March 2001 Modified to divide image with background image. This is usefull for MonoCL images where the collection efficiency isnīt uniform for field of view larger then the central focus spot of the collector. Also applies median filter to background image Modified by Roland Ries Oct 2006 --------------------------------------------------------------------------*/ /* This method calulates the slowly-varying background and divides it It is passed the source image, and returns the filtered image */ image Remove_Background( image source_img) { // Ensure image is 2d and, get the size of the source image if ( source_img.ImageGetNumDimensions() != 2) Throw("Select a 2d image for this operation") number sizeX, sizeY, filter_strength=16 Get2dSize(source_img, sizeX, sizeY) GetNumber("Enter the Filter strength [Pix]\n small values: high frequency fluctuations\n high values: low frequency fluctuations", filter_strength, filter_strength) // Make a copy of the image and reduce it to about 8 x 8 image reduced_img = RealImage("Reduced", 4, sizeX, sizeY) reduced_img = source_img while ( reduced_img.ImageGetDimensionSize(0) >= filter_strength \ && reduced_img.ImageGetDimensionSize(1) >= filter_strength ) { Reduce(Reduced_Img) } // Apply median filter to reduced image reduced_img = Median(reduced_img) // Rescale the reduced image to make a background // Note warp uses bilinear interpolation to rescale number sizeXr, sizeYr Get2DSize(Reduced_img, sizeXr, sizeYr) number Xfactor = (sizeX-1)/(sizeXr-1) number Yfactor = (sizeY-1)/(sizeYr-1) image Bkg_img = RealImage("Background", 4, sizeX, sizeY) Bkg_img = Warp(Reduced_Img, icol/Xfactor, irow/Yfactor) // Divide the source image through the background image, // and give the output image a sensible title string out_title = source_img.GetName() + " (variations in collection efficiency removed)" image out_img := RealImage(out_title, 4, sizeX, sizeY) out_img = source_img / Bkg_img // Rescale grey values out_img = average(reduced_img) * out_img out_img.ImageCopyCalibrationFrom( source_img ) return out_img } // Example of use; call the method with the front-most image ShowImage(Remove_Background(GetFrontImage()))