Reputation: 75
I have a bw image which has a black background and contains a number of white pixels (like stars in night sky). First, one of these pixels is chosen, and its coordinates are saved in a 1*2 matrix named FirstPixel. Then, all the other pixels are one by one chosen. Rows and Cols are two 1*numel(find(pic)) matrices, containing X and Y coordinates of these pixels, respectively.
I want to draw a line between the first chosen pixel and all the other pixels, which mostly looks like beams of light being emitted from the origin FirstPixel.
I have written some codes using plot
… but the answer was terribly wrong!
Like this:
pic = imread( 'interest points' );
figure, imshow ( pic );
hold on
plot([Rows (1,:)' Cols (1,:)'],[FirstPixel (1,1)' FirstPixel (1,2)'], 'r-');
any help?!
Thanks in advance :)
Upvotes: 1
Views: 784
Reputation: 489
The fact that Rows
represent the x coordinates in the question (and Cols
the y coordinates) can make the accepted answer a bit confusing.
I didn't know how to clarify this for future readers other than to make a new answer.
Here is a complete code example simulating the scenario in the question, but with rows
representing the y coordinates and cols
the x coordinates.
% replicate scenario (using 4 points)
I = uint8(zeros(256, 256));
I(124, 115) = 255;
I(139, 165) = 255;
I(12, 185) = 255;
I(124, 185) = 255;
n = numel(find(I));
[rows, cols] = ind2sub(size(I), find(I));
rows = rows';
cols = cols';
pixels = [rows; cols]';
firstPixel = pixels(1,:);
% show image and plot overlay
imshow(I);
hold on;
plot([cols; ones(1,n)*firstPixel(2)], [rows; ones(1,n)*firstPixel(1)], '-r');
Here is the resulting plot.
I hope this helps future visitors who have this problem.
Upvotes: 0
Reputation: 86
The two arguments of 'plot' are X and Y values, not first and second points as you did. Also you need to give numel times your firstpixel, try with this :
NUMEL = numel(find(pic));
hold on
plot([Rows; ones(1,NUMEL)*FirstPixel(1)],[Cols; ones(1,NUMEL)*FirstPixel(2)],'-k');
Upvotes: 1