implementing erosion, dilation in C, C++

后端 未结 4 1040
陌清茗
陌清茗 2021-02-06 06:23

I have theoretical understanding of how dilation in binary image is done.

AFAIK, If my SE (structuring element) is this

0 1
1 1. 

wher

4条回答
  •  失恋的感觉
    2021-02-06 06:37

    /* structure of the image variable
     * variable n stores the order of the square matrix */
    
    typedef struct image{
            int mat[][];
            int n;
            }image;
    
    
    /* function recieves image "to dilate" and returns "dilated"*
     * structuring element predefined:
     *             0  1  0
     *             1  1  1
     *             0  1  0
     */
    
    image* dilate(image* to_dilate)
    {
           int i,j;
           int does_order_increase;
           image* dilated;
    
           dilated = (image*)malloc(sizeof(image));
           does_order_increase = 0;
    
    /* checking whether there are any 1's on d border*/       
    
           for( i = 0 ; in ; i++ )
           {
                if( (to_dilate->a[0][i] == 1)||(to_dilate->a[i][0] == 1)||(to_dilate->a[n-1][i] == 1)||(to_dilate->a[i][n-1] == 1) )
                {
                    does_order_increase = 1;
                    break;
                }
           }
    
    /* size of dilated image initialized */       
    
           if( does_order_increase == 1)
               dilated->n = to_dilate->n + 1;
           else
               dilated->n = to_dilate->n;
    
    /* dilating image by checking every element of to_dilate and filling dilated *
     * does_order_increase serves to cope with adjustments if dilated 's order increase */
    
           for( i = 0 ; in ; i++ )
           {
                for( j = 0 ; jn ; j++ )
                {
                     if( to_dilate->a[i][j] == 1)
                     {
                         dilated->a[i + does_order_increase][j + does_order_increase] = 1;
                         dilated->a[i + does_order_increase -1][j + does_order_increase ] = 1;
                         dilated->a[i + does_order_increase ][j + does_order_increase -1] = 1;
                         dilated->a[i + does_order_increase +1][j + does_order_increase ] = 1;
                         dilated->a[i + does_order_increase ][j + does_order_increase +1] = 1;
                     }
                }
           }
    
    /* dilated stores dilated binary image */
    
           return dilated;
    }
    
    /* end of dilation */ 
    

提交回复
热议问题