Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conv2 in opencv

Tags:

c++

opencv

matlab

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.

like image 330
RAM Avatar asked Jan 03 '23 21:01

RAM


1 Answers

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);
like image 136
ibezito Avatar answered Jan 17 '23 03:01

ibezito