sobel.h 3.25 KB
Newer Older
Anirudh Kaushik's avatar
Anirudh Kaushik committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
//========================================================================================
// 
// File Name    : sobel.h
// Description  : SOBEL filter declaration
// Release Date : 23/07/2013
// Author       : PolyU DARC Lab
//                Benjamin Carrion Schafer, Anushree Mahapatra 
//
// Revision History
//---------------------------------------------------------------------------------------
// Date         Version   Author              Description
//---------------------------------------------------------------------------------------
//23/07/2013       1.0     PolyU DARC Lab      main component declaration of SOBEL filter
//=======================================================================================

#ifndef SOBEL_H_
#define SOBEL_H_


#include "define.h"


SC_MODULE (sobel){
    
 public:
  
  sc_in_clk clk; //clock
  sc_in<bool> rst; //reset

  //inputs
  sc_in <sc_uint <8> > input_row[SIZE_BUFFER];
 
  //outputs
 sc_out<sc_uint<8> > output_row;


 // Variables declarations
  sc_uint<8>  line_buffer[SIZE_BUFFER][SIZE_BUFFER];

  //function prototypes
 
 /* S */


void sobel_main(void)
{

  sc_uint<8> input_row_read[3];
  sc_uint<8> output_row_write;
  int i,j,x;


  wait();
 
  while(1)
    {

      // Reading triplet data needed for filter
      for(x=0;x<ROWS; x++){
	for(j=1;j<COLS;j++){
	  for(i=j-1;i<= j+1;i++){

	    if(i== j-1)
	      input_row_read[0] = input_row[0].read();
	    if(i==j)
	      input_row_read[1] = input_row[1].read();
	    else
	      input_row_read[2] = input_row[2].read();
	  }

	  // Perform the filtering of a 3x3 pixel image segment
	 output_row_write =  sobel_filter(input_row_read);
	 
	 
      // Writting filtered output back 
      output_row.write(output_row_write);
      wait();
	}
      }  
  }// end of while
}


//
// Sobel filter function
//
sc_uint<8>  sobel_filter(sc_uint<8> *input_row_r)
{

  unsigned int X, Y;
  unsigned char orow;
  int sumX, sumY;
  int SUM, rowOffset, colOffset;


  
  char Gx[3][3] ={{-1 ,-2 ,-1},
		  { 0, 0, 0},
		  { +1, 2, +1}};  


  char Gy[3][3] ={{1, -2, 1},
		  {0, 0, 0},
		  {-1, 0, 1}};


  /* Shifting 3x3 line buffer by one row  */

   for(Y=2;Y>0;Y--){
     for(X=0;X< 3;X++){
       line_buffer[Y][X]=line_buffer[Y-1][X];
     }
   }	
	
   // Reading new data into the line buffer
   for(X=0; X<SIZE_BUFFER; X++)
     line_buffer[0][X] = input_row_r[X];

 

   Y=1;
   X=1;
   sumX = 0;
   sumY = 0;

   // Convolution starts here
   //-------X GRADIENT APPROXIMATION------
   //-------Y GRADIENT APPROXIMATION------   
   for(rowOffset = -1; rowOffset <= 1; rowOffset++){
     for(colOffset = -1; colOffset <=1; colOffset++){
       sumX = sumX + line_buffer[Y -rowOffset][X+colOffset] * Gx[1+rowOffset][1+colOffset];
       sumY = sumY + line_buffer[Y -rowOffset][X+colOffset] * Gy[1+rowOffset][1+colOffset];
   	}
    }


      if(sumX > 255)    sumX = 255;
      else if(sumX < 0) sumX = 0;

      if(sumY > 255)    sumY = 255;
      else if(sumY < 0) sumY = 0;


      SUM = sumX + sumY;
      
      if(SUM > 255)    SUM = 255;
         else if(SUM < 0) SUM = 0;     
   
      orow = 255  - (unsigned char)(SUM);
      return ((sc_uint<8>) orow);

}
 

 SC_CTOR (sobel){
    SC_CTHREAD(sobel_main,clk.pos());
    reset_signal_is(rst,false); 
 };

 ~sobel(){};



};
   
#endif  // SOEBEL_H_