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; } |