I'm working on image processing and need to know an equivalent of conv2 of Matlab in c++ OpenCV.
I found this link, but it doesn't suit my requirements.
The problem I'm facing is that I need to convolute a Mat image with a 2-D double array, which is not the case given in the link above.
The matlab code is:
img = conv2(img1,Mx,'same')
where
Mx = {
{0, 0, 0, 0, 0, 0} ,
{0, -0.0003, -0.0035, 0, 0.0035, 0.0003} ,
{0, -0.0090, -0.0903, 0, 0.0903, 0.0090} ,
{0, -0.0229, -0.2292, 0, 0.2292, 0.0229} ,
{0, -0.0090, -0.0903, 0, 0.0903, 0.0090} ,
{0, -0.0003, -0.0035, 0, 0.0035, 0.0003}
};
Thank you.
Solution
Use OpenCV's filter2D function.
Code example
//initializes matrix
cv::Mat mat = cv::Mat::ones(50, 50, CV_32F);
//initializes kernel
float Mx[36] = { 0, 0, 0, 0, 0, 0 ,
0, -0.0003, -0.0035, 0, 0.0035, 0.0003 ,
0, -0.0090, -0.0903, 0, 0.0903, 0.0090 ,
0, -0.0229, -0.2292, 0, 0.2292, 0.0229 ,
0, -0.0090, -0.0903, 0, 0.0903, 0.0090 ,
0, -0.0003, -0.0035, 0, 0.0035, 0.0003
};
cv::Mat kernel(6, 6, CV_32F, Mx);
//convolove
cv::Mat dst;
cv::filter2D(mat, dst, mat.depth(), kernel);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With