Comparing files LDV\Segmentor.cpp and SEGMENTATION_CODE_FROM_JEFF_WELTY\SEGMENTOR_FOR_BOB.CPP
***** LDV\Segmentor.cpp

#include "stdafx.h"
#include "ldv.h"
#include "Segmentor.h"
***** SEGMENTATION_CODE_FROM_JEFF_WELTY\SEGMENTOR_FOR_BOB.CPP

#include <stdio.h>
#include "Segmentor.h"
*****

***** LDV\Segmentor.cpp

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

***** SEGMENTATION_CODE_FROM_JEFF_WELTY\SEGMENTOR_FOR_BOB.CPP

/*  #ifdef _DEBUG  */
/*  #undef THIS_FILE  */
/*  static char THIS_FILE[]=__FILE__;  */
/*  #define new DEBUG_NEW  */
/*  #endif  */

*****

***** LDV\Segmentor.cpp

CSegmentor::CSegmentor(LPCTSTR FileName, LPCTSTR OutputFileName, int ColorOption)
{
        m_Valid = FALSE;
        m_Rows = m_Columns = 0;
        m_NumberOfBasins = 0;

        m_Area = NULL;
        m_MaxHt = NULL;
        m_MaxPix = NULL;
        m_BasinDeleted = NULL;

        // do the segmentation
        CreateFromImageFile(FileName);
        CountAndSortPixels();
***** SEGMENTATION_CODE_FROM_JEFF_WELTY\SEGMENTOR_FOR_BOB.CPP

int CSegmentor::Segment(int ColorOption)
{
        CountAndSortPixels();
*****

***** LDV\Segmentor.cpp

//              LabelPeaks();
                
                if (OutputFileName[0] == '\0')
                        SaveOutputImage("watershed.bmp", ColorOption);
                else
                        SaveOutputImage(OutputFileName, ColorOption);
        }
}
***** SEGMENTATION_CODE_FROM_JEFF_WELTY\SEGMENTOR_FOR_BOB.CPP

                return 1 ;

//              LabelPeaks();
        }

        return 0 ;
}
*****

***** LDV\Segmentor.cpp

CSegmentor::~CSegmentor()
{
        if (m_Valid)
                Empty();
}
***** SEGMENTATION_CODE_FROM_JEFF_WELTY\SEGMENTOR_FOR_BOB.CPP

CSegmentor::CSegmentor(unsigned char **image, int rows, int cols, unsigned long **output_image, int ColorOption, int PeakFilter
ize, int LowFilterSize)
{
    m_Valid = FALSE;
    m_Rows = m_Columns = 0;
    m_NumberOfBasins = 0;

    m_Area = NULL;
    m_MaxHt = NULL;
    m_MaxPix = NULL;
    m_BasinDeleted = NULL;

    CreateFromByteArray(image, rows, cols, PeakFilterSize, LowFilterSize) ;

    // do the segmentation
    if(Segment(ColorOption)) {
        SaveOutputImageToArray(output_image, ColorOption) ;
    }
}
*****

***** LDV\Segmentor.cpp

BOOL CSegmentor::IsValid()
{
        return(m_Valid);
}
***** SEGMENTATION_CODE_FROM_JEFF_WELTY\SEGMENTOR_FOR_BOB.CPP

CSegmentor::~CSegmentor()
{
        if (m_Valid)
                Empty();
}
*****

***** LDV\Segmentor.cpp

void CSegmentor::Empty()
***** SEGMENTATION_CODE_FROM_JEFF_WELTY\SEGMENTOR_FOR_BOB.CPP

BOOL CSegmentor::IsValid()
{
        return(m_Valid);
}

void CSegmentor::Empty()
*****

***** LDV\Segmentor.cpp

        m_OriginalImage.Empty();

        // delete working arrays
        delete [] m_WorkingImage;
***** SEGMENTATION_CODE_FROM_JEFF_WELTY\SEGMENTOR_FOR_BOB.CPP

        // delete working arrays
        delete [] m_OriginalImage;
        delete [] m_WorkingImage;
*****

***** LDV\Segmentor.cpp

