# How I can I find the minimum and maximum distance between two freehand lines on a dicom image?

25 views (last 30 days)
Andrew Sprague on 21 Jan 2020 at 2:41
Commented: Andrew Sprague on 21 Jan 2020 at 16:04
I am importing dicom images of tendons and attempting to find the minimum and maximum thickness by tracing the superficial and deep border of the tendon. However, the problem I am encountering is that the two borders of the tendon are not perfectly linear and/or parallel. I've included an annotated version of my code below to illustrate my current work-around, which obtains a minimum and maximum thickness value in the y direction, but does not account for the curve in the tendon. Is there a more efficient and accurate way to analyze this?
Here is an example image of a tendon with the superficial and deep borders traced.
Here is the current code that I'm using and a deidentified example dicom file: Example Dicom
[file,path] = uigetfile('*.dcm'); %Open file
loc = fullfile(path, file); %Save file location
info = dicominfo(loc); %Retrieve dicom info
figure; %Create figure for displaying image
imshow(image, []); %Display image
pixel_length = info.SequenceOfUltrasoundRegions.Item_1.PhysicalDeltaY; %Store the pixel length from the dicom file info
%Draw the superficial border of the tendon
superficial = drawfreehand('Closed', false, 'FaceAlpha', 0);
%Draw deep border
deep = drawfreehand('Closed', false, 'FaceAlpha', 0);
%Get X and Y Coordinates of superficial and deep borders
pos_super = superficial.Position;
pos_deep = deep.Position;
%The density of points was too low so I interpolated the data to increase the point density
pos_super_x = interp(pos_super(:,1), 10);
pos_super_y = interp(pos_super(:,2), 10);
pos_deep_x = interp(pos_deep(:,1), 10);
pos_deep_y = interp(pos_deep(:,2), 10);
%Recombine the interpolated points
pos_super_final = horzcat(pos_super_x, pos_super_y);
pos_deep_final = horzcat(pos_deep_x, pos_deep_y);
%Round X values to make them comparable between vectors
pos_super_final(:,1) = round(pos_super_final(:,1));
pos_deep_final(:,1) = round(pos_deep_final(:,1));
%Find common x elements between superficial and deep lines (C)
%and the index of those points (i_super and i_deep)
[C, i_super, i_deep] = intersect(pos_super_final(:,1), pos_deep_final(:,1));
%Index Y-values for i_super and i_deep
super_final = pos_super_final(i_super, :);
deep_final = pos_deep_final(i_deep, :);
%Calculate thick along length of tendon in pixels
thick_pix = abs(deep_final(:,2) - super_final(:,2));
%Convert thickness to cm
thick_cm = pixel_length*thick_pix
%Find min and max values and their index
[Max, I_Max] = max(thick_cm)
[Min, I_Min] = min(thick_cm)
%Find location of max and min thickness
max_super = super_final(I_Max, :)
max_deep = deep_final(I_Max, :)
min_super = super_final(I_Min, :)
min_deep = deep_final(I_Min, :)
%Plot min and max thickness
line([max_super(1), max_deep(1)], [max_super(2), max_deep(2)])
line([min_super(1), min_deep(1)], [min_super(2), min_deep(2)])

Image Analyst on 21 Jan 2020 at 2:57
You need to get the distances for each point, then take the min and max. Let's say you have two lines with coordinates x1, and y1, and for line 2 x2 and y2. Get them if you don't have them, but you probably have them I'm just not sure which variables are which in your code. Then compute the distances from one line to each point in the other line (you'll have a bunch of distances) and take the smallest distance. This will be the closest one, which is not necessarily in the same column and may be at a slight angle.
minDistances = zeros(1, length(x1));
for k = 1 : length(x1)
% For each point in line 1, compute distances to all points in line 2.
distances = sqrt((x1(k) - x2) .^ 2 + (y1(k) - y2) .^ 2);
minDistances(k) = min(distances);
end
% Now find the max of the min distances.
maxDistance = max(minDistances);
You need to find the max of the min distances and NOT the max of the max distances because that would obviously just be the left point of the top line to the right point of the bottom line, which is definitely NOT what you want of course.

Andrew Sprague on 21 Jan 2020 at 12:54
Thank you for your quick response! This is exactly the explanation I was looking for. I had seen others discuss the distance formula but I was stuggling with how I would find the max thickness that was not comparing the top left to bottom right point.
Image Analyst on 21 Jan 2020 at 14:26
But if your endpoints are not roughly parallel (don't end roughly at the same location), then it might still find the two points at either the far right opening or far left opening. So you might want to check if that is the case and take special precautions if the largest distance includes the "1" or "end" indexes of x1 or x2.
Andrew Sprague on 21 Jan 2020 at 16:04
I was just working my way through that just now. My plan was to compare the min and max x coordinates for both lines and then subset the data so that they both have roughly the same end-points in both x directions. Once again, really appreciate your help. I spent an entire afternoon trying to problem solve my way to your answer.