VCL graphics code snippet to perform a threshold filter operation on a VCL TImage.
There are two types of ThresholdFilter operations: ThresholdFilterColor and ThresholdFilterNoColor. ThresholdFilterColor makes a pixel black if its color is below the threshold. ThresholdFilterNoColor additionally makes a pixel white if it is above or equal to the thresholdvalue. This example image show the difference between the two versions.
The tool ThresholdFilterer demonstrates the use of ThresholdFilter.
#include <cassert> #include <vcl.h> //If a pixel's grey value is below threshold, the pixel is made black //Else it keeps it color //From http://www.richelbilderbeek.nl/CppThresholdFilter.htm void ThresholdFilterColor( const TImage * const imageOriginal, const int threshold, TImage * const imageResult) { assert(imageOriginal!=0 && "imageOriginal must not be NULL"); assert(imageOriginal->Picture !=0 && "Picture of imageOriginal must not be NULL"); assert(imageOriginal->Picture->Bitmap !=0 && "Bitmap of imageOriginal must not be NULL"); assert(imageOriginal->Picture->Bitmap->PixelFormat == pf24bit && "Bitmap of imageOriginal must be 24 bit"); assert(imageResult!=0 && "imageResult must not be NULL"); assert(imageResult->Picture !=0 && "Picture of imageResult must not be NULL"); assert(imageResult->Picture->Bitmap !=0 && "Bitmap of imageResult must not be NULL"); assert(imageResult->Picture->Bitmap->PixelFormat == pf24bit && "Bitmap of imageResult must be 24 bit"); assert(threshold >= 0 && threshold < 256 && "Threshold must be in range [0,255]"); const int width = imageOriginal->Picture->Bitmap->Width; const int height = imageOriginal->Picture->Bitmap->Height; imageResult->Picture->Bitmap->Width = width; imageResult->Picture->Bitmap->Height = height; for (int y=0; y!=height; ++y) { const unsigned char * const lineOriginal = static_cast<unsigned char *>(imageOriginal->Picture->Bitmap->ScanLine[y]); unsigned char * const lineResult = static_cast<unsigned char *>(imageResult->Picture->Bitmap->ScanLine[y]); for (int x=0; x!=width; ++x) { const int grey = (lineOriginal[x*3+2] + lineOriginal[x*3+1] + lineOriginal[x*3+0]) / 3; if (grey < threshold) { lineResult[x*3+2] = 0; //Red lineResult[x*3+1] = 0; //Green lineResult[x*3+0] = 0; //Blue } else { lineResult[x*3+2] = lineOriginal[x*3+2]; //Red lineResult[x*3+1] = lineOriginal[x*3+1]; //Green lineResult[x*3+0] = lineOriginal[x*3+0]; //Blue } } } } #include <cassert> #include <vcl.h> //If a pixel's grey value is below threshold, the pixel is made black //Else it is made white //From http://www.richelbilderbeek.nl/CppThresholdFilter.htm void ThresholdFilterNoColor( const TImage * const imageOriginal, const int threshold, TImage * const imageResult) { assert(imageOriginal!=0 && "imageOriginal must not be NULL"); assert(imageOriginal->Picture !=0 && "Picture of imageOriginal must not be NULL"); assert(imageOriginal->Picture->Bitmap !=0 && "Bitmap of imageOriginal must not be NULL"); assert(imageOriginal->Picture->Bitmap->PixelFormat == pf24bit && "Bitmap of imageOriginal must be 24 bit"); assert(imageResult!=0 && "imageResult must not be NULL"); assert(imageResult->Picture !=0 && "Picture of imageResult must not be NULL"); assert(imageResult->Picture->Bitmap !=0 && "Bitmap of imageResult must not be NULL"); assert(imageResult->Picture->Bitmap->PixelFormat == pf24bit && "Bitmap of imageResult must be 24 bit"); assert(threshold >= 0 && threshold < 256 && "Threshold must be in range [0,255]"); const int width = imageOriginal->Picture->Bitmap->Width; const int height = imageOriginal->Picture->Bitmap->Height; imageResult->Picture->Bitmap->Width = width; imageResult->Picture->Bitmap->Height = height; for (int y=0; y!=height; ++y) { const unsigned char * const lineOriginal = static_cast<unsigned char *>(imageOriginal->Picture->Bitmap->ScanLine[y]); unsigned char * const lineResult = static_cast<unsigned char *>(imageResult->Picture->Bitmap->ScanLine[y]); for (int x=0; x!=width; ++x) { const int grey = (lineOriginal[x*3+2] + lineOriginal[x*3+1] + lineOriginal[x*3+0]) / 3; if (grey < threshold) { lineResult[x*3+2] = 0; //Red lineResult[x*3+1] = 0; //Green lineResult[x*3+0] = 0; //Blue } else { lineResult[x*3+2] = 255; //Red lineResult[x*3+1] = 255; //Green lineResult[x*3+0] = 255; //Blue } } } }