Ask Your Question
1

Implementing a kernel convolution

asked 2015-11-02 06:32:02 -0600

215 gravatar image

updated 2015-11-02 06:36:09 -0600

Hi guys.. I am felling a bit stupid asking this question, but I can't seem to come around the issue. I am trying to implement a contra harmonic filter using the equation given here

http://masters.donntu.org/2007/kita/gett/library/eng.htm

But I seem to be stuck at implementing the Kernel part of the filter..

The way i've coded until now is using 4 for - loop , first two to interate through the image, and second two to iterate through the kernel..

Mat ContraHarmonic(Mat src, int kernel)
{
    Mat output = src.clone();

    for(int x = 0; x < src.rows; x++)
    {
        for(int y = 0; y < src.cols; y++)
        {
            for(int subX = 0; subX < kernel; subX++)
            {
                for(int subY = 0; subY < kernel ; subY++)
                {
                    output.at<uchar>(x-1+subX,y-1+subY) = 255;
                }
            }
        }
    }
    imshow("output",output);
    waitKey(0);
    return output;
}

I know that the formula isn't right at the moment.. But When run this piece of code.. I do not get an output.. I am bit unsure whether I am doing this right, or I am missing some thing.. How do i detect whether the kernel is within the image, and not partly outside?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2015-11-02 06:44:42 -0600

thdrksdfthmn gravatar image

updated 2015-11-02 07:46:57 -0600

What is your kernel like?

I would suggest you to use something like

cv::Mat myKernel((cv::Mat_< uchar >(5, 5) <<
                                 1, 1,  1,  1, 1,
                                 1, 1,  1,  1, 1,
                                 1, 1, 25, 1, 1,
                                 1, 1,  1,  1, 1,
                                 1, 1,  1,  1, 1));

and then do your convolution using filter2d

For simple operations, like blur, Gaussian blur, or median blur, there are specific functions where you just need to give the size of the kernel. There is also a bilateral filter. Take a look at this demo.

For more useful functions for convolution you can see this page

edit flag offensive delete link more

Comments

1

The kernel which i want to use is the contra harmonic filter described in the link given above. The size of the kernel should be interchangeable.. What is wrong with my implementation.

I saw filter2d, but it looks a bit closed.. Am I able to define the filter type I am going to use in someway or?

215 gravatar image215 ( 2015-11-02 07:01:48 -0600 )edit

How do i detect whether the kernel is outside the image, and how do only apply it if its inside the image... Like a real convolutional kernel would do.

215 gravatar image215 ( 2015-11-02 07:26:47 -0600 )edit
3
  • "What is wrong with my implementation?" - it will be gross slow, and you're reinventing filter2d, only worse.
  • "and how do only apply it if its inside the image ? " - again, use filter2D, and supply an appropriate borderType
berak gravatar imageberak ( 2015-11-02 07:54:49 -0600 )edit

The contra harmonic filter is simple to implement if the exponent is 0, but what if it is 2? How to implement a filter2d on it?

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-11-02 08:04:49 -0600 )edit

I think i am bit unsure on how to create a kernel based on filter description....

215 gravatar image215 ( 2015-11-02 09:59:02 -0600 )edit
1

that's not an image convolution but ratio of two convolution of image power Q and Q+1

LBerger gravatar imageLBerger ( 2015-11-02 14:46:03 -0600 )edit

So using multiply, you can do the power (with a for loop), but transform the image in float or int first (CV_32SCx or CV_32FCx), then apply the filter2d on each and then do the division?

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-11-03 03:35:42 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-11-02 06:32:02 -0600

Seen: 1,741 times

Last updated: Nov 02 '15