opencv中有和matlab中conv2一样功能的函数吗?


我正在把一份matlab图像处理的代码翻译成C++代码,用的是opencv的库,现在有一个问题,matlab中的conv2函数在opencv中有没有对应的函数呢?我用的是cvFilter2D,不过结果貌似不对,下面是两个函数分别做卷积的结果:

opencv C++ 代码:

   
  CvMat * Aa = cvCreateMat(2, 2, CV_32FC1);
  
CvMat * Bb = cvCreateMat(2, 2, CV_32FC1);
CvMat * Cc = cvCreateMat(2, 2, CV_32FC1);
cvSetReal2D(Aa, 0, 0, 1);
cvSetReal2D(Aa, 0, 1, 2);
cvSetReal2D(Aa, 1, 0, 3);
cvSetReal2D(Aa, 1, 1, 4);
cvSetReal2D(Bb, 0, 0, 5);
cvSetReal2D(Bb, 0, 1, 5);
cvSetReal2D(Bb, 1, 0, 5);
cvSetReal2D(Bb, 1, 1, 5);
cvFilter2D(Aa, Cc, Bb);

产生的结果:矩阵 [20 30; 40 50]

MATLAB代码:

   
  >> A=[1 2; 3 4]
  
A =
1 2
3 4

>> B=[5 5; 5 5]
B =
5 5
5 5

>> conv2(A,B,'same')
ans =
50 30
35 20

怎么样用opencv的函数产生和conv2一样的功能呢?我要实现的是conv2(A,B,‘valid’)的功能。谢谢大家

opencv matlab

请叫我OOXX 12 years, 10 months ago

我自己写的答案如下:

   
  IplImage* cvImgConv2(IplImage* src,CvMat* kernel,ConvolutionType type)
  
{
IplImage* source;
IplImage* destinate;

if(CONVOLUTION_FULL == type)
{
const int additionalRows = kernel->rows-1;
const int additionalCols = kernel->cols-1;

source = cvCreateImage(cvSize(additionalCols+src->width,additionalRows+src->height),src->depth,src->nChannels);

cvCopyMakeBorder(src,source,cvPoint((additionalCols+1)/2,(additionalRows+1)/2),IPL_BORDER_CONSTANT);
}
else
{
source=cvCloneImage(src);
}

destinate=cvCloneImage(source);

/*CvMat* FlipKernel=cvCloneMat(kernel);
cvFlip(kernel,FlipKernel,-1);*/不知道该不该flip,暂时先不flip

cvFilter2D(source,destinate,kernel);

cvReleaseImage(&source);
//cvReleaseMat(&FlipKernel);

if(CONVOLUTION_VALID == type)
{
IplImage* dst=cvCreateImage(cvSize(src->width-kernel->width+1,src->height-kernel->height+1),src->depth,src->nChannels);

CvRect roi=cvRect(kernel->height/2,kernel->width/2,src->width-kernel->width+1,src->height-kernel->height+1);

dst=cvGetSubImage(destinate,roi);

cvReleaseImage(&destinate);

return dst;
}
else
{
return destinate;
}
}

兄贵都要死 answered 12 years, 10 months ago

Your Answer