Figure 12 A region-growing algorithm improved to work with any gray shades, limit the size of regions, and exclude pixels with special values (like edges)

1. Given:
      Image g with m rows and n columns
      g(i,j) for i=1,m j=1,n
      g(i,j) = gray shades
              = FORGET_IT value if edges are overlayed (optional)
      Image output with m rows and n columns
      output(i,j) for i=1,m j=1,n
      output(i,j) = all zeros
      Parameter diff = allowable difference in gray shades
      Parameter min_area = minimum size of a region allowed
      Parameter max_area = maximum size of a region allowed

2. set g_label=2  this is the label value

3.    for (i=0; i<m; i++)
            scan ith row
            for (j=0; j<n; j++)
                 check jth element
                 stack_empty = true
                 target      = g(i,j)
                 sum         = target
                 count       = 0

4.               if g(i,j)      != FORGET_IT   AND
                    output(i,j)  == 0          AND
                    is_close(g(i,j), target, diff)
                       pixel_label_and_check_neighbor(g(i,j), output,
                                            count, sum, target, diff)
                       object_found = 1
                 end if

5.               while stack_empty = false do
                       pop element (i,j) off the stack
                       pixel_label_and_check_neighbor(g(i,j), output,
                                             count, sum, target, diff)
                 end while

6.               if(object_found == 1)
                       object_found = 0
                       if(count>= min_area AND
                          count <= max_area)
                             g_label = g_label + 1
                       else remove object
                             for all output(i,j)  = g_label
                                   output(i,j) =  0
                                   input(i,j)  =  FORGET_IT
                       end else remove object
                 end if

            end of checking jth element
      end of scanning ith row

7. The End

                  --------------------------------------

procedure pixel_label_and_check_neighbor(g(r,e), output,
                                         count, sum,
                                         target, diff)

      output(r,e)  =  g_label
      count        =  count+1
      sum          =  sum + g(r,e)
      target       =  sum/count

      for (R=r-1; r<=r+1; R++)
            for (E=e-1; e<=e+1; e++)
                 if g(R,E)    !=  FORGET_IT  AND
                    output(R,E)  ==  0          AND
                    is_close(g(R,E), target, diff)
                       push (R,E) onto the stack
                       stack_empty  =  false
                 end if

            end loop over E
      end loop over R
end procedure pixel_label_and_check_neighbor

                  ---------------------------------------

procedure is_close(pixel, target, diff)
      if absolute value(pixel - target) < diff
            return 1
      else
            return 0
end procedure is_close