【发布时间】:2021-02-20 20:18:25
【问题描述】:
我正在尝试传递 matlab 中用于 c ++ 的代码,但它给出了分段错误(核心转储),有人可以帮助我吗?
MATLAB 中的代码
function strip = strips(strip,n,number_pixel)
se = strel('square', 3);
strip=imdilate(strip, se);
strip = imfill(strip);
[m1,n1] = size(strip);
vet=0;
for j=1:n1
for i=1:m1
if strip(i,j)==0
vet=vet+1;
end
end
aux(j)=vet;
vet=0;
end
limite=uint8(number_pixel/4);
for i=1:n1
if aux(i) < limite
strip(:, i)=1;
end
if aux(i) >= limite
strip(:, i)=0;
end
end
end
C++ 代码
Mat strips(Mat strip, int n, int number_pixel){
int vet = 0;
int limite = (number_pixel/4);
int v[strip.cols];
dilate(strip, strip, getStructuringElement(MORPH_ELLIPSE, Size(3,3)));
strip = imfill(strip);
int n1 = strip.cols;
int m1 = strip.rows;
for (int j = 0; j < n1; j++){
for (int i = 0; i < m1; i++){
if(strip.at<uchar>(i, j) == 0){
vet += 1;
}
}
v[j] = vet;
vet = 0;
}
for (int i = 0; i < n1; i++){
int j;
if (v[i] < limite){
strip.at<uchar>(i, j) = 1;
}
if (v[i] >= limite){
strip.at<uchar>(i, j) = 0;
}
}
}
我已经将函数实现为imfill,我认为我的错误在循环中,但我找不到
【问题讨论】:
-
我不知道其中有什么作用,但我看到在第二个循环中使用了未初始化的变量 j。不知道跟这个有没有关系
-
我有一个数组,我只想改变 i,保持 j 的值