![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/173819/image.jpeg)
How to get the pixels intersected with a vector line?
조회 수: 15 (최근 30일)
이전 댓글 표시
I would like to get the image pixels which intersect with a vector line . As shown in the figure below, I would like to get the yellow pixels; the starting coordinates (x1,y1) and ending coordinates (x2,y2) of the vector line are known. Could anybody help me to solve this question? I used matlab sometimes but not well.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/158709/image.jpeg)
댓글 수: 0
답변 (3개)
Image Analyst
2016년 3월 24일
You can use linspace and round the coordinates. Be sure to have enough points so that you don't skip any pixels. But when you do that you might have duplicate pixels that you have to remove. Here's the demo:
m = zeros(11,7)
imshow(m, 'InitialMagnification', 1600);
axis on;
axis image;
grid on;
x1 = 1;
y1 = 7;
x2 = 7;
y2 = 5;
% Create a line from point 1 to point 2
spacing = 0.4;
numSamples = ceil(sqrt((x2-x1)^2+(y2-y1)^2) / 0.4)
x = linspace(x1, x2, numSamples)
y = linspace(y1, y2, numSamples)
xy = round([x',y'])
dxy = abs(diff(xy, 1))
duplicateRows = [0; sum(dxy, 2) == 0]
% Now for the answer:
finalxy = xy(~duplicateRows,:)
finalx = finalxy(:, 1);
finaly = finalxy(:, 2);
% Plot the points
hold on;
plot(finalx, finaly, 'y*');
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/173819/image.jpeg)
댓글 수: 0
Reza Teimoori
2021년 3월 3일
If you are looking for an accurate and fast method you have to employ a so-called ray-tracing approach such as this one. Most of them try to find the intersection of the line with planes (rather than pixels). This way you would be reducing your problem's dimensionality from
to
in 2D and from
to
in 3D.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/537506/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/537511/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/537516/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/537521/image.png)
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!