Ask Your Question
0

How does the filter2d algorithm *actually* work?

asked 2012-12-20 16:28:34 -0500

paj006@gmail.com gravatar image

Are there any experts on the filter2D algorithm? I know how it's supposed to work but not how it actually works. I made my own filter2d() function to test things, and the results are substantially different from opencvs filter2D(). Here's my code:

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

Here is my execution:

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

Here is my kernel:

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

And here are the (substantially different) results:

Thoughts, anyone?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2013-03-18 07:41:18 -0500

wuling gravatar image
edit flag offensive delete link more
Login/Signup to Answer

Question Tools

Stats

Asked: 2012-12-20 16:28:34 -0500

Seen: 726 times

Last updated: Mar 18 '13