home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_10_01 / 1001049a < prev    next >
Text File  |  1991-12-12  |  10KB  |  365 lines

  1.  
  2.        /***********************************************
  3.        *
  4.        *       Functions: This file contains
  5.        *          homogeneity
  6.        *          difference_edge
  7.        *          contrast_edge
  8.        *
  9.        *       Purpose:
  10.        *          These functions implement several
  11.        *          types of advanced edge detection.
  12.        *
  13.        *       External Calls:
  14.        *          wtiff.c - does_not_exist
  15.        *                    round_off_image_size
  16.        *                    create_allocate_tiff_file
  17.        *                    write_array_into_tiff_image
  18.        *          tiff.c - read_tiff_header
  19.        *          rtiff.c - read_tiff_image
  20.        *          numcvrt.c - get_integer
  21.        *          edge.c - fix_edges
  22.        *
  23.        *       Modifications:
  24.        *          26 March 1991 - created
  25.        *
  26.        *************************************************/
  27.  
  28.  
  29.  
  30. #include "d:\cips\cips.h"
  31.  
  32.  
  33.  
  34. short e_mask[3][3] = {
  35.        {-9,  0, -9},
  36.        { 0, 36,  0},
  37.        {-9,  0, -9} };
  38.  
  39. short contrast[3][3] = {
  40.    {  1,  1,  1},
  41.    {  1,  1,  1},
  42.    {  1,  1,  1}};
  43.  
  44.  
  45.    /**************************************************
  46.    *
  47.    *   homogeneity(...
  48.    *
  49.    *   This function performs edge detection by looking
  50.    *   for the absence of an edge.  The center of a
  51.    *   3x3 area is replaced by the absolute value of
  52.    *   the max difference between the center point
  53.    *   and its 8 neighbors.
  54.    *
  55.    ***************************************************/
  56.  
  57.  
  58. homogeneity(in_name, out_name, the_image, out_image,
  59.              il, ie, ll, le, threshold, high)
  60.  
  61.    char   in_name[], out_name[];
  62.    int    high, il, ie, ll, le, threshold;
  63.    short  the_image[ROWS][COLS], out_image[ROWS][COLS];
  64. {
  65.    int a, b, absdiff, absmax, diff, i, j,
  66.        length, max, max_diff, new_hi, new_low, width;
  67.  
  68.    struct tiff_header_struct image_header;
  69.  
  70.    if(does_not_exist(out_name)){
  71.       printf("\n\nHOMO> output file does not exist %s",
  72.              out_name);
  73.       read_tiff_header(in_name, &image_header);
  74.       round_off_image_size(&image_header,
  75.                            &length, &width);
  76.       image_header.image_length = length*ROWS;
  77.       image_header.image_width  = width*COLS;
  78.       create_allocate_tiff_file(out_name, &image_header,
  79.                                 out_image);
  80.    }  /* ends if does_not_exist */
  81.  
  82.    read_tiff_header(in_name, &image_header);
  83.  
  84.    new_hi  = 250;
  85.    new_low = 16;
  86.    if(image_header.bits_per_pixel == 4){
  87.        new_hi  = 10;
  88.        new_low = 3;
  89.    }
  90.  
  91.    max = 255;
  92.    if(image_header.bits_per_pixel == 4)
  93.       max = 16;
  94.    read_tiff_image(in_name, the_image, il, ie, ll, le);
  95.  
  96.    for(i=0; i<ROWS; i++)
  97.       if( (i%10) == 0) printf("%3d", i);
  98.       for(j=0; j<COLS; j++)
  99.          out_image[i][j] = 0;
  100.  
  101.    for(i=1; i<ROWS-1; i++){
  102.       for(j=1; j<COLS-1; j++){
  103.  
  104.           max_diff = 0;
  105.           for(a=-1; a<=1; a++){
  106.              for(b=-1; b<=1; b++){
  107.  
  108.                 <%-2>diff = the_image[i][j] - the_image[i+a][j+b];
  109. <%0>                absdiff = abs(diff);
  110.                 if(absdiff > max_diff) max_diff = absdiff;
  111.  
  112.              }  /* ends loop over b */
  113.           }  /* ends loop over a */
  114.  
  115.           out_image[i][j] = max_diff;
  116.       }  /* ends loop over j */
  117.    }  /* ends loop over i */
  118.  
  119.  
  120.      /* if desired, threshold the output image */
  121.  
  122.    if(threshold == 1){
  123.        for(i=0; i<ROWS; i++){
  124.           for(j=0; j<COLS; j++){
  125.              if(out_image[i][j] > high){
  126.                   out_image[i][j] = new_hi;
  127.              }
  128.              else{
  129.                   out_image[i][j] = new_low;
  130.              }
  131.           }
  132.        }
  133.    }  /* ends if threshold == 1 */
  134.  
  135.    fix_edges(out_image, 1);
  136.  
  137.    write_array_into_tiff_image(out_name, out_image,
  138.                                il, ie, ll, le);
  139.  
  140. } /* ends homogeneity */
  141.  
  142.    /**************************************************
  143.    *
  144.    *   difference_edge(...
  145.    *
  146.    *   This function performs edge detection by looking
  147.    *   at the differences in the pixels that surround
  148.    *   the center point of a 3x3 area.  It replaces the
  149.    *   center point with the absolute value of the
  150.    *   max difference of:
  151.    *      upper left - lower right
  152.    *      upper right - lower left
  153.    *      left - right
  154.    *      top - bottom
  155.    *
  156.    ***************************************************/
  157.  
  158. difference_edge(in_name, out_name, the_image, out_image,
  159.                 il, ie, ll, le, threshold, high)
  160.    char   in_name[], out_name[];
  161.    int    high, il, ie, ll, le, threshold;
  162.    short  the_image[ROWS][COLS], out_image[ROWS][COLS];
  163. {
  164.    int a, b, absdiff, absmax, diff, i, j,
  165.        length, max, max_diff, new_hi, new_low, width;
  166.  
  167.    struct tiff_header_struct image_header;
  168.  
  169.  
  170.    if(does_not_exist(out_name)){
  171.       printf("\n\nDIFF> output file does not exist %s",
  172.              out_name);
  173.       read_tiff_header(in_name, &image_header);
  174.  
  175.       round_off_image_size(&image_header,
  176.                            &length, &width);
  177.       image_header.image_length = length*ROWS;
  178.       image_header.image_width  = width*COLS;
  179.       create_allocate_tiff_file(out_name, &image_header,
  180.                                 out_image);
  181.    }  /* ends if does_not_exist */
  182.  
  183.    read_tiff_header(in_name, &image_header);
  184.  
  185.    new_hi  = 250;
  186.    new_low = 16;
  187.    if(image_header.bits_per_pixel == 4){
  188.        new_hi  = 10;
  189.        new_low = 3;
  190.    }
  191.  
  192.    max = 255;
  193.    if(image_header.bits_per_pixel == 4)
  194.       max = 16;
  195.  
  196.    read_tiff_image(in_name, the_image, il, ie, ll, le);
  197.  
  198.    for(i=0; i<ROWS; i++)
  199.       for(j=0; j<COLS; j++)
  200.          out_image[i][j] = 0;
  201.  
  202.    for(i=1; i<ROWS-1; i++){
  203.       if( (i%10) == 0) printf("%3d", i);
  204.       for(j=1; j<COLS-1; j++){
  205.  
  206.           max_diff = 0;
  207.           absdiff = abs(the_image[i-1][j-1] -
  208.                          the_image[i+1][j+1]);
  209.           if(absdiff > max_diff) max_diff = absdiff;
  210.  
  211.           absdiff = abs(the_image[i-1][j+1] -
  212.                          the_image[i+1][j-1]);
  213.           if(absdiff > max_diff) max_diff = absdiff;
  214.  
  215.           absdiff = abs(the_image[i][j-1] -
  216.                          the_image[i][j+1]);
  217.           if(absdiff > max_diff) max_diff = absdiff;
  218.  
  219.           absdiff = abs(the_image[i-1][j] -
  220.                          the_image[i+1][j]);
  221.           if(absdiff > max_diff) max_diff = absdiff;
  222.  
  223.  
  224.           out_image[i][j] = max_diff;
  225.  
  226.       }  /* ends loop over j */
  227.    }  /* ends loop over i */
  228.  
  229.      /* if desired, threshold the output image */
  230.    if(threshold == 1){
  231.        for(i=0; i<ROWS; i++){
  232.           for(j=0; j<COLS; j++){
  233.  
  234.              if(out_image[i][j] > high){
  235.                   out_image[i][j] = new_hi;
  236.              }
  237.              else{
  238.                   out_image[i][j] = new_low;
  239.              }
  240.           }
  241.        }
  242.    }  /* ends if threshold == 1 */
  243.  
  244.  
  245.  
  246.    fix_edges(out_image, 1);
  247.    write_array_into_tiff_image(out_name, out_image,
  248.                                il, ie, ll, le);
  249.  
  250.  
  251. } /* ends difference_edge */
  252.  
  253.  
  254.  
  255.    /**************************************************
  256.    *
  257.    *   contrast_edge(...
  258.    *
  259.    *   The edge detector uses the basic quick edge
  260.    *   detector mask and then divides the result
  261.    *   by a contrast smooth mask.  This implements
  262.    *   Johnson's contrast based edge detector.
  263.    *
  264.    ***************************************************/
  265.  
  266. contrast_edge(in_name, out_name, the_image, out_image,
  267.                 il, ie, ll, le, threshold, high)
  268.    char   in_name[], out_name[];
  269.    int    high, il, ie, ll, le, threshold;
  270.    short  the_image[ROWS][COLS], out_image[ROWS][COLS];
  271. {
  272.    int ad, d;
  273.    int a, b, absdiff, absmax, diff, i, j,
  274.        length, max, new_hi, new_low, sum_d, sum_n, width;
  275.  
  276.    struct tiff_header_struct image_header;
  277.  
  278.  
  279.    if(does_not_exist(out_name)){
  280.       printf("\n\nCONTRAST> output file does not exist %s",
  281.              out_name);
  282.       read_tiff_header(in_name, &image_header);
  283.       round_off_image_size(&image_header,
  284.                            &length, &width);
  285.       image_header.image_length = length*ROWS;
  286.       image_header.image_width  = width*COLS;
  287.  
  288.       create_allocate_tiff_file(out_name, &image_header,
  289.                                 out_image);
  290.    }  /* ends if does_not_exist */
  291.    read_tiff_header(in_name, &image_header);
  292.  
  293.    new_hi  = 250;
  294.    new_low = 16;
  295.    if(image_header.bits_per_pixel == 4){
  296.        new_hi  = 10;
  297.        new_low = 3;
  298.    }
  299.  
  300.    max = 255;
  301.    if(image_header.bits_per_pixel == 4)
  302.       max = 16;
  303.  
  304.    read_tiff_image(in_name, the_image, il, ie, ll, le);
  305.  
  306.    for(i=0; i<ROWS; i++)
  307.       for(j=0; j<COLS; j++)
  308.          out_image[i][j] = 0;
  309.  
  310.    for(i=1; i<ROWS-1; i++){
  311.       if( (i%10) == 0) printf("%3d", i);
  312.       for(j=1; j<COLS-1; j++){
  313.  
  314.          sum_n = 0;
  315.          sum_d = 0;
  316.  
  317.          for(a=-1; a<2; a++){
  318.             for(b=-1; b<2; b++){
  319.                sum_n = sum_n + the_image[i+a][j+b] *
  320.                        e_mask[a+1][b+1];
  321.                sum_d = sum_d + the_image[i+a][j+b] *
  322.                        contrast[a+1][b+1];
  323.             }
  324.          }
  325.  
  326.          d = sum_d / 9;
  327.          if(d == 0)
  328.             d = 1;
  329.  
  330.          out_image[i][j] = sum_n/d;
  331.  
  332.          if(out_image[i][j] > max) out_image[i][j] = max;
  333.          if(out_image[i][j] < 0) out_image[i][j] = 0;
  334.  
  335.  
  336.       }  /* ends loop over j */
  337.    }  /* ends loop over i */
  338.  
  339.  
  340.  
  341.      /* if desired, threshold the output image */
  342.    if(threshold == 1){
  343.        for(i=0; i<ROWS; i++){
  344.           for(j=0; j<COLS; j++){
  345.              if(out_image[i][j] > high){
  346.                   out_image[i][j] = new_hi;
  347.              }
  348.  
  349.              else{
  350.                   out_image[i][j] = new_low;
  351.              }
  352.           }
  353.        }
  354.    }  /* ends if threshold == 1 */
  355.  
  356.  
  357.    fix_edges(out_image, 1);
  358.    write_array_into_tiff_image(out_name, out_image,
  359.                                il, ie, ll, le);
  360.  
  361. } /* ends contrast_edge */
  362.  
  363. /* End of File */ 
  364.  
  365.