BOOL CSegmentor::CreateFromImageFile(LPCTSTR FileName)
{
***** SEGMENTATION_CODE_FROM_JEFF_WELTY\SEGMENTOR_FOR_BOB.CPP

BOOL CSegmentor::CreateFromByteArray(unsigned char **image, int rows, int cols, int PeakFilterSize, int LowFilterSize)
{
*****

***** LDV\Segmentor.cpp

        // read image
        m_OriginalImage.ReadImage(FileName);

        if (m_OriginalImage.IsValid()) {
                // set variables
                CSize sz = m_OriginalImage.GetDimensions();
                m_Rows = sz.cy;
                m_Columns = sz.cx;

                // create working arrays
                m_WorkingImage = new BYTE[m_Rows * m_Columns];
                m_Labels = new int[m_Rows * m_Columns];
                m_SortedPixels = new int[m_Rows * m_Columns];
                m_Distances = new BYTE[m_Rows * m_Columns];
                m_Q.Create((m_Rows * m_Columns) / 4);

                // compute intensity levels and create input image
                int intensity;
                COLORREF pixcolor;
                BYTE alpha;
                for (int i = 0; i < m_Rows; i ++) {
                        for (int j = 0; j < m_Columns; j ++) {
                                pixcolor = m_OriginalImage.GetImagePixel(j, i, &alpha);
                                intensity = (int) ((double) GetRValue(pixcolor) * 0.2126 + (double) GetGValue(pixcolor) * 0.715
 + (double) GetBValue(pixcolor) * 0.0722);

                                // invert
                                intensity = 255 - intensity;

                                m_WorkingImage[i * m_Columns + j] = (BYTE) intensity;
                                m_Labels[i * m_Columns + j] = INIT;
                        }
                }

                m_Valid = TRUE;

                // filter to enhance peaks
                MinMaxFilter(5, FALSE, ROUND_SE);

                // filter to enhance lows
                MinMaxFilter(11, TRUE, ROUND_SE);

                // set up working arrays
                for (i = 0; i < m_Rows; i ++) {
                        for (int j = 0; j < m_Columns; j ++) {
                                if (m_WorkingImage[i * m_Columns + j] >= 252)                           // set to 252 to drop o
t points close to ground...low height in original image
                                        m_Labels[i * m_Columns + j] = IGNOREPT;
                                else
                                        m_Labels[i * m_Columns + j] = INIT;
                                m_SortedPixels[i * m_Columns + j] = INIT;
                                m_Distances[i * m_Columns + j] = 0;
                        }
                }

//              // save temp image
                SaveWorkingImage("original_working.bmp");
                
                // filter the working image to reduce oversegmentation
                // histogram equalization
//              HistogramEqualize();
//              SaveWorkingImage("hist_eq_working.bmp");

//              MedianFilter(7);
//              Smooth3by3();
//              SaveWorkingImage("smooth_working.bmp");

//              Slice(16);
        }
***** SEGMENTATION_CODE_FROM_JEFF_WELTY\SEGMENTOR_FOR_BOB.CPP

        // set variables
        m_Rows = rows ;
        m_Columns = cols ;

        // create working arrays
        m_WorkingImage = new BYTE[m_Rows * m_Columns];
        m_OriginalImage = new BYTE[m_Rows * m_Columns];
        m_Labels = new int[m_Rows * m_Columns];
        m_SortedPixels = new int[m_Rows * m_Columns];
        m_Distances = new BYTE[m_Rows * m_Columns];
        m_Q.Create((m_Rows * m_Columns) / 2);

        // compute intensity levels and create input image
        int intensity;

        for (int i = 0; i < m_Rows; i ++) {
                for (int j = 0; j < m_Columns; j ++) {
                        intensity = (int) image[i][j] ;
                        m_OriginalImage[i * m_Columns + j] = (BYTE) intensity;

                        // invert
                        intensity = 255 - intensity;

                        m_WorkingImage[i * m_Columns + j] = (BYTE) intensity;
                        m_Labels[i * m_Columns + j] = INIT;
                }
        }
*****

***** LDV\Segmentor.cpp

        return(m_Valid);
}

void CSegmentor::CountAndSortPixels()
{
        // initialize
        for (int i = 0; i < MAX_LEVELS; i ++) {
                m_Level_CD[i] = 0;
                m_Level_Count[i] = 0;
        }
***** SEGMENTATION_CODE_FROM_JEFF_WELTY\SEGMENTOR_FOR_BOB.CPP

        m_Valid = TRUE;

        /* filter size of 5 for enhancing lows works better */
        // filter to enhance peaks
        if(PeakFilterSize > 0) {
            MinMaxFilter(PeakFilterSize, FALSE, ROUND_SE);
        }
*****

***** LDV\Segmentor.cpp

        // count pixels
        for (i = 0; i < m_Rows * m_Columns; i ++)
                m_Level_Count[m_WorkingImage[i]] ++;

        // build cumulative distribution...actually starting index for a given level into m_SortedPixels
        for (i = 1; i < MAX_LEVELS; i ++)
                m_Level_CD[i] = m_Level_CD[i - 1] + m_Level_Count[i - 1];

        // reset counts...makes it easier to do sort
        for (i = 0; i < MAX_LEVELS; i ++)
                m_Level_Count[i] = 0;

        // sort pixels
        for (i = 0; i < m_Rows * m_Columns; i ++) {
                m_SortedPixels[m_Level_CD[m_WorkingImage[i]] + m_Level_Count[m_WorkingImage[i]]] = i;
                m_Level_Count[m_WorkingImage[i]] ++;
        }
}
***** SEGMENTATION_CODE_FROM_JEFF_WELTY\SEGMENTOR_FOR_BOB.CPP

        // filter to enhance lows
        if(LowFilterSize > 0) {
            MinMaxFilter(LowFilterSize, TRUE, ROUND_SE);
        }

        // set up working arrays
        for (int i = 0; i < m_Rows; i ++) {
                for (int j = 0; j < m_Columns; j ++) {
                        if (m_WorkingImage[i * m_Columns + j] >= 252) {                         // set to 252 to drop out point
 close to ground...low height in original image
                                m_Labels[i * m_Columns + j] = IGNOREPT;
/*                              fprintf(stderr, "cell %d, %d with height %d ignored\n", i, j, (int)m_WorkingImage[i*m_Columns+j
) ;  */
                        } else
                                m_Labels[i * m_Columns + j] = INIT;

                        m_SortedPixels[i * m_Columns + j] = INIT;
                        m_Distances[i * m_Columns + j] = 0;
                }
        }

        return(m_Valid);
}
*****

***** LDV\Segmentor.cpp

