Ask Your Question
0

Filter 2D with even length kernels

asked 2014-03-19 11:42:34 -0600

updated 2014-03-19 12:10:03 -0600

berak gravatar image

I'm new to using OpenCV, and want to convolve an even length kernel, say 4 x 4 kernel with an image. Does filter 2D handle only odd length kernels?

Can anyone show me an example of how to use it with even length kernels for convolution?

Thanks H

edit retag flag offensive close merge delete

2 answers

Sort by » oldest newest most voted
1

answered 2014-03-20 02:42:31 -0600

Filter2D() has no problem with even size kernel, but some other functions use it in their implementations do, like GaussianBlur (see more at link text). But I think this small obstacle is not the problem, you can rectify it by adding a new row (if kernel's row is even) or a new col (if kernel's col is even) with zero values to the first or the last row, (or column) of the kernel, depending on where you want the anchor point of the kernel is. Following is a small example of using filter2D function and its result:

#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
int main(int argc, char * argv[])
{
    Mat kernel=(Mat_<float>(2,2)<<
        0.0,-1.0,
        1.0,0.0);
    Mat src = imread(argv[1], CV_LOAD_IMAGE_GRAYSCALE);

    Mat dst;

    // anchor Point
    Point anchor(kernel.cols - kernel.cols/2 - 1, kernel.rows - kernel.rows/2 - 1);

    // flip the kernel
    Mat fl_kernel;
    int flip_code = -1; // -1-both axis, 0-x-axis, 1-y-axis
    cv::flip(kernel, fl_kernel, flip_code);

    int borderMode = BORDER_CONSTANT;
    filter2D(src, dst, src.depth(), fl_kernel, anchor, borderMode);
    cv::normalize(dst, dst, 0, 255, cv::NORM_MINMAX, CV_8U);
    imshow("Filter2D demo-Source image",src);
    imshow("Filter2D demo-Result image",dst);
    waitKey(0);

    return 0;
}

Result.

edit flag offensive delete link more
0

answered 2014-03-19 19:05:14 -0600

wuling gravatar image

Hi, the same like odd length kernel, for example:

Mat ker=(Mat_<float>(2,2)<<
0.0,-1.0,
1.0,0.0);

filter2D(src,dst,prewit,CV_32FC,ker)//other paramemter set default

....................

edit flag offensive delete link more

Question Tools

Stats

Asked: 2014-03-19 11:42:34 -0600

Seen: 3,714 times

Last updated: Mar 20 '14