Opencv: ¿cómo funciona realmente el método filter2D ()?

Busqué el código fuente de Filter2D pero no lo encontré. Tampoco podría Visual C ++. ¿Hay algún experto en el algoritmo filter2D aquí? Lo sécomo se supone que funciona pero no como realmente funciona Hice mi propia función filter2d () para probar cosas, y los resultados son sustancialmente diferentes a los de opencvs filter2D (). Aquí está mi código:

Mat myfilter2d(Mat input, Mat filter){

Mat dst = input.clone();
cout << " filter data successfully found.  Rows:" << filter.rows << " cols:" << filter.cols << " channels:" << filter.channels() << "\n";
cout << " input data successfully found.  Rows:" << input.rows << " cols:" << input.cols << " channels:" << input.channels() << "\n";

for (int i = 0-(filter.rows/2);i<input.rows-(filter.rows/2);i++){
    for (int j = 0-(filter.cols/2);j<input.cols-(filter.cols/2);j++){  //adding k and l to i and j will make up the difference and allow us to process the whole image
        float filtertotal = 0;
        for (int k = 0; k < filter.rows;k++){
            for (int l = 0; l < filter.rows;l++){
                if(i+k >= 0 && i+k < input.rows && j+l >= 0 && j+l < input.cols){  //don't try to process pixels off the endge of the map
                    float a = input.at<uchar>(i+k,j+l);
                    float b = filter.at<float>(k,l);
                    float product = a * b;
                    filtertotal += product;
                }
            }
        }
        //filter all proccessed for this pixel, write it to dst
        st.at<uchar>(i+(filter.rows/2),j+(filter.cols/2)) = filtertotal;

    }
}
return dst;
}

¿Alguien ve algo mal con mi implementación? (además de ser lento)

Aquí está mi ejecución:

  cvtColor(src,src_grey,CV_BGR2GRAY);
  Mat dst = myfilter2d(src_grey,filter);
  imshow("myfilter2d",dst);
  filter2D(src_grey,dst2,-1,filter);
  imshow("filter2d",dst2);

Aquí está mi núcleo:

float megapixelarray[basesize][basesize] = {
            {1,1,-1,1,1},
            {1,1,-1,1,1},
            {1,1,1,1,1},
            {1,1,-1,1,1},
            {1,1,-1,1,1}
            };

YAquí están los resultados (sustancialmente diferentes):

Pensamientos, alguien?

EDITAR: Gracias a la respuesta de Brians he añadido este código:

//normalize the kernel so its sum = 1
Scalar mysum = sum(dst);
dst = dst / mysum[0];   //make sure its not 0
dst = dst * -1;  //show negetive

y filter2d funcionó mejor. Ciertos filtros dan una coincidencia exacta, y otros filtros, como el Sobel,fracasar miserablemente.

Me estoy acercando al algoritmo real, pero todavía no. ¿Alguien más con alguna idea?

Respuestas a la pregunta(2)

Su respuesta a la pregunta