void CSegmentor::Flood()
***** SEGMENTATION_CODE_FROM_JEFF_WELTY\SEGMENTOR_FOR_BOB.CPP

void CSegmentor::CountAndSortPixels()
{
        // initialize
        for (int i = 0; i < MAX_LEVELS; i ++) {
                m_Level_CD[i] = 0;
                m_Level_Count[i] = 0;
        }

        // count pixels
        for (int i = 0; i < m_Rows * m_Columns; i ++)
                m_Level_Count[m_WorkingImage[i]] ++;

        // build cumulative distribution...actually starting index for a given level into m_SortedPixels
        for (int i = 1; i < MAX_LEVELS; i ++)
                m_Level_CD[i] = m_Level_CD[i - 1] + m_Level_Count[i - 1];

        // reset counts...makes it easier to do sort
        for (int i = 0; i < MAX_LEVELS; i ++)
                m_Level_Count[i] = 0;

        // sort pixels
        for (int i = 0; i < m_Rows * m_Columns; i ++) {
                m_SortedPixels[m_Level_CD[m_WorkingImage[i]] + m_Level_Count[m_WorkingImage[i]]] = i;
                m_Level_Count[m_WorkingImage[i]] ++;
        }
}

void CSegmentor::Flood()
*****

***** LDV\Segmentor.cpp

void CSegmentor::SaveOutputImage(LPCTSTR FileName, int ColorOption)
{
        // ColorOption == 0 random colors for each segment...black lines for boundaries...red for ignored areas...yellow for hi
***** SEGMENTATION_CODE_FROM_JEFF_WELTY\SEGMENTOR_FOR_BOB.CPP

void CSegmentor::SaveOutputImageToArray(unsigned long **image, int ColorOption)
{
        FILE *fp = fopen("basins.dat", "w") ;

        // ColorOption == 0 random colors for each segment...black lines for boundaries...red for ignored areas...yellow for hi
*****

***** LDV\Segmentor.cpp
        // ColorOption == 1 only boundaries in white with all other areas black
        CImage oi;
        CSize sz = m_OriginalImage.GetDimensions();
        oi.CreateImage(sz, 24);

***** SEGMENTATION_CODE_FROM_JEFF_WELTY\SEGMENTOR_FOR_BOB.CPP
        // ColorOption == 1 only boundaries in white with all other areas black

*****

***** LDV\Segmentor.cpp
        // generate random colors for basins
        int r, g, b;
        COLORREF* bcolors = new COLORREF[m_NumberOfBasins];
        srand(31521);
        for (int color = 0; color < m_NumberOfBasins; color ++) {
                r = (int) (((double) rand() / (double) RAND_MAX)* 128.0) + 63;
                g = (int) (((double) rand() / (double) RAND_MAX)* 128.0) + 63;
                b = (int) (((double) rand() / (double) RAND_MAX)* 128.0) + 63;
                bcolors[color] = RGB(r, g, b);
        }

        // step through labels and build image
***** SEGMENTATION_CODE_FROM_JEFF_WELTY\SEGMENTOR_FOR_BOB.CPP
        // generate random colors for basins

#define RED 0x01
#define GREEN 0x02
#define BLUE 0x04
        // step through labels and build image
*****

***** LDV\Segmentor.cpp
                for (int j = 0; j < m_Columns; j ++) {
                        if (m_Labels[i * m_Columns + j] == MASK) {
***** SEGMENTATION_CODE_FROM_JEFF_WELTY\SEGMENTOR_FOR_BOB.CPP
                for (int j = 0; j < m_Columns; j ++) {
                        fprintf(fp, "%d %d %d\n", i, j, m_Labels[i * m_Columns + j]) ;
                        if (m_Labels[i * m_Columns + j] == MASK) {
*****

***** LDV\Segmentor.cpp
                                if (ColorOption == 0)
                                        oi.SetImagePixel(j, i, RGB(0, 0, 0));
                                else if (ColorOption == 1)
                                        oi.SetImagePixel(j, i, RGB(0, 0, 0));
                        }
***** SEGMENTATION_CODE_FROM_JEFF_WELTY\SEGMENTOR_FOR_BOB.CPP
                                if (ColorOption == 0)
                                        image[i][j] = 0 ;
                                else if (ColorOption == 1)
                                        image[i][j] = 0 ;
                        }
*****

***** LDV\Segmentor.cpp
                                if (ColorOption == 0)
                                        oi.SetImagePixel(j, i, RGB(0, 0, 0));
                                else if (ColorOption == 1)
                                        oi.SetImagePixel(j, i, RGB(255, 255, 255));
                        }
***** SEGMENTATION_CODE_FROM_JEFF_WELTY\SEGMENTOR_FOR_BOB.CPP
                                if (ColorOption == 0)
                                        image[i][j] = 0 ;
                                else if (ColorOption == 1)
                                        image[i][j] = RED | GREEN | BLUE ;
                        }
*****

***** LDV\Segmentor.cpp
                        else if (m_Labels[i * m_Columns + j] == IGNOREPT) {
                                if (ColorOption == 0)
                                        oi.SetImagePixel(j, i, RGB(255, 0, 0));
                                else if (ColorOption == 1)
                                        oi.SetImagePixel(j, i, RGB(0, 0, 0));
                        }
***** SEGMENTATION_CODE_FROM_JEFF_WELTY\SEGMENTOR_FOR_BOB.CPP
                        else if (m_Labels[i * m_Columns + j] == IGNOREPT) {
/*                              fprintf(stderr, "post seg cell %d, %d with height %d ignored\n", i, j, (int)m_WorkingImage[i*m_
olumns+j]) ;  */
                                if (ColorOption == 0)
                                        image[i][j] = RED ;
                                else if (ColorOption == 1)
                                        image[i][j] = 0 ;
                        }
*****

***** LDV\Segmentor.cpp
                                if (ColorOption == 0)
                                        oi.SetImagePixel(j, i, RGB(0, 0, 255));
                                else if (ColorOption == 1)
                                        oi.SetImagePixel(j, i, RGB(0, 0, 0));
                        }
***** SEGMENTATION_CODE_FROM_JEFF_WELTY\SEGMENTOR_FOR_BOB.CPP
                                if (ColorOption == 0)
                                        image[i][j] = BLUE ;
                                else if (ColorOption == 1)
                                        image[i][j] = 0 ;
                        }
