Image binarization threshold (kirsch operator)

Source: Internet
Author: User

Image binarization threshold kirsch operator

The implementation of the kirsch operator is a little complicated. It uses eight templates to perform convolution and derivative for each pixel in the image. These eight templates represent eight directions, the maximum response is made to eight specific edge directions on the image. In the calculation, the maximum value is taken as the edge output of the image (the eight templates used in the preceding algorithm are provided in the implementation code below ). To help readers understand the implementation of the algorithm, we provide the function code to implement the algorithm. You can apply the Code to your project with slight changes.

Bool Kirsch (byte * pdata, int width, int height)
{// Defines eight templates that implement the Kirsch algorithm;
Int I, j, S, T, K, Max, sum [8];
Static A [3] [3] = {+ 5, + 5, + 5}, {-3,-3}, {-3,-3, -3 }};
Static A1 [3] [3] = {-3, + 5, + 5}, {-3, 0, + 5}, {-3,-3, -3 }};
Static A2 [3] [3] = {-3,-3, + 5}, {-3, 0, + 5}, {-3,-3, + 5 }};
Static A3 [3] [3] = {-3,-3,-3}, {-3, 0, + 5}, {-3, + 5, + 5 }};
Static A4 [3] [3] = {-3,-3,-3}, {-3,-3}, {+ 5, + 5, + 5 }};
Static A5 [3] [3] = {-3,-3,-3}, {+ 5, 0,-3}, {+ 5, + 5, -3 }};
Static A6 [3] [3] = {+ 5,-3,-3}, {+ 5,-3}, {+ 5,-3, -3 }};
Static A7 [3] [3] = {+ 5, + 5,-3}, {+ 5,-3}, {-3,-3, -3 }};
Byte * pdata1;
If (pdata = NULL)
{
Afxmessagebox ("image data is empty. Please read image data! ");
Return false;
}
Pdata1 = (byte *) New char [width * Height];
If (pdata1 = NULL)
{
Afxmessagebox ("Image Buffer data zone application failed. Please apply for image data buffer again! ");
Return false;
}
Memcpy (pdata1, pdata, width * 8 * Height );
// The kirsch operator is used to calculate the derivative of each pixel in eight directions ;;
For (I = 1; I For (j = 1; j <width-1; j ++)
{
Sum [1] = sum [2] = sum [3] = sum [4] = sum [5] = sum [6] = sum [7] = sum [8] = 0;
For (t =-1; t <2; t ++)
{
For (S =-1; S <2; s ++)
{Sum [1] + = * (pdata + widthbytes (width * 8) * (I + t) + J + S) * A [1 + T] [1 + S];
Sum [2] + = * (pdata + widthbytes (width * 8) * (I + t) + J + S) * A1 [1 + T] [1 + S]; sum [3] + = * (pdata + widthbytes (width * 8) * (I + t) + J + S) * A2 [1 + T] [1 + S]; sum [4] + = * (pdata + widthbytes (width * 8) * (I + t) + J + S) * A3 [1 + T] [1 + S]; sum [5] + = * (pdata + widthbytes (width * 8) * (I + t) + J + S) * A4 [1 + T] [1 + S]; sum [6] + = * (pdata + widthbytes (width * 8) * (I + t) + J + S) * A5 [1 + T] [1 + S]; sum [7] + = * (pdata + widthbytes (width * 8) * (I + t) + J + S) * A6 [1 + T] [1 + S]; sum [8] + = * (pdata + widthbytes (width * 8) * (I + t) + J + S) * A7 [1 + T] [1 + S];
}
}
// Obtain the derivative of the maximum direction;
For (k = 0; k <8; k ++)
{
Max = 0;
If (max <sum [k])
Max = sum [k];
}
If (max <0)
Max = 0;
If (max> 255)
Max = 255;
* (Pdata1 + width * 8 * I + J) = max;
}
Memcpy (pdata, pdata1, width * 8 * Height );
Delete pdata1;
Return true;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.