Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

low-pass filter in opencv

I would like to know how I can do a low-pass filter in opencv on an IplImage. For example "boxcar" or something similar.

I've googled it but i can't find a clear solution. If anyone could give me an example or point me in the right direction on how to implement this in opencv or javacv I would be grateful.

Thx in advance.

like image 974
Olivier_s_j Avatar asked Mar 23 '12 14:03

Olivier_s_j


2 Answers

Here is an example using the C API and IplImage:

#include "opencv2/imgproc/imgproc_c.h"
#include "opencv2/highgui/highgui_c.h"

int main()
{
    IplImage* img = cvLoadImage("input.jpg", 1);
    IplImage* dst=cvCreateImage(cvGetSize(img),IPL_DEPTH_8U,3);
    cvSmooth(img, dst, CV_BLUR);
    cvSaveImage("filtered.jpg",dst);
}

For information about what parameters of the cvSmooth function you can have a look at the cvSmooth Documentation.

If you want to use a custom filter mask you can use the function cvFilter2D:

#include "opencv2/imgproc/imgproc_c.h"
#include "opencv2/highgui/highgui_c.h"

int main()
{
    IplImage* img = cvLoadImage("input.jpg", 1);
    IplImage* dst=cvCreateImage(cvGetSize(img),IPL_DEPTH_8U,3);
    double a[9]={   1.0/9.0,1.0/9.0,1.0/9.0,
                    1.0/9.0,1.0/9.0,1.0/9.0,
                    1.0/9.0,1.0/9.0,1.0/9.0};
    CvMat k;
    cvInitMatHeader( &k, 3, 3, CV_64FC1, a );

    cvFilter2D( img ,dst, &k,cvPoint(-1,-1));
    cvSaveImage("filtered.jpg",dst);
}

These examples use OpenCV 2.3.1.

like image 61
sietschie Avatar answered Sep 30 '22 08:09

sietschie


The openCV filtering documentation is a little confusing because the functions try and efficently cover every possible filtering technique.

There is a tutorial on using your own filter kernels which covers box filters

like image 20
Martin Beckett Avatar answered Sep 30 '22 06:09

Martin Beckett