Implementing contraharmonic filter I am at the moment trying to implement a contra harmonic filter,to remove the pepper noise from an image. But the implementation of it seems a bit incorrect, and I am not sure what is wrong. 

void ContraHarmonic(Mat src, Mat dst, int kernel, double P)
{
  cout &lt;&lt; "got it" &lt;&lt; endl;
    Mat temp = src.clone();
    //copyMakeBorder(temp,temp,kernel-1,kernel-1,kernel-1,kernel-1,BORDER_CONSTANT,Scalar(0,0,0));

    for(int row = kernel/2; row &lt; temp.rows - kernel/2-1; row++)
    {
        for(int col = kernel/2; col &lt; temp.cols - kernel/2-1; col++)
        {

        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;float&gt;(row+i,col+j),P);
                num += pow(temp.at&lt;float&gt;(row+i,col+j),P+1);
            }
          }

        dst.at&lt;float&gt;(row,col) = num/den;
      }
    }
    dst.convertTo(dst,CV_8U);
}


The output i receive from it point at something being wrong with it.. I am not sure whether the implementation is incorrect, or the programming isn't done correctly.  This is the output i receive.. Which point at something being wrong. 



--Updated--

Updated with the answer provided and the image looks like this
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;