Implementing contraharmonic filter
The problem occurred due to the pixel values of temp had the type uchar. 

The solution to my problem was to output the pixel values as uchar, manipulate them as double, and store them as uchar. 

double den=0,num=0;
      for(int i = -(kernel/2); i &lt;= (kernel/2) ; i++)
      {
        for(int j = -(kernel/2) ; j &lt;= (kernel/2); j++)
        {
            den += pow(temp.at&lt;uchar&gt;(row+i,col+j),P);
            num += pow(temp.at&lt;uchar&gt;(row+i,col+j),P+1);
        }
      }

    dst.at&lt;uchar&gt;(row,col) = num/den;