Recursive algorithm for color substitution

Source: Internet
Author: User

    Suppose a two-dimensional array G[1..M][1..N] represents an area of an image, G[i][j] represents the color of the midpoint (I,J) of the region, and its value is an integer from 0 to K. Try to write a recursive algorithm that replaces the color of the area where the point (i0,j0) is in color C. The i0,j0 of the upper, lower, left, and right adjacent points of the same color are the points of the same color area. The type definition that represents the image area is as follows:/* in G[1..M][1..N], replace the color of the same color area where the element G[i0][j0] *//* is in color C */typedef Char gtype[m+1][n+1];
1Voidchangecolor (GTYPE G,intMintNCharCintI0,intj0)2     {3         Chartemp;4      5         //determine if it is within the legal region6         if(I0 <1|| I0 > M | | J0 <1|| J0 >N)7             return;8      9         //prevent entry into the dead recursionTentemp =G[i0][j0]; One         if(c = =temp) A             return; -      -g[i0][j0]=C; the           //the upper, lower, left, and right points are judged in turn -         if(I0-1>=1&& g[i0-1][j0]==temp) -ChangeColor (g,m,n,c,i0-1, j0); -         if(I0 +1<= m && g[i0+1][j0]==temp) +ChangeColor (g,m,n,c,i0+1, j0); -         if(J0-1>=1&& g[i0][j0-1]==temp) +ChangeColor (g,m,n,c,i0,j0-1); A         if(j0+1<= N && g[i0][j0+1]==temp) atChangeColor (g,m,n,c,i0,j0+1); -}

Idea: The general idea is to replace and find first. Starting from the specified point, save the color value of the point temp and replace it with the C color value, and then determine whether or not the color value in the area is equal to temp, and if so, the next point is recursively judged by the same method, until all the required points are replaced with the C color value.    Some readers will ask: can you put the action of replacing the color value after one of the if statements, that is, let the function find the next point first, and then replace the color value of the specified point?    Let's assume that the statement that replaces the color value is placed after the last if (in other cases), the code looks like this
1Voidchangecolor (GTYPE G,intMintNCharCintI0,intj0)2     {3         Chartemp;4      5         //determine if it is within the legal region6         if(I0 <1|| I0 > M | | J0 <1|| J0 >N)7             return;8      9         //prevent entry into the dead recursionTentemp =G[i0][j0]; One         if(c = =temp) A             return; -       -         //the upper, lower, left, and right points are judged in turn the         if(I0-1>=1&& g[i0-1][j0]==temp) -ChangeColor (g,m,n,c,i0-1, j0); -         if(I0 +1<= m && g[i0+1][j0]==temp) -ChangeColor (g,m,n,c,i0+1, j0); +         if(J0-1>=1&& g[i0][j0-1]==temp) -ChangeColor (g,m,n,c,i0,j0-1); +         if(j0+1<= N && g[i0][j0+1]==temp) AChangeColor (g,m,n,c,i0,j0+1); at       -g[i0][j0]=C; -}
    We know that every invocation of a function will save the function arguments and local variables in the function, so when you call the ChangeColor function you will save G (two-dimensional function pointers, 4 bytes) in the stack, array ranges m and N, replacement color values C, Refers to the fixed-point coordinates i0 and j0, and the local variable temp in the function, and each function stack is independent and non-affected in general, in order to guarantee the function's normal operation.    When we call ChangeColor (g,6,4,2,4,3) to replace the (4,3) point with a color value of 2 o'clock, the arguments to the function and the local variables of the temp stack are pressed, and we simply use (4,3) to represent them. According to the function code, the point above the specified point (3,3) is searched first, because the (3,3) color value is 1, the (3,3) stack is pressed, and the point above is continued on the basis of the (3,3) specified point, at which point the color value is 0 non-conforming; the next specified point (3,3 3) The color value of 1, has not been replaced with a color value of 2, in line with the replacement requirements, will (4,3) pressure stack, then the problem came, took a lap and back to the original point, so that it will be infinitely recursive to go down. Because the stack space is limited, each function call will be stacked, resulting in stack overflow.    When a point jumps to another point without being replaced, and the jump back does not change, it goes into the dead recursion until the stack overflow error occurs, so the color value substitution operation needs to be performed before all jumps.    The attentive reader may ask: what happens if the replacement color value C is the same as the color at the beginning of the specified point? There is no doubt that the last stack overflows with dead recursion. It has been said above to prevent "one point jumping to another point not being replaced before jumping back without changing" in this case, you need to perform a color value substitution operation before all jumps. However, the color value replaced at this time is the same as the color value of the specified point, which is no different from no substitution, when one point jumps to another point, it will also jump back, thus entering a special dead recursion.    Finally through a simple recursive example summed up a sentence: The language is advanced, the algorithm is not rigorous also useless!extended reading: http://www.nowamagic.net/librarys/veda/detail/2314

This article link: http://www.cnblogs.com/cposture/p/4487417.html

Recursive algorithm for color substitution

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.