# How to create lines and calculate their lenghts

2 views (last 30 days)
Jórdan Venâncio Leite on 17 Oct 2020
Commented: Rik on 30 Nov 2020
I need to create lines in the filtered image bellow (already filtered image of a bubble seeping into a tube (cropped image)) in a similar way to the orange lines (drawn randomly as an example using the software paint) already in the example figure (10 lines is fine) so that I can calculate the lengths of these lines and then make an average. Any idea how i could do this?
cropped filtered example clc;
clear
close all;
figure, imshow(cropped);
gray = rgb2gray(cropped);
figure, imshow(gray);
thresh = im2bw(gray, 0.7);
figure, imshow(thresh);
remove = bwareaopen(thresh,13200);
figure, imshow(remove);
se = strel('line',400,0);
closingOperation = imclose(remove,se);
figure, imshow(closingOperation);
OriginalLine = closingOperation(1 , :);
OriginalLine2 = closingOperation(end , :);
closingOperation(1, :) = true;
closingOperation(end, :) = true;
filtered = imfill(closingOperation, 'holes');
closingOperation(end,:) = false;
closingOperation(1,:) = false;
filtered(1, :) = OriginalLine;
filtered(end, :) = OriginalLine2;
figure, imshow(filtered);

Show 1 older comment
Jórdan Venâncio Leite on 29 Oct 2020
Jórdan Venâncio Leite on 28 Nov 2020
Jórdan Venâncio Leite on 30 Nov 2020
It worked!!

Image Analyst on 28 Nov 2020
Jordan:
Why not do it for ALL widths (every single line of the blob), rather than just 10? Just simply do
verticalProfile = sum(binaryImage, 2); % A list of all possible widths - hundreds of them.
meanWidth = mean(verticalProfile);
If you really want exactly 10 lines dividing the blob into 11 sections, do this (untested):
[r, c] = find(binaryImage);
topRow = min(r)
bottomRow = max(r)
rows = linspace(bottomRow, topRow, 12); % 12 if you include the top row and bottom row.
% Don't include very top or very bottom
rows = rows(2:end-1) % Only 10 now.
for k = 1 : length(rows)
row = rows(k);
col1 = find(binaryImage(row, :), 1, 'first');
col2 = find(binaryImage(row, :), 1, 'last');
width(k) = col2 - col1; % Add 1 if you want, depends on your definition of width.
end

Jórdan Venâncio Leite on 30 Nov 2020
I didnt try it yet
Image Analyst on 30 Nov 2020
But you actually did because Rik said to use sum() and find() but didn't give any code, and you said it worked. I also said that and additionally gave code for how to find the location of the "orange lines". So if you coded up Rik's suggestion, you actually did something like what I gave above.
Rik on 30 Nov 2020
And that is the reason why I didn't post my suggestion as an answer, as this answer gives a specific example of how to implement my suggestion.