Opencv - jak działa metoda filter2D ()?

Szukałem kodu źródłowego do Filter2D, ale nie mogłem go znaleźć. Ani też Visual c ++. Czy są tu jacyś specjaliści od algorytmu filter2D? wiemjak to powinno działać ale nie jak to działa. Zrobiłem własną funkcję filter2d (), aby przetestować rzeczy, a wyniki znacznie różnią się od opencvs filter2D (). Oto mój kod:

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;
}

Czy ktoś widzi coś złego w mojej implementacji? (poza tym, że jest wolny)

Oto moja egzekucja:

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

Oto moje jądro:

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}
            };

IOto (zasadniczo różne) wyniki:

Myśli, ktoś?

EDYCJA: Dzięki odpowiedzi Briana dodałem ten kod:

//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

i filter2d działało lepiej. Niektóre filtry dają dokładne dopasowanie, a inne filtry, takie jak Sobel,okropnie zawieść.

Zbliżam się do rzeczywistego algorytmu, ale jeszcze go nie ma. Czy ktoś jeszcze ma jakieś pomysły?

questionAnswers(2)

yourAnswerToTheQuestion