Code
%%// 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
Associated function
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;
Output with cluster gradient values for sample image
gradient1 =
1.6643 1.9626 2.0503 2.0503
Please note that the clusters are ordered according to the column-major indexing as used in MATLAB.