我正在尝试使用C语言中的Opencv 3.0实现Active Contour Models算法 . 这个算法基于我为MatLab编写的脚本,并没有按预期工作 . 这两个图像显示了两种算法运行的结果 .
MatLab脚本:
和OpenCV一个:
在它们两个中,我对所有ACM参数使用相同的值,因此它们应该返回相同的东西,即白色圆形轮廓 . 我怀疑问题是我的图像能量函数,因为opencv和matlab中的梯度操作不一样 . 图像能量的matlab脚本是:
function [Eext] = get_eext(wl, we, wt, image)
%External Energy
[row,col] = size(image);
eline = image; %eline is simply the image intensities
[grady,gradx] = gradient(image);
eedge = -1 *(gradx .* gradx + grady .* grady);
%masks for taking various derivatives
m1 = [-1 1];
m2 = [-1;1];
m3 = [1 -2 1];
m4 = [1;-2;1];
m5 = [1 -1;-1 1];
cx = conv2(image,m1,'same');
cy = conv2(image,m2,'same');
cxx = conv2(image,m3,'same');
cyy = conv2(image,m4,'same');
cxy = conv2(image,m5,'same');
eterm = zeros(row, col);
for i = 1:row;
for j= 1:col;
% eterm as deined in Kass et al Snakes paper
eterm(i,j) = (cyy(i,j)*cx(i,j)*cx(i,j) -2 *cxy(i,j)*cx(i,j)...
*cy(i,j) + cxx(i,j)*cy(i,j)*cy(i,j))/((1+cx(i,j)*cx(i,j)...
+ cy(i,j)*cy(i,j))^1.5);
end;
end;
Eext = (wl*eline + we*eedge + wt*eterm);
在C中我的功能如下:
Mat get_eext(float wl, float we, float wt, Mat image){
Mat eline, gradx, grady, img_gray, eedge;
//bitdepth defined as CV_32F
image.convertTo(img_gray, bitdepth);
//Convolution Kernels
Mat m1, m2, m3, m4, m5;
m1 = (Mat_<float>(1, 2) << -1, 1);
m2 = (Mat_<float>(2, 1) << -1, 1);
m3 = (Mat_<float>(1, 3) << 1, -2, 1);
m4 = (Mat_<float>(3, 1) << 1, -2, 1);
m5 = (Mat_<float>(2, 2) << 1, -1, -1, 1);
//cvtColor(image, img_gray, CV_BGR2GRAY); <- Not required since image already in grayscale
img_gray.copyTo(eline);
Mat kernelx = (Mat_<float>(1, 3) << -0.5, 0, 0.5);
Mat kernely = (Mat_<float>(3, 1) << -0.5, 0, 0.5);
filter2D(img_gray, gradx, -1, kernelx);
filter2D(img_gray, grady, -1, kernely);
//Edge Energy
eedge = -1 * (gradx.mul(gradx) + grady.mul(grady));
//Termination Energy Convolution
Mat cx, cy, cxx, cyy, cxy, eterm, cxm1, den, cxcx, cxcxm1, cxcxcy, cxcycxy, cycycxx;
filter2D(img_gray, cx, bitdepth, m1);
filter2D(img_gray, cy, bitdepth, m2);
filter2D(img_gray, cxx, bitdepth, m3);
filter2D(img_gray, cyy, bitdepth, m4);
filter2D(img_gray, cxy, bitdepth, m5);
//element wise operations to find Eterm
cxcx = cx.mul(cx);
cxcx.convertTo(cxcxm1, -1, 1, 1);
den = cxcxm1 + cy.mul(cy);
cv::pow(den, 1.5, den);
cxcxcy = cxcx.mul(cy);
cxcycxy = cx.mul(cy);
cxcycxy = cxcycxy.mul(cxy);
cycycxx = cy.mul(cy);
cycycxx = cycycxx.mul(cxx);
eterm = (cxcxcy - 2 * cxcycxy + cycycxx);
cv::divide(eterm,den,eterm,-1);
//Image energy
Mat eext;
eext = wl*eline + we*eedge + wt*eterm;
return eext;}
有谁知道什么可能是错的?
1 回答
正如David Doria所说,经过一些修正后,这里是get_eext函数的最终版本 . 这个版本对我来说很好 .
希望能帮助到你!