我的第一篇帖子=) 我在MATLAB中遇到了一个看似简单的计算问题。 我有一个1000x1000的0和1的矩阵。 1s在矩阵的对角线上聚集成粗线,我需要测量这些线簇的梯度。 (从SW到NE的粗白线)。 到目前为止我所做的是在每个簇上放置一个标尺并提取线的点。然而,这不是一个解决方案,因为我有2000个矩阵来分析。
阅读渐变 -
问题:
提前非常感谢。如果我的问题不清楚,请告诉我=)
答案 0 :(得分:1)
<强>代码强>
%%// Select approach
%%// 1. Gradient values for all clusters
%%// 2. One dominant gradient value for one image
approach_id = 1;
%%// Threshold to the number of pixels that a blob must have
%%// to be declared as a cluster
thresh = 850;
%%// Image scaling factor
img_scale = 0.2; %%// 0.2 seemed to work for the sample
img = imread(image_filenpath);
bw1 = im2bw(img,0.3); %%// 0.3 as threshold-level worked for sample image
bw2 = medfilt2(bw1,[5 5]); %%// 5x5 as denoising window worked
[L, num] = bwlabel(bw2, 8);
counts = sum(bsxfun(@eq,L(:),1:num));
switch approach_id
case 1
count1 = 1;
for k = 1:num
if counts(k)>thresh
bw5 = imresize(L==k,img_scale);
gradient1(count1) = gradval(bw5);
count1 = count1+1;
end
end
case 2
bw4 = false(size(bw1));
for k = 1:num
if counts(k)>thresh
bw4 = bw4 | L==k;
end
end
%%// At this point we have a cleaned-up binary image of the input
bw5 = imresize(bw4,img_scale);
gradient1 = gradval(bw5);
end
%%// gradient1 is what you need
相关功能
function gradient_value = gradval(BW)
angles = 45:-1:0;
for iter = 1:numel(angles)
BWr = imrotate(BW,angles(iter));
t1(iter) = max(sum(BWr,1));
end
[~,ind] = max(t1);
gradient_value = tand(90 - angles(ind));
return;
使用样本图像的群集渐变值输出
gradient1 =
1.6643 1.9626 2.0503 2.0503
请注意,群集是根据MATLAB中使用的列主索引进行排序的。