*****

***** LDV\Segmentor.cpp
                                if (ColorOption == 0)
                                        oi.SetImagePixel(j, i, RGB(255, 255, 0));
                                else if (ColorOption == 1)
                                        oi.SetImagePixel(j, i, RGB(0, 0, 0));
                        }
***** SEGMENTATION_CODE_FROM_JEFF_WELTY\SEGMENTOR_FOR_BOB.CPP
                                if (ColorOption == 0)
                                        image[i][j] = RED | GREEN ;
                                else if (ColorOption == 1)
                                        image[i][j] = 0 ;
                        }
*****

***** LDV\Segmentor.cpp
                        else {
                                if (ColorOption == 0)
////                            oi.SetImagePixel(j, i, RGB(0, 255, 0));
                                        oi.SetImagePixel(j, i, bcolors[m_Labels[i * m_Columns + j]]);
                                else if (ColorOption == 1)
                                        oi.SetImagePixel(j, i, RGB(0, 0, 0));
                        }
***** SEGMENTATION_CODE_FROM_JEFF_WELTY\SEGMENTOR_FOR_BOB.CPP
                        else {
                                if (ColorOption == 0) {
                                        srand(m_Labels[i * m_Columns + j]) ;
                                        image[i][j] = m_Labels[i * m_Columns + j] + 8 ;
                                } else if (ColorOption == 1)
                                        image[i][j] = 0 ;
                        }
*****

***** LDV\Segmentor.cpp

        delete [] bcolors;

        // save image file
        CFile file;
        if (file.Open(FileName, CFile::modeCreate | CFile::modeWrite | CFile::typeBinary)) {
                oi.Write(&file);
                file.Close();
        }
***** SEGMENTATION_CODE_FROM_JEFF_WELTY\SEGMENTOR_FOR_BOB.CPP

        fclose(fp) ;
}


void CSegmentor::HistogramEqualize()
{
        int Level_CD[MAX_LEVELS];
        int Level_Count[MAX_LEVELS];

        // initialize
        for (int i = 0; i < MAX_LEVELS; i ++) {
                Level_CD[i] = 0;
                Level_Count[i] = 0;
        }
*****

***** LDV\Segmentor.cpp

        // write world file
        if (m_OriginalImage.m_GeoInfoIsValid) {
                CString wf = _T(FileName);
                wf = wf + "w";
                m_OriginalImage.m_GeoreferenceInfo.WriteValues(wf);
        }
}
***** SEGMENTATION_CODE_FROM_JEFF_WELTY\SEGMENTOR_FOR_BOB.CPP

        // count pixels
        for (int i = 0; i < m_Rows * m_Columns; i ++)
                Level_Count[m_WorkingImage[i]] ++;

        // build cumulative distribution
        for (int i = 1; i < MAX_LEVELS; i ++)
                Level_CD[i] = Level_CD[i - 1] + Level_Count[i - 1];

        // normalize histogram
        for (int i = 1; i < MAX_LEVELS; i ++)
                Level_CD[i] = (int) ((double) Level_CD[i] * (double) MAX_LEVELS / (double) (m_Rows * m_Columns));

        // use histogram as look up table for new gray level
        for (int i = 0; i < m_Rows * m_Columns; i ++)
                m_WorkingImage[i] = Level_CD[m_WorkingImage[i]];
}
*****

***** LDV\Segmentor.cpp

