How to Implement the Sobel Filter?
How to implement the Sobel filter?¶
Introduction
If you want to find out the edge of the image. We can use the Sobel filter. There are two matrix a horizontal and a vertical matrix, respectively. There are a 3 by 3 matrix matching the image. We do the amount of the pixels of the 3 by 3 matrix. The result is the center pixel value of the 3 by 3 matrix. We will rewrite the result in the image. This is called convolution calculation.
Equipment
Operation System: Microsoft Windows 7 (64 bit)
Development Utility: Microsoft Visual Studio 2010
Usage
// The variable on the horizontal of the 3 by 3 matrix. double dobFilterResultH = 0; // The variable on the vertical of the 3 by 3 matrix. double dobFilterResultV = 0; // The variable of the gray scale result. double dobFilterResult = 0; // This is a horizontal Sobel filter matrix. double adobHS[3][3] = { {-(1), -(2), -(1) }, { (0), (0), (0) }, { (1), (2), (1) } }; // This is a vertical Sobel filter matrix. double adobVS[3][3] = { {-(1), (0), (1) }, {-(2), (0), (2) }, {-(1), (0), (1) } }; // The height of the image. for ( int iY = 1; iY < imageA->DibInfo->bmiHeader.biHeight - 1; iY++ ) { // The width of the image. for ( int iX = 1; iX < imageA->DibInfo->bmiHeader.biWidth - 1; iX++ ) { // Set all variables to zero. dobFilterResultH = 0; dobFilterResultV = 0; dobFilterResult = 0; // This is a convolution calculate about the horizontal Sobel filter. // The height of the 3 by 3 matrix. for ( intFWY = -1; intFWY <= 1; intFWY++ ) { // The width of the 3 by 3 matrix. for ( intFWX = -1; intFWX <= 1; intFWX++ ) { // The index of the image with 3 by 3 matrix. // The bit depth is three. So we need to be multiplied by three. // This is a 24 bits depth format. lIDXA = ( (iX+intFWX) * 3 ) + ( (iY+intFWY) * imageA->DibInfo->bmiHeader.biWidth * 3 ); // Get the pixel of the blue channel. byteRGB_BA = imageA->DibArry[lIDXA+0]; // Get the pixel of the green channel. byteRGB_GA = imageA->DibArry[lIDXA+1]; // Get the pixel of the red channel. byteRGB_RA = imageA->DibArry[lIDXA+2]; // Transform an RGB color space to gray scale. dobYUV_YA = (0.299 * byteRGB_RA + 0.587 * byteRGB_GA + 0.114 * byteRGB_BA); // The variable is the amount of the 3 by 3 matrix. dobFilterResultH += ( adobHS[intFWX+1][intFWY+1] * dobYUV_YA ); } // The closing "The width of 3 by 3 matrix". } // The closing "The height of 3 by 3 matrix". // The absolute value of horizontal variable. dobFilterResultH = abs(dobFilterResultH); // The height of the 3 by 3 matrix. for ( intFWY = -1; intFWY <= 1; intFWY++ ) { // The width of the 3 by 3 matrix. for ( intFWX = -1; intFWX <= 1; intFWX++ ) { // The index of the image with 3 by 3 matrix. // This is 24 bits depth format. lIDXA = ( (iX+intFWX) * 3 ) + ( (iY+intFWY) * imageA->DibInfo->bmiHeader.biWidth * 3 ); // Get the pixel of the blue channel. byteRGB_BA = imageA->DibArry[lIDXA+0]; // Get the pixel of the green channel. byteRGB_GA = imageA->DibArry[lIDXA+1]; // Get the pixel of the red channel. byteRGB_RA = imageA->DibArry[lIDXA+2]; // Transform an RGB color space to gray scale. dobYUV_YA = (0.299 * byteRGB_RA + 0.587 * byteRGB_GA + 0.114 * byteRGB_BA); // The variable is the amount of the 3 by 3 matrix. dobFilterResultV += ( adobVS[intFWX+1][intFWY+1] * dobYUV_YA ); } // The closing "The width of 3 by 3 matrix". } // The closing "The height of 3 by 3 matrix". // The absolute value of vertical variable. dobFilterResultV = abs(dobFilterResultV); // To choose the maximum value, but less than 255. dobFilterResult = min ( max ( dobFilterResultV, dobFilterResultH ), 255 ); // The index of the image. lIDXB = ( iX * 3 ) + ( iY * imageB->DibInfo->bmiHeader.biWidth * 3 ); // Set the pixel of the blue channel. imageB->DibArry[lIDXB+0] = dobFilterResult; // Set the pixel of the green channel. imageB->DibArry[lIDXB+1] = dobFilterResult; // Set the pixel of the red channel. imageB->DibArry[lIDXB+2] = dobFilterResult; } // The closing "The width of the image". } // The closing "The height of the image".
You can download source code and binary code.
https://drive.google.com/file/d/0BzHb_OyLyVZlUVpaTDdPUjhqd1U/view?usp=sharing
Exception
There is a notice, if your bit depth of bitmap file are not 24 bits, you should change your bitmap files to adapt this program, or you could rewrite this source code to fit your bitmap format.
You have to install Microsoft SDK v7.1, because I include windowscodes.lib.
#pragma comment(lib, "windowscodecs.lib")
Reference
[1] Gary Bradski and Adrian Kaehler, “Learning OpenCV: Computer Vision with the OpenCV Library,” O’REILLY, September 2008, ISBN:978-0-596-51613-0
Acknowledge
Thank you (Microsoft Visual Studio 2010, Lenna Sjööblom) very much for this great development utility and beautiful photo.