Interactive pixel extraction from an image along custom line
27 views (last 30 days)
Show older comments
The following code extracts pixel values for the first column of an image.
IMt = imread('test.jpg');
% convert to double format
IMtd=double(IMt);
% isolate first trace
trace=(IMtd(:,1));
What I need to do instead is extract the pixel values along an arbitrary line (see red lines in below image). In other words, along a direction which is diagonal to rows and columns.
Is there a tool in Matlab available where one can plot an image and then interactively draw a line along which the values will be extracted?
If I use...
imshow(IMt)
datacursormode on
...the pixel values are interactively displayed when I hove over the image. There just needs to be a way to write them into an array at the same time.
0 Comments
Accepted Answer
Image Analyst
on 6 Jun 2020
Try improfile()
or drawline() with a callback function
5 Comments
Image Analyst
on 7 Jun 2020
You never know before you draw the line but once you draw it, you know, and that's what I computed and passed in.
You can rate it one star in the help and it will bring up a form that you can put your comment.
Z.S.
on 2 Jul 2021
I think the variables linestart and lineend are miss-positioning the x and y coordinates of the end points for using improfile. According to the help page of improfile, the format should be improfile(I,x,y). Then the code should be something like:
I = imread('liftingbody.png');
imshow(I);
roi = drawline('Color','r');
% now draw line interactively
%
% Read out the X and Y coordinates of the start and end points
line_xs = [roi.Position(1,1), roi.Position(2,1)]
line_ys = [roi.Position(1,2), roi.Position(2,2)];
% extract pixel values along line
pixvals = improfile(I,line_xs,line_ys);
figure
plot(pixvals,1:length(pixvals),'k'), axis tight, title('pixel values')
xlabel('pixel greyscale values')
ylabel('relative position from linestart (0) to lineend')
More Answers (1)
Julianna Mather
on 8 Jun 2020
I've been thinking about how to frame the enhancement request for the functionality you requested, and I have some questions.
I'm curious to know what your ultimate goal is and what exactly you want to happen along the diagonals.
Suppose you have a diagonal line that crosses through a pixel just a tiny, tiny bit, should it get counted? Or perhaps it would be appropriate to determine the number of rows and columns the diagnol spans and then use hypot to figure out the number of pixels to sample uniformly along the line. (You can do that right now with improfile now, of course, for non-interactive syntaxes.)
So all of this gets me to the first question: What is your ultimate goal of having improfile return more pixel sites? How will you use that increased resolution?
(BTW, I'm not critiquing your request, I just want to make sure that we get a solution that will satisfy your use case.)
1 Comment
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!