void CSegmentor::SaveWorkingImage(LPCTSTR FileName)
{
        CImage oi;
        CSize sz = m_OriginalImage.GetDimensions();
        oi.CreateImage(sz, 24);

        // step through labels and build image
        for (int i = 0; i < m_Rows; i ++) {
                for (int j = 0; j < m_Columns; j ++) {
                        oi.SetImagePixel(j, i, RGB(m_WorkingImage[i * m_Columns + j], m_WorkingImage[i * m_Columns + j], m_Work
ngImage[i * m_Columns + j]));
                }
        }

        // save image file
        CFile file;
        if (file.Open(FileName, CFile::modeCreate | CFile::modeWrite | CFile::typeBinary)) {
                oi.Write(&file);
                file.Close();
        }
***** SEGMENTATION_CODE_FROM_JEFF_WELTY\SEGMENTOR_FOR_BOB.CPP

void CSegmentor::Smooth3by3()
{
        int i, j, k;
        double weights[9] = 
                                        {2.5,  2.5,  2.5, 
                                         2.5,  1.0,  2.5, 
                                         2.5,  2.5,  2.5};
        double weightsum = 0.0;
        double valuesum;
        
        for (k = 0; k < 9; k ++)
                weightsum += weights[k];

        BYTE* TempImage = (BYTE*) new BYTE[m_Rows * m_Columns];
        if (TempImage) {
                // copy original image to temp
                for (i = 0; i < m_Rows * m_Columns; i ++)
                        TempImage[i] = m_WorkingImage[i];

                for (i = 1; i < m_Rows - 1; i ++) {
                        for (j = 1; j < m_Columns - 1; j ++) {
                                valuesum = weights[0] * (double) TempImage[(i + 1) * m_Columns + (j - 1)];
                                valuesum += weights[1] * (double) TempImage[(i + 1) * m_Columns + (j - 0)];
                                valuesum += weights[2] * (double) TempImage[(i + 1) * m_Columns + (j + 1)];
                                valuesum += weights[3] * (double) TempImage[(i - 0) * m_Columns + (j - 1)];
                                valuesum += weights[4] * (double) TempImage[(i - 0) * m_Columns + (j - 0)];
                                valuesum += weights[5] * (double) TempImage[(i - 0) * m_Columns + (j + 1)];
                                valuesum += weights[6] * (double) TempImage[(i - 1) * m_Columns + (j - 1)];
                                valuesum += weights[7] * (double) TempImage[(i - 1) * m_Columns + (j - 0)];
                                valuesum += weights[8] * (double) TempImage[(i - 1) * m_Columns + (j + 1)];

                                m_WorkingImage[i * m_Columns + j] = (BYTE) (valuesum / weightsum);
                        }
                }
                delete [] TempImage;
        }
*****

***** LDV\Segmentor.cpp

void CSegmentor::HistogramEqualize()
{
        int Level_CD[MAX_LEVELS];
        int Level_Count[MAX_LEVELS];

        // initialize
        for (int i = 0; i < MAX_LEVELS; i ++) {
                Level_CD[i] = 0;
                Level_Count[i] = 0;
        }

        // count pixels
        for (i = 0; i < m_Rows * m_Columns; i ++)
                Level_Count[m_WorkingImage[i]] ++;

        // build cumulative distribution
        for (i = 1; i < MAX_LEVELS; i ++)
                Level_CD[i] = Level_CD[i - 1] + Level_Count[i - 1];

        // normalize histogram
        for (i = 1; i < MAX_LEVELS; i ++)
                Level_CD[i] = (int) ((double) Level_CD[i] * (double) MAX_LEVELS / (double) (m_Rows * m_Columns));

        // use histogram as look up table for new gray level
        for (i = 0; i < m_Rows * m_Columns; i ++)
                m_WorkingImage[i] = Level_CD[m_WorkingImage[i]];
}
***** SEGMENTATION_CODE_FROM_JEFF_WELTY\SEGMENTOR_FOR_BOB.CPP

void CSegmentor::Slice(int levels)
{
        int intensity;
        int val = 256 / levels;

        for (int i = 0; i < m_Rows; i ++) {
                for (int j = 0; j < m_Columns; j ++) {
                        intensity = m_WorkingImage[i * m_Columns + j];

                        // slice into levels
                        intensity = intensity / val;
                        intensity = intensity * val;

                        m_WorkingImage[i * m_Columns + j] = (BYTE) intensity;
                }
        }
}
*****

***** LDV\Segmentor.cpp

void CSegmentor::Smooth3by3()
{
        int i, j, k;
        double weights[9] = 
                                        {2.5,  2.5,  2.5, 
                                         2.5,  1.0,  2.5, 
                                         2.5,  2.5,  2.5};
        double weightsum = 0.0;
        double valuesum;
        
        for (k = 0; k < 9; k ++)
                weightsum += weights[k];

        BYTE* TempImage = (BYTE*) new BYTE[m_Rows * m_Columns];
        if (TempImage) {
                // copy original image to temp
                for (i = 0; i < m_Rows * m_Columns; i ++)
                        TempImage[i] = m_WorkingImage[i];

                for (i = 1; i < m_Rows - 1; i ++) {
                        for (j = 1; j < m_Columns - 1; j ++) {
                                valuesum = weights[0] * (double) TempImage[(i + 1) * m_Columns + (j - 1)];
                                valuesum += weights[1] * (double) TempImage[(i + 1) * m_Columns + (j - 0)];
                                valuesum += weights[2] * (double) TempImage[(i + 1) * m_Columns + (j + 1)];
                                valuesum += weights[3] * (double) TempImage[(i - 0) * m_Columns + (j - 1)];
                                valuesum += weights[4] * (double) TempImage[(i - 0) * m_Columns + (j - 0)];
                                valuesum += weights[5] * (double) TempImage[(i - 0) * m_Columns + (j + 1)];
                                valuesum += weights[6] * (double) TempImage[(i - 1) * m_Columns + (j - 1)];
                                valuesum += weights[7] * (double) TempImage[(i - 1) * m_Columns + (j - 0)];
                                valuesum += weights[8] * (double) TempImage[(i - 1) * m_Columns + (j + 1)];

                                m_WorkingImage[i * m_Columns + j] = (BYTE) (valuesum / weightsum);
                        }
***** SEGMENTATION_CODE_FROM_JEFF_WELTY\SEGMENTOR_FOR_BOB.CPP

void CSegmentor::FixBoundaries()
{
        // fix the watershed boundaries
        int q[8];               // max of 8 neighbors
        int connects = CONNECTION;              // either 4 or 8...4 looks left, right, up, and down...8 adds the diagonals
        int n;

        for (int p = 0; p < m_Rows * m_Columns; p ++) {
                if (m_Labels[p] >= 0) {
                        // look at neighboring pixels
                        q[0] = NeighborP(p, 0,  -1, TRUE);              // left 1 column
                        q[1] = NeighborP(p, 0,   1, TRUE);              // right 1 column
                        q[2] = NeighborP(p, -1,  0, TRUE);              // down 1 row
                        q[3] = NeighborP(p,  1,  0, TRUE);              // up 1 row
                        if (connects == 8) {
                                q[4] = NeighborP(p, -1, -1, TRUE);              // left 1 column, down 1 row
                                q[5] = NeighborP(p,  1, -1, TRUE);              // left 1 column, up 1 row
                                q[6] = NeighborP(p, -1,  1, TRUE);              // right 1 column, down 1 row
                                q[7] = NeighborP(p,  1,  1, TRUE);              // right 1 column, up 1 row
                        }
                        for (n = 0; n < connects; n ++) {
                                if (q[n] >= 0) {
                                        if (m_Labels[q[n]] >= 0 && m_Labels[q[n]] < m_Labels[p]) {
                                                m_Labels[p] = WSHED;
                                                break;
                                        }
                                        if (m_Labels[q[n]] == IGNOREPT) {
                                                m_Labels[p] = WSHED;
                                                break;
                                        }
                                }
                        }
*****

***** LDV\Segmentor.cpp
                }
                delete [] TempImage;
        }
}
***** SEGMENTATION_CODE_FROM_JEFF_WELTY\SEGMENTOR_FOR_BOB.CPP
                }
        }

}
*****

***** LDV\Segmentor.cpp

void CSegmentor::Slice(int levels)
{
        int intensity;
        int val = 256 / levels;

        for (int i = 0; i < m_Rows; i ++) {
                for (int j = 0; j < m_Columns; j ++) {
                        intensity = m_WorkingImage[i * m_Columns + j];

                        // slice into levels
                        intensity = intensity / val;
                        intensity = intensity * val;

                        m_WorkingImage[i * m_Columns + j] = (BYTE) intensity;
                }
        }
}

void CSegmentor::FixBoundaries()
{
        // fix the watershed boundaries
        int q[8];               // max of 8 neighbors
***** SEGMENTATION_CODE_FROM_JEFF_WELTY\SEGMENTOR_FOR_BOB.CPP

void CSegmentor::EraseBoundaries()
{
        // remove the watershed boundaries
        int q[8];               // max of 8 neighbors
*****

***** LDV\Segmentor.cpp
        for (int p = 0; p < m_Rows * m_Columns; p ++) {
                if (m_Labels[p] >= 0) {
                        // look at neighboring pixels
                        q[0] = NeighborP(p, 0,  -1, TRUE);              // left 1 column
                        q[1] = NeighborP(p, 0,   1, TRUE);              // right 1 column
                        q[2] = NeighborP(p, -1,  0, TRUE);              // down 1 row
                        q[3] = NeighborP(p,  1,  0, TRUE);              // up 1 row
                        if (connects == 8) {
                                q[4] = NeighborP(p, -1, -1, TRUE);              // left 1 column, down 1 row
                                q[5] = NeighborP(p,  1, -1, TRUE);              // left 1 column, up 1 row
                                q[6] = NeighborP(p, -1,  1, TRUE);              // right 1 column, down 1 row
                                q[7] = NeighborP(p,  1,  1, TRUE);              // right 1 column, up 1 row
                        }
***** SEGMENTATION_CODE_FROM_JEFF_WELTY\SEGMENTOR_FOR_BOB.CPP
        for (int p = 0; p < m_Rows * m_Columns; p ++) {
                if (m_Labels[p] == WSHED) {
                        // look at neighboring pixels
                        q[0] = NeighborP(p, 0,  -1, FALSE);             // left 1 column
                        q[1] = NeighborP(p, 0,   1, FALSE);             // right 1 column
                        q[2] = NeighborP(p, -1,  0, FALSE);             // down 1 row
                        q[3] = NeighborP(p,  1,  0, FALSE);             // up 1 row
                        if (connects == 8) {
                                q[4] = NeighborP(p, -1, -1, FALSE);             // left 1 column, down 1 row
                                q[5] = NeighborP(p,  1, -1, FALSE);             // left 1 column, up 1 row
                                q[6] = NeighborP(p, -1,  1, FALSE);             // right 1 column, down 1 row
                                q[7] = NeighborP(p,  1,  1, FALSE);             // right 1 column, up 1 row
                        }
*****

***** LDV\Segmentor.cpp
                                if (q[n] >= 0) {
                                        if (m_Labels[q[n]] >= 0 && m_Labels[q[n]] < m_Labels[p]) {
                                                m_Labels[p] = WSHED;
                                                break;
***** SEGMENTATION_CODE_FROM_JEFF_WELTY\SEGMENTOR_FOR_BOB.CPP
                                if (q[n] >= 0) {
                                        if (m_Labels[q[n]] >= 0) {
                                                m_Labels[p] = m_Labels[q[n]];
                                                break;
*****

***** LDV\Segmentor.cpp
                                        }
                                        if (m_Labels[q[n]] == IGNOREPT) {
                                                m_Labels[p] = WSHED;
                                                break;
                                        }
                                }
***** SEGMENTATION_CODE_FROM_JEFF_WELTY\SEGMENTOR_FOR_BOB.CPP
                                        }
                                }
*****

***** LDV\Segmentor.cpp
        }

}
***** SEGMENTATION_CODE_FROM_JEFF_WELTY\SEGMENTOR_FOR_BOB.CPP
        }
}
*****

***** LDV\Segmentor.cpp

void CSegmentor::EraseBoundaries()
{
        // remove the watershed boundaries
        int q[8];               // max of 8 neighbors
        int connects = CONNECTION;              // either 4 or 8...4 looks left, right, up, and down...8 adds the diagonals
        int n;

        for (int p = 0; p < m_Rows * m_Columns; p ++) {
                if (m_Labels[p] == WSHED) {
                        // look at neighboring pixels
                        q[0] = NeighborP(p, 0,  -1, FALSE);             // left 1 column
                        q[1] = NeighborP(p, 0,   1, FALSE);             // right 1 column
                        q[2] = NeighborP(p, -1,  0, FALSE);             // down 1 row
                        q[3] = NeighborP(p,  1,  0, FALSE);             // up 1 row
                        if (connects == 8) {
                                q[4] = NeighborP(p, -1, -1, FALSE);             // left 1 column, down 1 row
                                q[5] = NeighborP(p,  1, -1, FALSE);             // left 1 column, up 1 row
                                q[6] = NeighborP(p, -1,  1, FALSE);             // right 1 column, down 1 row
                                q[7] = NeighborP(p,  1,  1, FALSE);             // right 1 column, up 1 row
                        }
                        for (n = 0; n < connects; n ++) {
                                if (q[n] >= 0) {
                                        if (m_Labels[q[n]] >= 0) {
                                                m_Labels[p] = m_Labels[q[n]];
                                                break;
                                        }
                                }
                        }
                }
***** SEGMENTATION_CODE_FROM_JEFF_WELTY\SEGMENTOR_FOR_BOB.CPP

void CSegmentor::FixEdges()
{
        // fix the edges of the image
        for (int i = 0; i < m_Rows; i ++) {
                if (i == 0 || i == m_Rows - 1) {
                        for (int j = 0; j < m_Columns; j ++)
                                m_Labels[i * m_Columns + j] = WSHED;
                }
                else {
                        m_Labels[i * m_Columns] = WSHED;
                        m_Labels[i * m_Columns + (m_Columns - 1)] = WSHED;
                }
*****

***** LDV\Segmentor.cpp

void CSegmentor::FixEdges()
{
        // fix the edges of the image
        for (int i = 0; i < m_Rows; i ++) {
                if (i == 0 || i == m_Rows - 1) {
                        for (int j = 0; j < m_Columns; j ++)
                                m_Labels[i * m_Columns + j] = WSHED;
                }
                else {
                        m_Labels[i * m_Columns] = WSHED;
                        m_Labels[i * m_Columns + (m_Columns - 1)] = WSHED;
                }
***** SEGMENTATION_CODE_FROM_JEFF_WELTY\SEGMENTOR_FOR_BOB.CPP

void CSegmentor::LabelPeaks()
{
        if (!m_NumberOfBasins)
                return;

        for (int i = 0; i < m_NumberOfBasins; i ++) {
                if (m_Area[i]) {
                        // mark highest pixel
                        m_Labels[m_MaxPix[i]] = HIGHPT;
                }
*****

***** LDV\Segmentor.cpp

void CSegmentor::LabelPeaks()
{
        if (!m_NumberOfBasins)
                return;

        for (int i = 0; i < m_NumberOfBasins; i ++) {
                if (m_Area[i]) {
                        // mark highest pixel
                        m_Labels[m_MaxPix[i]] = HIGHPT;
                }
        }
}
***** SEGMENTATION_CODE_FROM_JEFF_WELTY\SEGMENTOR_FOR_BOB.CPP

int CSegmentor::GetNumberOfBasins()
{
        return(m_NumberOfBasins);
}
*****

***** LDV\Segmentor.cpp

int CSegmentor::GetNumberOfBasins()
{
        return(m_NumberOfBasins);
}

BOOL CSegmentor::GetBasinInfo(int Basin, int &Area, int &MaxHt, int &HighRow, int &HighCol, BOOL &Deleted)
***** SEGMENTATION_CODE_FROM_JEFF_WELTY\SEGMENTOR_FOR_BOB.CPP

BOOL CSegmentor::GetBasinInfo(int Basin, int &Area, int &MaxHt, int &HighRow, int &HighCol, BOOL &Deleted)
*****

***** LDV\Segmentor.cpp

int CSegmentor::GetBasinFromPoint(double X, double Y, BOOL IgnoreBoundaries)
{
        int retcode = -1;
        int Row, Col;

        if (m_NumberOfBasins && m_OriginalImage.m_GeoInfoIsValid) {
                m_OriginalImage.MapGeoToImage(X, Y, Col, Row, TRUE);

                retcode = GetBasinFromPoint(Row, Col, IgnoreBoundaries);
        }
        return(retcode);
}

***** SEGMENTATION_CODE_FROM_JEFF_WELTY\SEGMENTOR_FOR_BOB.CPP


*****

***** LDV\Segmentor.cpp
        int p;
        BOOL Cull = TRUE;

***** SEGMENTATION_CODE_FROM_JEFF_WELTY\SEGMENTOR_FOR_BOB.CPP
        int p;
        BOOL Cull = FALSE;

*****

***** LDV\Segmentor.cpp

                for (i = 0; i < m_Rows; i ++) {
                        if (i == 0 || i == m_Rows - 1) {
***** SEGMENTATION_CODE_FROM_JEFF_WELTY\SEGMENTOR_FOR_BOB.CPP

                for (int i = 0; i < m_Rows; i ++) {
                        if (i == 0 || i == m_Rows - 1) {
*****

***** LDV\Segmentor.cpp
                        int index;
                        for (i = 0; i < m_Rows; i ++) {
                                for (int j = 0; j < m_Columns; j ++) {
***** SEGMENTATION_CODE_FROM_JEFF_WELTY\SEGMENTOR_FOR_BOB.CPP
                        int index;
                        for (int i = 0; i < m_Rows; i ++) {
                                for (int j = 0; j < m_Columns; j ++) {
*****

***** LDV\Segmentor.cpp
        // accumulate stats
        for (i = 0; i < m_Rows; i ++) {
                for (int j = 0; j < m_Columns; j ++) {
***** SEGMENTATION_CODE_FROM_JEFF_WELTY\SEGMENTOR_FOR_BOB.CPP
        // accumulate stats
        for (int i = 0; i < m_Rows; i ++) {
                for (int j = 0; j < m_Columns; j ++) {
*****

***** LDV\Segmentor.cpp
                                index = i * m_Columns + j;
                                ht = (255 - m_WorkingImage[index]);
                                m_Area[m_Labels[index]] ++;
***** SEGMENTATION_CODE_FROM_JEFF_WELTY\SEGMENTOR_FOR_BOB.CPP
                                index = i * m_Columns + j;
                                ht = m_OriginalImage[index];
                                m_Area[m_Labels[index]] ++;
*****

***** LDV\Segmentor.cpp
        int index;
        for (i = 0; i < m_Rows; i ++) {
                for (int j = 0; j < m_Columns; j ++) {
***** SEGMENTATION_CODE_FROM_JEFF_WELTY\SEGMENTOR_FOR_BOB.CPP
        int index;
        for (int i = 0; i < m_Rows; i ++) {
                for (int j = 0; j < m_Columns; j ++) {
*****

***** LDV\Segmentor.cpp
        int cnt = 0;
        for (i = 0; i < m_NumberOfBasins; i ++) {
                if (lutable[i] == 1) {
***** SEGMENTATION_CODE_FROM_JEFF_WELTY\SEGMENTOR_FOR_BOB.CPP
        int cnt = 0;
        for (int i = 0; i < m_NumberOfBasins; i ++) {
                if (lutable[i] == 1) {
*****

***** LDV\Segmentor.cpp
        // relabel image
        for (i = 0; i < m_Rows; i ++) {
                for (int j = 0; j < m_Columns; j ++) {
***** SEGMENTATION_CODE_FROM_JEFF_WELTY\SEGMENTOR_FOR_BOB.CPP
        // relabel image
        for (int i = 0; i < m_Rows; i ++) {
                for (int j = 0; j < m_Columns; j ++) {
*****

***** LDV\Segmentor.cpp
                                                                // check the mask
                                                                if (l == 0 || k == 0) {         // always test the center "+"
***** SEGMENTATION_CODE_FROM_JEFF_WELTY\SEGMENTOR_FOR_BOB.CPP
                                                                // check the mask
                                                                n = -1 ;
                                                                if (l == 0 || k == 0) {         // always test the center "+"
*****

***** LDV\Segmentor.cpp
                                                                        n = Neighbor(i, j, k, l);
                                                                        if (n >= 0) {
                                                                                if (!DoMaximum && TempImage[n] < value)
                                                                                        value = TempImage[n];
                                                                                else if (DoMaximum && TempImage[n] > value)
                                                                                        value = TempImage[n];
                                                                        }
                                                                }
                                                                else {
                                                                        if (Mask[(abs(k) - 1) * HalfFilterSize + (abs(l) - 1)])
{
                                                                                n = Neighbor(i, j, k, l);
                                                                                if (n >= 0) {
                                                                                        if (!DoMaximum && TempImage[n] < value)

                                                                                                value = TempImage[n];
                                                                                        else if (DoMaximum && TempImage[n] > va
ue)
                                                                                                value = TempImage[n];
                                                                                }
                                                                        }
                                                                }
***** SEGMENTATION_CODE_FROM_JEFF_WELTY\SEGMENTOR_FOR_BOB.CPP
                                                                        n = Neighbor(i, j, k, l);
                                                                }
                                                                else if (Mask[(abs(k) - 1) * HalfFilterSize + (abs(l) - 1)]) {
                                                                        n = Neighbor(i, j, k, l);
                                                                }

                                                                if (n >= 0) {
                                                                        if (!DoMaximum && (TempImage[n] < value))
                                                                                value = TempImage[n];
                                                                        else if (DoMaximum && (TempImage[n] > value))
                                                                                value = TempImage[n];
                                                                }
*****

