s900n
s900n

Reputation: 3375

C++ OpenCV: What is the easiest way to apply 2-D convolution

I have a Kernel filter that I generated and I want to apply it to my image but I could not get a right result by doing this: Actually I can use a different method as well since I am not to familiar with opencv I need help thanks.

channel[c] is the read image;

int size = 5; // Gaussian filter box side size
double gauss[5][5];

int sidestp = (size - 1) / 2;

// I have a function to generate the gaussiankernel filter

float sum = 0;
for (int x = 1; x < channels[c].cols - 1; x++){
    for (int y = 1; y < channels[c].rows - 1; y++){
        for (int i = -size; i <= size; i++){
            for (int j = -sidestp; j <= sidestp; j++){
                sum = sum + gauss[i + sidestp][j + sidestp] * channels[c].at<uchar>(x - i, y - j);
            }
        }
        result.at<uchar>(y, x) = sum;
    }
}

Upvotes: 1

Views: 1641

Answers (2)

bikz05
bikz05

Reputation: 1605

Just to add to the previous answer, since you are performing Gaussian blur, you can use the OpenCV GaussianBlur (Check here). Unlike filter2D, you can use the standard deviations as input parameter.

Upvotes: 2

a-Jays
a-Jays

Reputation: 1212

OpenCV has an inbuilt function filter2D that does this convolution for you. You need to provide your source and destination images, along with the custom kernel (as a Mat), and a few more arguments. See this if it still bothers you.

Upvotes: 2

Related Questions