⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 segments.c

📁 This is code tutorial for image processing include:histogram,sketon....
💻 C
📖 第 1 页 / 共 5 页
字号:


     /*******************************************
     *
     *   find_cutoff_point(..
     *
     *   This function looks at a histogram
     *   and sets a cuttoff point at a given
     *   percentage of pixels.
     *   For example, if percent=0.6, you
     *   start at 0 in the histogram and count
     *   up until you've hit 60% of the pixels.
     *   Then you stop and return that pixel
     *   value.
     *
     ********************************************/

find_cutoff_point(histogram, percent, cutoff)
   unsigned long histogram[];
   float    percent;
   short    *cutoff;
{
   float  fd, fsum, sum_div;
   int    i, looking;
   long   lc, lr, num=0, sum=0;

   sum     = 0;
   i       = 0;
   lr      = (long)(ROWS);
   lc      = (long)(COLS);
   num     = lr*lc;
   fd      = (float)(num);

   while(looking){
      fsum    = (float)(sum);
      sum_div = fsum/fd;
      if(sum_div >= percent)
         looking = 0;
      else
         sum = sum + histogram[i++];
   }  /* ends while looking */

   if(i >= 256) i = 255;
   *cutoff = i;
   printf("\nCutoff is %d sum=%ld", *cutoff, sum);
}  /* ends find_cutoff_point */





     /*******************************************
     *
     *   edge_region(..
     *
     *   This function segments an image by
     *   growing regions inside of edges.
     *   The steps are:
     *      . detect edges
     *      . threshold edge output to a
     *        percent value
     *      . remove edges from consideration
     *      . grow regions
     *
     *******************************************/


edge_region(in_name, out_name, the_image, out_image,
            il, ie, ll, le, edge_type, min_area,
            max_area, diff, percent, set_value,
            erode)
   char     in_name[], out_name[];
   float    percent;
   int      edge_type, il, ie, ll, le;
   short    diff, erode, 
            max_area, min_area, 
            set_value,
            the_image[ROWS][COLS],
            out_image[ROWS][COLS];
{

   int    a, b, count, i, j, k,
          length, width;
   short  cutoff;
   struct tiff_header_struct image_header;
   unsigned long histogram[GRAY_LEVELS+1];


   create_file_if_needed(in_name, out_name, out_image);

   read_tiff_image(in_name, the_image, il, ie, ll, le);

      /***************************
      *
      *   Detect the edges.  Do
      *   not threshold.
      *
      ****************************/

   if(edge_type == 1  ||
      edge_type == 2  ||
      edge_type == 3)
      detect_edges(in_name, out_name, the_image,
                   out_image, il, ie, ll, le,
                   edge_type, 0, 0);

   if(edge_type == 4){
      quick_edge(in_name, out_name, the_image,
                 out_image, il, ie, ll, le,
                 0, 0);
   }  /* ends if 4 */

   if(edge_type == 5){
      homogeneity(in_name, out_name, the_image,
                 out_image, il, ie, ll, le,
                 0, 0);
   }  /* ends if 5 */

   if(edge_type == 6){
      difference_edge(in_name, out_name, the_image,
                 out_image, il, ie, ll, le,
                 0, 0);
   }  /* ends if 6 */

   if(edge_type == 7){
      contrast_edge(in_name, out_name, the_image,
                 out_image, il, ie, ll, le,
                 0, 0);
   }  /* ends if 7 */

   if(edge_type == 8){
      gaussian_edge(in_name, out_name, the_image,
                 out_image, il, ie, ll, le,
                 3, 0, 0);
   }  /* ends if 8 */

   if(edge_type == 10){
      range(in_name, out_name, the_image,
            out_image, il, ie, ll, le,
            3, 0, 0);
   }  /* ends if 10 */

   if(edge_type == 11){
      variance(in_name, out_name, the_image,
               out_image, il, ie, ll, le,
               0, 0);
   }  /* ends if 11 */

/**write_array_into_tiff_image("f:e1.tif", out_image,
il, ie, ll, le);**/

      /* copy out_image to the_image */
   for(i=0; i<ROWS; i++)
      for(j=0; j<COLS; j++)
         the_image[i][j] = out_image[i][j];

      /******************************
      *
      *   Threshold the edge detector
      *   output at a given percent.
      *   This eliminates the weak
      *   edges.
      *
      *******************************/
   zero_histogram(histogram);
   calculate_histogram(the_image, histogram);
   find_cutoff_point(histogram, percent, &cutoff);
   threshold_image_array(the_image, out_image,
                         255, cutoff, set_value);
/**write_array_into_tiff_image("f:e2.tif", out_image,
il, ie, ll, le);**/

   if(erode != 0){
         /* copy out_image to the_image */
      for(i=0; i<ROWS; i++)
         for(j=0; j<COLS; j++)
            the_image[i][j] = out_image[i][j];
      erode_image_array(the_image, out_image,
                        set_value, erode);
   }  /* ends if erode */

/**write_array_into_tiff_image("f:e3.tif", out_image,
il, ie, ll, le);**/

      /*******************************
      *
      *   Set all the edge values to
      *   FORGET_IT so the region
      *   growing will not use those
      *   points.
      *
      *******************************/

   for(i=0; i<ROWS; i++)
      for(j=0; j<COLS; j++)
         if(out_image[i][j] == set_value)
            out_image[i][j] = FORGET_IT;

   for(i=0; i<ROWS; i++)
      for(j=0; j<COLS; j++)
         the_image[i][j] = out_image[i][j];

   pixel_grow(the_image, out_image, diff,
              min_area, max_area);

   write_array_into_tiff_image(out_name, out_image,
                               il, ie, ll, le);

}  /* ends edge_region */



     /*******************************************
     *
     *   gray_shade_region(...
     *
     *   This function segments an image by
     *   growing regions based only on gray
     *   shade.
     *
     *******************************************/


