Question

My first post here =) Im stuck with a seemingly simple computation problem in MATLAB. I have a 1000x1000 matrix of 0s and 1s. The 1s are clustered in a thick lines diagonally across the matrix and I need to measure the gradient of these line clusters. (The thick white lines that runs SW to NE). What i did so far was placing a ruler on each cluster and extracting the points of the lines. This however isnt a solution as i have 2000 matrices to analyze.

enter image description here

Reading the gradients -

enter image description here

Problems:

  • I cant fit a gradient as there are more than one line cluster.
  • I tried using imclose to remove the stray points but it doesnt help me isolate each clusters
  • I tried using edge detection and Hough transforms but neither of them helps me isolates the clusters.

Thanks so much in advance. Do let me know if my problem isnt clear =)

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top