get the matrix values under a line

Hi,
I have a matrix of data (120x90) that I plot and then I draw a diagonal line by doing something like:
plot([x1,x2],[Y1,Y2],'g.');
I need to be abe to determine all the matrix values under the line. Any idea about how to perform this efficiently?

Antworten (3)

Image Analyst
Image Analyst am 12 Okt. 2015

5 Stimmen

If you have the Image Processing Toolbox, you can get this information from the function improfile().
improfile(I,xi,yi,n) returns pixel intensity values, where n specifies the number of points to include.
c = improfile(___) returns the intensity values in c, an n-by-1 vector, if the input is a grayscale intensity image, or an n-by-1-by-3 array if the input is an RGB image.
[cx,cy,c] = improfile(I,xi,yi,n) additionally returns the spatial coordinates of the pixels, cx and cy, of length n.

3 Kommentare

Armindo
Armindo am 13 Okt. 2015
Thank you very much for all the help. Problem solved, I have used the improfile function which give good results.
Image Analyst
Image Analyst am 13 Okt. 2015
You're welcome. Can you go ahead and "Accept this answer" then? Thanks!
Rahand Dalshad
Rahand Dalshad am 2 Mai 2021
Dear Image Analyst,
I want to thank you personally for all your help, suggestions and solutions to Matlab issues and questions. Lately, I am doing much images post-processing of optical obtained images and data. Wherever I stuck and google for a proper solution, I find the best and easiest solution from you.
Thank you again and keep up the good work
Regards form Germany
Rahand

Melden Sie sich an, um zu kommentieren.

Star Strider
Star Strider am 11 Okt. 2015

2 Stimmen

You would have to define every (x,y) pair along the line, in terms of the row and column values of the matrix coordinates. That will likely be straightforward when you derive an equation for the line.
I am not certain what information you want from the matrix, but you might also consider using the interp2 function if you want to interpolate the values of the matrix.

4 Kommentare

Armindo
Armindo am 12 Okt. 2015
Thank you for the help. The problem seems simple but is not. The matrix is not square therefore some of the points of the line dont touch exactlly in 1 square but can pass in the midle of two. Moreover to maitain the image scaled I am using different X and Y intervals. Therefore the pixels are not squares.
I try to perform this: y = mx+b
calculate slope (m) calculate the b calculate y = mini:interv:maxi; % maitaining the right interval (0.2539063) and then calculate the x = (y-b)/m;
However x will not have its right interval. If use the right interval (i.e. 0.1692708) I get different size between y and x.
My pleasure.
None of that information was in your original Question!
I may be missing something, but in this instance the geometry of the pixels (square or rectangular) seems to me to be unimportant. You are using ‘units’ of matrix indices, that by definition are integers. No scaling is required.
You would have to determine the intersections of your line with a grid of your coordinates. The code in my Answer to this post might be relevant.
This is an updated and corrected version of that code. You will need to adapt it to your problem:
x = 0:5; % X-range
y = 0:25; % Y-range
lxmb = @(x,mb) mb(1).*x + mb(2); % Line equation: y = m*x+b
m = -5; % Slope (or slope array)
b = 15; % Intercept (or intercept array)
mb = [m b]; % Matrix of [slope intercept] values
L1 = lxmb(x,mb); % Calculate Line #1 = y(x,m,b)
hix = @(y,mb) [(y-mb(2))./mb(1); y]; % Calculate horizontal intercepts
vix = @(x,mb) [x; lxmb(x,mb)]; % Calculate vertical intercepts
hrz = hix(x(2:end),mb)'; % [X Y] Matrix of horizontal intercepts
vrt = vix(y(1:6),mb)'; % [X Y] Matrix of vertical intercepts
hvix = [hrz; vrt]; % Concatanated ‘hrz’ and ‘vrt’ arrays
exbd = find( (hvix(:,2) < 0) | (hvix(:,2) > 25) );
hvix(exbd,:) = [];
srtd = unique(hvix,'rows'); % Remove repeats and sort ascending by ‘x’
exL1 = find((L1 < 0) | (L1 > 25)); % Find ‘y’ values for ‘L1’ off grid
xp = x; % Create plotting x-vector for L1
xp(exL1) = []; % Eliminate out-of-bounds ‘y’ values from ‘x’
L1(exL1) = []; % Eliminate out-of-bounds ‘y’ values from ‘Li’
figure(1) % Draw grids & plot lines
plot(repmat(x,2,length(x)), [0 length(y)-1]) % Vertical gridlines
hold on
plot([0 length(x)-1], repmat(y,2,length(y))) % Horizontal gridlines
plot(xp, L1) % Plot more lines here (additional ‘plot’ statements)
hold off
axis equal
Armindo
Armindo am 12 Okt. 2015
Your are completly right. I did not explain myself properly and I apologize for that. I belive that your suggestion (about determine the intersections of the line with a grid of coordinates) would work perfectly I will give it a try. Thank you very much for the help and code.
Star Strider
Star Strider am 12 Okt. 2015
My pleasure.
I tested that code thoroughly so it should be robust. I’ll do my best to help you adapt it if necessary, but I wrote it years ago (and haven’t used it much since), so I’d have to remember what I did.

Melden Sie sich an, um zu kommentieren.

William Chamberlain
William Chamberlain am 17 Okt. 2018

0 Stimmen

1 Kommentar

Image Analyst
Image Analyst am 17 Okt. 2018
Using improfile() with the 'nearest' option would also do that, if he wanted to go to the nearest pixel instead of doing subpixel interpolation.

Melden Sie sich an, um zu kommentieren.

Gefragt:

am 11 Okt. 2015

Community Treasure Hunt

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

Start Hunting!

Translated by