link: No
#define LOW_THRESHOLD_INTENSITY 63
#define HIGH_THRESHOLD_INTENSITY 127
void RunHysteresisThresholding(unsigned char* input, unsigned char* output, int imageWidth, int imageHeight)
{
int low = LOW_THRESHOLD_INTENSITY;
int high = HIGH_THRESHOLD_INTENSITY;
register int i, j;
for(i=0; i<imageHeight; i++)
{
for(j=0; j<imageWidth; j++)
{
if(input[i*imageWidth+j] > high)
{
output[i*imageWidth+j] = 255;
}
else
{
output[i*imageWidth+j] = 0;
}
}
}
int grown;
do {
grown = 0;
for(i=0; i<imageHeight; i++)
{
for(j=0; j<imageWidth; j++)
{
if(input[i*imageWidth+j] > low && CheckNeighbor(output, imageWidth, imageHeight, i, j))
{
output[i*imageWidth+j] = 255;
grown++;
}
}
}
} while(grown>0);
}
int CheckNeighbor(unsigned char* target, int imageWidth, int imageHeight, int i, int j)
{
if(target[i*imageWidth+j]>0)
{
return 0;
}
if(i>0 && target[(i-1)*imageWidth+j]>0)
{
return 1;
}
if(i>0 && j>0 && target[(i-1)*imageWidth+(j-1)]>0)
{
return 1;
}
if(j<imageWidth-1 && target[i*imageWidth+(j+1)]>0)
{
return 1;
}
if(i<imageHeight-1 && j<imageWidth-1 && target[(i+1)*imageWidth+(j+1)]>0)
{
return 1;
}
return 0;
}