Team Gryffindor: Difference between revisions

From Cpre584
Jump to navigation Jump to search
Marinama (talk | contribs)
Pm429015 (talk | contribs)
No edit summary
Line 20: Line 20:
Sobel edge detection algorithm code with C (''Since the code is very long, I just show key part of algorithm''):
Sobel edge detection algorithm code with C (''Since the code is very long, I just show key part of algorithm''):


   int sobel(double threshold) {
   * files
  unsigned int x, y, i, v, u;            // for loop counter
** mypgm.h - header file 
  unsigned char R, G, B;        // color of R, G, B
** sobel.c - sobel algorithm
  double val[MASK_N] = {0.0};
** Lena.pgm - image files
  int adjustX, adjustY, xBound, yBound;
 
  double total;
Sobel edge detection algorithm code with C
 
  for(y = 0; y != height; ++y) {
void sobel_filtering( ) {
    for(x = 0; x != width; ++x) {  
    /* Spatial filtering of image data */
      for(i = 0; i != MASK_N; ++i) {
    /* Sobel filter (horizontal differentiation */
        adjustX = (MASK_X % 2) ? 1 : 0;
    /* Input: image1[y][x] ---- Outout: image2[y][x] */
                adjustY = (MASK_Y % 2) ? 1 : 0;
  /* Definition of Sobel filter in horizontal direction */
                xBound = MASK_X / 2;
  int maskx[3][3] = {{-1,0,1},{-2,0,2},{-1,0,1}};
                yBound = MASK_Y / 2;
  int masky[3][3] = {{1,2,1},{0,0,0},{-1,-2,-1}};
           
  double pixel_value_x, pixel_value_y, pixel_value;
        val[i] = 0.0;
  double min, max;
        for(v = -yBound; v != yBound + adjustY; ++v) {
  int x, y, i, j; /* Loop variable */
                    for (u = -xBound; u != xBound + adjustX; ++u) {
  /* Initialization of image2[y][x] */
            if (x + u >= 0 && x + u < width && y + v >= 0 && y + v < height) {
  x_size2 = x_size1;
              R = *(image_s + byte_per_pixel * (width * (y+v) + (x+u)) + 2);
  y_size2 = y_size1;
              G = *(image_s + byte_per_pixel * (width * (y+v) + (x+u)) + 1);
  for (y = 0; y < y_size2; y++) {
               B = *(image_s + byte_per_pixel * (width * (y+v) + (x+u)) + 0);
    for (x = 0; x < x_size2; x++){
             
      image2[y][x] = 0;
                  val[i] +=    color_to_int(R, G, B) * mask[i][u + xBound][v + yBound];
    }
            }
  }
                    }
  /* Generation of image2 after linear transformtion */
        }
  for (y = 1; y < y_size1 - 1; y++) {
      }
    for (x = 1; x < x_size1 - 1; x++) {
      pixel_value_x = 0.0;
      total = 0.0;
      pixel_value_y = 0.0;
      for (i = 0; i != MASK_N; ++i) {
        for (j = -1; j <= 1; j++) {  
              total += val[i] * val[i];
        for (i = -1; i <= 1; i++) {
      }
              pixel_value_x += image1[y+j][x+i] * maskx[j+1][i+1];// do x-convolution
               pixel_value_y += image1[y+j][x+i] * masky[j+1][i+1];// do y-convolution }    
          total = sqrt(total);
      pixel_value = (abs(pixel_value_x)+abs(pixel_value_y)); // easier to implement on hardware 
         
      if (pixel_value >= 255)pixel_value=255;
      if (total - threshold >= 0) {
      if (pixel_value <= 0)pixel_value=0;    
        // black
      image2[y][x] = (unsigned char)pixel_value;
        *(image_t + byte_per_pixel * (width * y + x) + 2) = BLACK;
        *(image_t + byte_per_pixel * (width * y + x) + 1) = BLACK;
        *(image_t + byte_per_pixel * (width * y + x) + 0) = BLACK;
      }
            else {
              // white
        *(image_t + byte_per_pixel * (width * y + x) + 2) = WHITE;
        *(image_t + byte_per_pixel * (width * y + x) + 1) = WHITE;
        *(image_t + byte_per_pixel * (width * y + x) + 0) = WHITE;
      }
     }
     }
    }
   }
   }
 
}
  return 0;
main( )
{
  load_image_data( );  /* Input of image1 */
  sobel_filtering( );  /* Sobel filter is applied to image1 */
  save_image_data( );  /* Output of image2 */
  return 0;
  }
  }


We use color lena with 24bit bmp file
We use color lena with 8bit pgm file
[[Image:lena.jpg|200px|thumb|left|alt text]]
[[Image:lena.jpg|200px|thumb|left|alt text]]


Line 91: Line 87:
  %      cumulative  self            self    total           
  %      cumulative  self            self    total           
      
      
'''80.80'''      0.15    0.15       1   145.44  175.53  sobel
  100.27    0.04    0.04       1   40.11   40.11 sobel_filtering
16.72      0.18   0.03  4706312    0.00    0.00 color_to_int
  0.00      0.04     0.00        1    0.00    0.00  load_image_data
0.00      0.18     0.00        1    0.00    0.00  read_bmp
  0.00      0.04     0.00        1    0.00    0.00  save_image_data
0.00      0.18     0.00        1    0.00    0.00  write_bmp
 





Revision as of 03:57, 15 February 2012

Team Gryffindor
Error creating thumbnail: File missing
Gryffindor Team Logo
Team Members
Chad Nelson - cnel711
Mihir Awatramani - mihir
Mengduo Ma - marinama
Kuan-Hsing Ho - pm429015

Assignment #2

Description of Sobel's Algorithm - This is a great website teachs us how it works, and it also provide some examples, which help us to implement in C code.

Compiler:

gcc -O -lm sobel.c -o sobel
./sobel

Sobel edge detection algorithm code with C (Since the code is very long, I just show key part of algorithm):

  * files 
    • mypgm.h - header file
    • sobel.c - sobel algorithm
    • Lena.pgm - image files
Sobel edge detection algorithm code with C
void sobel_filtering( ) {
    /* Spatial filtering of image data */
    /* Sobel filter (horizontal differentiation */
    /* Input: image1[y][x] ---- Outout: image2[y][x] */
 /* Definition of Sobel filter in horizontal direction */
 int maskx[3][3] = {{-1,0,1},{-2,0,2},{-1,0,1}};
 int masky[3][3] = {{1,2,1},{0,0,0},{-1,-2,-1}};
 double pixel_value_x, pixel_value_y, pixel_value;
 double min, max;
 int x, y, i, j;  /* Loop variable */
 /* Initialization of image2[y][x] */
 x_size2 = x_size1;
 y_size2 = y_size1;
 for (y = 0; y < y_size2; y++) {
   for (x = 0; x < x_size2; x++){
     image2[y][x] = 0;
   }
 }
 /* Generation of image2 after linear transformtion */
  for (y = 1; y < y_size1 - 1; y++) {
    for (x = 1; x < x_size1 - 1; x++) {
      pixel_value_x = 0.0;
      pixel_value_y = 0.0;
       for (j = -1; j <= 1; j++) { 
        for (i = -1; i <= 1; i++) {
              pixel_value_x += image1[y+j][x+i] * maskx[j+1][i+1];// do x-convolution
              pixel_value_y += image1[y+j][x+i] * masky[j+1][i+1];// do y-convolution }      
     pixel_value = (abs(pixel_value_x)+abs(pixel_value_y)); // easier to implement on hardware  
     if (pixel_value >= 255)pixel_value=255;
     if (pixel_value <= 0)pixel_value=0;     
     image2[y][x] = (unsigned char)pixel_value;
    }
   }
  }
}
main( )
{
 load_image_data( );   /* Input of image1 */ 
 sobel_filtering( );   /* Sobel filter is applied to image1 */
 save_image_data( );   /* Output of image2 */
 return 0;
}

We use color lena with 8bit pgm file

Error creating thumbnail: File missing
alt text
Error creating thumbnail: File missing
alt text

Profiling Sobel edge detection algorithm

This command will generate a test file:

gcc -lm -pg sobel.c
./a.out
gprof a.out > test
  

test file:

%      cumulative  self             self     total           
   
 100.27    0.04     0.04        1    40.11    40.11  sobel_filtering
 0.00      0.04     0.00        1     0.00     0.00  load_image_data
 0.00      0.04     0.00        1     0.00     0.00  save_image_data












Assignment #2 (New Algorithm)

Compiler:

gcc -O -lm sobel.c -o sobel
./sobel
  • files
    • mypgm.h - header file
    • sobel.c - sobel algorithm
    • Lena.pgm - image files
Sobel edge detection algorithm code with C
void sobel_filtering( ) {
    /* Spatial filtering of image data */
    /* Sobel filter (horizontal differentiation */
    /* Input: image1[y][x] ---- Outout: image2[y][x] */
 /* Definition of Sobel filter in horizontal direction */
 int maskx[3][3] = {{-1,0,1},{-2,0,2},{-1,0,1}};
 int masky[3][3] = {{1,2,1},{0,0,0},{-1,-2,-1}};
 double pixel_value_x, pixel_value_y, pixel_value;
 double min, max;
 int x, y, i, j;  /* Loop variable */
 /* Initialization of image2[y][x] */
 x_size2 = x_size1;
 y_size2 = y_size1;
 for (y = 0; y < y_size2; y++) {
   for (x = 0; x < x_size2; x++){
     image2[y][x] = 0;
   }
 }
 /* Generation of image2 after linear transformtion */
  for (y = 1; y < y_size1 - 1; y++) {
    for (x = 1; x < x_size1 - 1; x++) {
      pixel_value_x = 0.0;
      pixel_value_y = 0.0;
       for (j = -1; j <= 1; j++) { 
        for (i = -1; i <= 1; i++) {
              pixel_value_x += image1[y+j][x+i] * maskx[j+1][i+1];// do x-convolution
              pixel_value_y += image1[y+j][x+i] * masky[j+1][i+1];// do y-convolution }      
     pixel_value = (abs(pixel_value_x)+abs(pixel_value_y)); // easier to implement on hardware  
     if (pixel_value >= 255)pixel_value=255;
     if (pixel_value <= 0)pixel_value=0;     
     image2[y][x] = (unsigned char)pixel_value;
    }
   }
  }
}
main( )
{
 load_image_data( );   /* Input of image1 */ 
 sobel_filtering( );   /* Sobel filter is applied to image1 */
 save_image_data( );   /* Output of image2 */
 return 0;
}