C # image refinement: Hilditch refinement Algorithm

Source: Internet
Author: User

Theory:

To refine the algorithm, perform the following steps:

Iteration of each pixel from left to right from top to down is an iteration cycle. In each iteration cycle, if each pixel P meets six conditions at the same time, it is marked. At the end of the current iteration cycle, the values of all marked pixels are set as background values. If a tag does not exist in an iteration cycle (that is, pixels that meet six conditions), the algorithm ends. If the background value is 0 and the foreground value is 1, then:

The six conditions are as follows:

(I): p is 1, that is, P is not the background;

(2): The numbers X1, X3, X5, and X7 are not all 1 (otherwise, the p Mark is deleted and the image is hollow );

(3): X1 ~ In X8, at least two of them are 1 (if only one is 1, it is the endpoint of the line segment. If no value is 1, it is an isolated point );

(4): the 8-connected concatenation of P is 1;

Concatenation refers to the number of graphical components connected to P in the 3*3 neighborhood of P:

(5) Assuming X3 has been marked and deleted, when X3 is 0, the 8 CCN Number of P is 1;

(6) If X5 has been marked to be deleted, when X5 is 0, the 8 Unicom connection count of P is 1.

The above theoretical choice network blog: http://www.cnblogs.com/xiaotie/archive/2010/08/12/1797760.html (this blog has C # version of hilditch refinement algorithm, but the use of unsafe code is not very easy to understand.

The code I wrote last time

 

/// <Summary> /// Hilditch refined algorithm /// </summary> /// <param name = "input"> </param> // <returns> </returns> private int [,] thinnerHilditch (int [,] input) {int lWidth = input. getLength (0); int lHeight = input. getLength (1); bool IsModified = true; int Counter = 1; int [] nnb = new int [9]; // remove the border pixel for (int I = 0; I <lWidth; I ++) {input [I, 0] = 0; input [I, lHeight-1] = 0 ;}for (int j = 0; j <lHeight; J ++) {input [0, j] = 0; input [lWidth-1, j] = 0;} do {Counter ++; IsModified = false; int [,] nb = new int [3, 3]; for (int I = 1; I <lWidth; I ++) {for (int j = 1; j <lHeight; j ++) {// condition 1 must be a black spot if (input [I, j]! = 1) {continue;} // take 3*3 fields for (int m = 0; m <3; m ++) {for (int n = 0; n <3; n ++) {nb [m, n] = input [I-1 + m, j-1 + n] ;}} // copy nnb [0] = nb [2, 1] = 1?; Nnb [1] = nb [2, 0] = 1?; Nnb [2] = nb [1, 0] = 1?; Nnb [3] = nb [0, 0] = 1?; Nnb [4] = nb [0, 1] = 1?; Nnb [5] = nb [0, 2] = 1?; Nnb [6] = nb [1, 2] = 1?; Nnb [7] = nb [2, 2] = 1? 0: 1; // condition 2: p0, p2, p4, p6 is not all foreground vertex if (nnb [0] = 0 & nnb [2] = 0 & nnb [4] = 0 & nnb [6] = = 0) {continue;} // Condition 3: p0 ~ P7 has at least two foreground vertices: int iCount = 0; for (int ii = 0; ii <8; ii ++) {iCount + = nnb [ii];} if (iCount> 6) continue; // Condition 4: the number of links equals 1 if (DetectConnectivity (nnb )! = 1) {continue;} // condition 5: if p2 is marked to be deleted, p2 is set as the background without changing the concatenation Number of p. if (input [I, j-1] =-1) {nnb [2] = 1; if (DetectConnectivity (nnb )! = 1) continue; nnb [2] = 0;} // Condition 6: If p4 is marked as deleted, the p4 is set as the background, do not change the concatenation Number of p if (input [I, j + 1] =-1) {nnb [6] = 1; if (DetectConnectivity (nnb )! = 1) continue; nnb [6] = 0;} input [I, j] =-1; IsModified = true ;}} for (int I = 0; I <lWidth; I ++) {for (int j = 0; j <lHeight; j ++) {if (input [I, j] =-1) {input [I, j] = 0 ;}}}while (IsModified); return input ;}

I hope it will be useful to you.

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.