Matlab在openCV中进行了讨论

时间:2015-11-24 06:22:15

标签: c++ matlab opencv

我抱怨你可以帮助我。 我目前正在用opencv将一些matlab代码翻译成c ++,但它不起作用......

现在我不确定,如果由于我的翻译错误或我错过了某些内容。

Matlab的:

R=rmin:rmax;
count=size(R,2);
for k=1:count
    [L(k)]=lineint(I,C,R(k),n,part);
    if L(k)==0
        L(k)=[];
        break;
    end
end

D=diff(L);
D=[0 D];

if strcmp(sigma,'inf')==1
    f=ones(1,7)/7;
else
    f=fspecial('gaussian',[1,5],sigma);
end

blur=convn(D,f,'same');%Smooths the D vecor by 1-D convolution 
blur=abs(blur);
[b,i]=max(blur);
r=R(i);
b=blur(i);

的OpenCV:

std::vector<double> L;
std::vector<int> radii;
std::vector<void*> result;
cv::Mat blur;
cv::Mat kernel;

for (int i = rmin; i <= rmax; i++) {
    double tmp = lineint(I, C, i, n, part);
    if (tmp != 0) {
        L.push_back(tmp);
        radii.push_back(i);
    }
    else
        break;
}

std::vector<double> D;
D.push_back(0);
for (int i = 1; i < L.size(); i++) {
    double tmp = L.at(i-1) - L.at(i);
    D.push_back(tmp >= 0 ? tmp : -1 * tmp);
}

//inf case
if (sigma < -1) {
    kernel = cv::Mat::ones(Size(1, 7), CV_32FC1) / 7;
}
else {
    kernel = cv::getGaussianKernel(5, sigma, CV_32FC1);
}

//flip vertical and horizontal
//rearange anchor
//http://docs.opencv.org/2.4/modules/imgproc/doc/filtering.html#filter2d
cv::flip(kernel, kernel, -1);
cv::Point anchor = cv::Point(-1, -1);
anchor = cv::Point(kernel.cols - anchor.x - 1, kernel.rows - anchor.y - 1);
cout << anchor << "|" << kernel << endl;
cv::filter2D(D, blur, -1, kernel, anchor, 0.0f, BORDER_DEFAULT);
blur = cv::abs(blur);

double min, max;
cv::Point min_loc, max_loc;
cv::minMaxLoc(blur, &min, &max, &min_loc, &max_loc);
int radius = (rmin + max_loc.y);

result.push_back(&max);
result.push_back(&radius); 

谢谢!

修改: 抱歉错过了错误消息:

OpenCV Error: Assertion failed (anchor.inside(Rect(0, 0, ksize.width,    ksize.height))) in cv::normalizeAnchor, file c:\builds\master_packslave-win64-vc12-shared\opencv\modules\imgproc\src\filterengine.hpp, line 363

当我在filter2D上应用过滤器

时会发生这种情况

EDIT2 : 如果我删除卷积更新(删除翻转和锚的重新排列)

,它会起作用

1 个答案:

答案 0 :(得分:0)

好的我想我修好了(实际上错误消失了,我不确定结果是否相同)

我使用了此处提供的功能:http://blog.timmlinder.com/2011/07/opencv-equivalent-to-matlabs-conv2-function/

void conv2(cv::Mat &img, cv::Mat& kernel, int type, cv::Mat& dest) {
    cv::Mat source = img;
    if (CONVOLUTION_FULL == type) {
        source = cv::Mat();
        const int additionalRows = kernel.rows - 1, additionalCols = kernel.cols - 1;
        cv::copyMakeBorder(img, source, (additionalRows + 1) / 2, additionalRows / 2, (additionalCols + 1) / 2, additionalCols / 2, BORDER_CONSTANT, Scalar(0));
    }

    cv::Point anchor(kernel.cols - kernel.cols / 2 - 1, kernel.rows - kernel.rows / 2 - 1);
    int borderMode = BORDER_CONSTANT;
    cv::flip(kernel, kernel, -1);
    cv::filter2D(source, dest, img.depth(), kernel , anchor, 0, borderMode);

    if (CONVOLUTION_VALID == type) {
        dest = dest.colRange((kernel.cols - 1) / 2, dest.cols - kernel.cols / 2)
        .rowRange((kernel.rows - 1) / 2, dest.rows - kernel.rows / 2);
    }
}

他基本上以不同的方式对准锚。我会看看结果是否足够