Interactive pixel extraction from an image along custom line
42 次查看(过去 30 天)
显示 更早的评论
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/310937/image.jpeg)
0 个评论
采纳的回答
Image Analyst
2020-6-6
Try improfile()
or drawline() with a callback function
5 个评论
Image Analyst
2020-6-7
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.
2021-7-2
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')
更多回答(1 个)
Julianna Mather
2020-6-8
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.)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!