// -*- C++ -*- // $RCSfile: equalize.C,v $ // $Revision: 1.2 $ // $Author: langer $ // $Date: 2000/09/06 20:11:30 $ /* This software was produced by NIST, an agency of the U.S. government, * and by statute is not subject to copyright in the United States. * Recipients of this software assume all responsibilities associated * with its operation, modification and maintenance. However, to * facilitate maintenance we ask that before distributing modifed * versions of this software, you first contact the authors at * oof_manager@ctcms.nist.gov. */ #include "menuDef.h" #include "image.h" #include "ishape.h" #include "goof.h" #include //************************************************************************** double Image::totalGray(const ImageRect &window, double averageValue) const { double sum = 0; Cell_coordinate cell; ArrayIterator iter(window); while(iter(cell)) { if(contains(cell)) //get the total of the cell colors in the window sum+=(*this)[cell].query_red(); else sum+=averageValue; } return sum; } //************************************************************************** double Image::totalBright(const ImageRect &window, double averageValue) const { double sum = 0; Cell_coordinate cell; ArrayIterator iter(window); while(iter(cell)) { if(contains(cell)) //get the total of the cell colors in the window sum+=(*this)[cell].brightness(); else sum+=averageValue; } return sum; } //************************************************************************** void Image::averageSmooth(int sizeX, int sizeY) { //equalizes the image by replacing each pixel with the average of a window //(specified by the function parameters) of its neighbors const Cell_coordinate yhat(0, 1); //unit vector j, <0, 1> const Cell_coordinate xhat(1, 0); //unit vector i, <1, 0> sizeX=abs(sizeX); sizeY=abs(sizeY); int windowArea=(2*sizeX+1)*(2*sizeY+1); Cell_coordinate diagonal(sizeX, sizeY); Cell_coordinate diagonal2(-sizeX, sizeY); double averageValue; //create a copy of the calling object so calculations aren't based //on things that have already been equalized Image newImage(query_width(), query_height(), isgray()); if(isgray()){ //compute average gray of entire image double grayTotal=0; ArrayIterator it(*this); Cell_coordinate cc; while(it(cc)){ grayTotal+=(*this)[cc].query_red(); } averageValue=grayTotal/(query_height()*query_width()); //pixels that don't have enough surrounding pixels to make up the window //have the image's average gray substituted in for these "missing" pixels grayTotal=0; bool first=true; //true for the first iteration of the loop ArrayIterator iter(*this); Cell_coordinate cell, lastPixel; while(iter(cell)) if(current_goof->active(cell)){ ImageRect window(cell+diagonal, cell-diagonal); //averaging region if(first){ grayTotal = totalGray(window, averageValue); first=false; } else{ //now beyond the very first pixel if(cell-lastPixel==xhat){ //window has moved one pixel in the x direction Cell_coordinate mover1=cell+diagonal2; //bottom left of window Cell_coordinate mover2=cell+diagonal; //bottom right of window for(int i=0; iactive(cell)){ ImageRect window(cell+diagonal, cell-diagonal); //averaging region if(first) { brightTotal = totalBright(window, averageValue); first=false; } else{ //now beyond the very first pixel if(cell-lastPixel==xhat){ //window has moved one pixel in the x direction Cell_coordinate mover1=cell+diagonal2; //bottom left of window Cell_coordinate mover2=cell+diagonal; //bottom right of window for(int i=0; imsout << ms_info << "Equalization complete: average value = " << averageValue << endl << ms_normal; }