MATLAB Answers

Intersection between lines and traced shape

6 views (last 30 days)
Kevin WIlleford
Kevin WIlleford on 15 Jul 2019
Answered: KSSV on 16 Jul 2019
I am trying to implement an algorithm to find a single intersection point between known lines and a custom traced shape. The goal is to record the distance between the origin and the point of intersection for each angle. The shape was originally traced in photoshop, and was converted into two-dimensional coordinates through extracting the locations which contained non-zero values for each pixel.
When using a code which looks for the intersection for each line, multiple intersections are found because the 'equation' for the custom shape contains non-unique X and Y coordinates at many locations. Additionally, the found intersections depend on whether the 'equation' for the custom shape is sorted by the column or row indices. I am stuck...
The code below is the algorithm I currently have. The blue channel contains the user-defined origin. The red channel contains the user-defined shape. I draw lines with known multiple angles and look for the intersection between them and the shape.
%% starting to play with image processing
proc1 = imread('proc1.png');
B = proc1(:, :, 3);
bLinIdx = find(B > 1);
[rowsB, colsB] = ind2sub(size(B), bLinIdx);
p1c = mean(colsB); p1r = mean(rowsB);
R = proc1(:, :, 1);
rLinIdx = find(R > 1);
[rowsR, colsR] = ind2sub(size(R), rLinIdx);
rSubIdx = [rowsR, colsR]; rSubIdx = sortrows(rSubIdx, 1);
rowsR = rSubIdx(:, 1); colsR = rSubIdx(:, 2);
angles = linspace(-70, 70, 281);
cMap = viridis(length(angles));
numBasis = 1000;
for iA = 1 : length(angles)
% line 1
m1 = tand(angles(iA));
x1 = linspace(p1c, 300, numBasis);
y1 = m1 .* (x1 - x1(1)) + p1r;
L1 = [x1; y1];
% line 2
L2 = [colsR'; rowsR'];
% intersection
P = InterX(L1, L2);
% plotting
% plot(L1(1, :), L1(2, :))
% hold on
% plot(L2(1, :), L2(2, :), 'Color', 'k', 'LineWidth', 2);
p2c = mean(P(1, :));
p2r = mean(P(2, :));
scatter(p2c, p2r, 50, cMap(iA, :), 'filled')
hold on
The viridis color map plots are the found intersection points using the above algorithm. The black dots are the non-zero data points in the red channel.
I tried using a thinner line, and it does help a touch, but the same issues still persist so I am trying to address them first.


Sign in to comment.

Sign in to answer this question.