Intersection between lines and traced shape

7 ビュー (過去 30 日間)
Kevin WIlleford
Kevin WIlleford 2019 年 7 月 15 日
回答済み: KSSV 2019 年 7 月 16 日
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
end
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.

回答 (1 件)

KSSV
KSSV 2019 年 7 月 16 日

カテゴリ

Help Center および File ExchangeComputational Geometry についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by