gray_shade_region(in_name, out_name, the_image,
                  out_image, il, ie, ll, le,
                  diff, min_area, max_area)
   char   in_name[], out_name[];
   int    il, ie, ll, le;
   short  the_image[ROWS][COLS],
          out_image[ROWS][COLS],
          diff, min_area, max_area;
{
   int    a, b, big_count, count, i, j, k, l,
          not_finished, length, width;
   short  temp[3][3];
   struct tiff_header_struct image_header;

   create_file_if_needed(in_name, out_name, out_image);

   read_tiff_image(in_name, the_image, il, ie, ll, le);
   pixel_grow(the_image, out_image, diff,
              min_area, max_area);
   write_array_into_tiff_image(out_name, out_image,
                               il, ie, ll, le);

}  /* ends gray_shade_region */





     /*******************************************
     *
     *   edge_gray_shade_region(..
     *
     *   This function segments an image by
     *   growing gray shade regions inside of
     *   edges.  It combines the techniques
     *   of the edge_region and gray_shade_region
     *   functions.
     *
     *   The steps are:
     *      . detect edges
     *      . threshold edge output to a
     *        percent value
     *      . lay the edges on top of the original
     *        image to eliminate them from
     *        consideration
     *      . grow regions
     *
     *******************************************/

edge_gray_shade_region(in_name, out_name, the_image,
            out_image, il, ie, ll, le, edge_type,
            min_area, max_area, diff, percent,
            set_value, erode)
   char     in_name[], out_name[];
   float    percent;
   int      edge_type, il, ie, ll, le;
   short    diff, erode, 
            max_area, min_area, 
            set_value,
            the_image[ROWS][COLS],
            out_image[ROWS][COLS];
{
   int    a, b, count, i, j, k,
          length, width;
   short  cutoff;
   struct tiff_header_struct image_header;
   unsigned long histogram[GRAY_LEVELS+1];

   create_file_if_needed(in_name, out_name, out_image);

   read_tiff_image(in_name, the_image, il, ie, ll, le);

      /***************************
      *
      *   Detect the edges.  Do
      *   not threshold.
      *
      ****************************/

   if(edge_type == 1  ||
      edge_type == 2  ||
      edge_type == 3)
      detect_edges(in_name, out_name, the_image,
                   out_image, il, ie, ll, le,
                   edge_type, 0, 0);

   if(edge_type == 4){
      quick_edge(in_name, out_name, the_image,
                 out_image, il, ie, ll, le,
                 0, 0);
   }  /* ends if 4 */
   if(edge_type == 5){
      homogeneity(in_name, out_name, the_image,
                 out_image, il, ie, ll, le,
                 0, 0);
   }  /* ends if 5 */

   if(edge_type == 6){
      difference_edge(in_name, out_name, the_image,
                 out_image, il, ie, ll, le,
                 0, 0);
   }  /* ends if 6 */

   if(edge_type == 7){
      contrast_edge(in_name, out_name, the_image,
                 out_image, il, ie, ll, le,
                 0, 0);
   }  /* ends if 7 */

   if(edge_type == 8){
      gaussian_edge(in_name, out_name, the_image,
                 out_image, il, ie, ll, le,
                 3, 0, 0);
   }  /* ends if 8 */

   if(edge_type == 10){
      range(in_name, out_name, the_image,
            out_image, il, ie, ll, le,
            3, 0, 0);
   }  /* ends if 10 */

   if(edge_type == 11){
      variance(in_name, out_name, the_image,
               out_image, il, ie, ll, le,
               0, 0);
   }  /* ends if 11 */

/**write_array_into_tiff_image("f:e1.tif", out_image,
il, ie, ll, le);**/

      /* copy out_image to the_image */
   for(i=0; i<ROWS; i++)
      for(j=0; j<COLS; j++)
         the_image[i][j] = out_image[i][j];

      /******************************
      *
      *   Threshold the edge detector
      *   output at a given percent.
      *   This eliminates the weak
      *   edges.
      *
      *******************************/

   zero_histogram(histogram);
   calculate_histogram(the_image, histogram);
   find_cutoff_point(histogram, percent, &cutoff);
   threshold_image_array(the_image, out_image,
                         255, cutoff, set_value);

/**write_array_into_tiff_image("f:e2.tif", out_image,
il, ie, ll, le);**/

   if(erode != 0){
         /* copy out_image to the_image */
      for(i=0; i<ROWS; i++)
         for(j=0; j<COLS; j++)
            the_image[i][j] = out_image[i][j];
      erode_image_array(the_image, out_image,
                        set_value, erode);
   }  /* ends if erode */

/**write_array_into_tiff_image("f:e3.tif", out_image,
il, ie, ll, le);**/

      /*******************************
      *
      *   Read the original gray shade
      *   image back into the_image.
      *
      *******************************/

   read_tiff_image(in_name, the_image, il, ie, ll, le);

      /*******************************
      *
      *   Overlay the edge values
      *   on top of the original
      *   image by setting them to
      *   FORGET_IT so the region
      *   growing will not use those
      *   points.
      *
      *******************************/

   for(i=0; i<ROWS; i++)
      for(j=0; j<COLS; j++)
         if(out_image[i][j] == set_value)
            the_image[i][j] = FORGET_IT;

/**write_array_into_tiff_image("f:e4.tif", the_image,
il, ie, ll, le);**/

   pixel_grow(the_image, out_image, diff,
              min_area, max_area);

   write_array_into_tiff_image(out_name, out_image,
                               il, ie, ll, le);

}  /* ends edge_gray_shade_region */




    /**********************************************
    *
    *   pixel_grow(...
    *
    *   The function grows regions.  It is similar
    *   to the grow function in segment.c, but it
    *   has several new capabilities.  It can
    *   eliminate regions if they are too large or
    *   too small.
    *
    *   It ignores pixels = FORGET_IT.  This allows
    *   it to ignore edges or regions already
    *   eliminated from consideration.
    *
    *   It adds pixels to a growing region only if
    *   the pixel is close enough to the average gray

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -