Tracing Line from an Image
이전 댓글 표시
I have a 2D matrix that looks like the attached image when plotted using the imagesc function. I wonder if there is a simple method to trace a single line (1D data) that would follow the yellow curves going from the left to right. Thanks a lot for your help!

댓글 수: 2
Walter Roberson
2023년 2월 10일
Do you need multiple lines such as bright middle and the two orange bands around it? Or just the bright middle? Or do you need to trace lines of constant value (in which case, contour())?
yjin
2023년 2월 10일
채택된 답변
추가 답변 (1개)
n= 30 ; m= 40 ;
A = rand(n,m) ;
B = A ; B(B ~= max(B)) = 0 ;
index = find(B) ;
[q,r] = quorem(sym(index),sym(n)) ;
r(r == 0) = n ;
imagesc(A)
hold on
plot(1:m,r,"linewidth",3,"color","r")
as max(A) yields the maximum value of the array line-wise.
This example show in the case of random matrix A, but you have noisy data around a clearly identifiable value. Apply low-pass filter on data if it is noisy, or try approximating better than taking max value, as my method relies on too few information.
Just bear in mind it might not be the most efficient method at all.
EDIT : of course it doesn't work with multiple same values in columns. Using @DGM 's reply, here's an updated version (using your picture and not a random matrix) :
pict = imread("image.png") ;
graypict = rgb2ind(pict,parula(256)) ;
A = double(graypict) ;
A(1:35,:) = [] ; % trimming picture from bad data on top
B = A ; % making copy of array
[n,m] = size(A) ;
B(B ~= max(B)) = 0 ; % setting to zero all non-maximum values of picture
index = zeros(1,m) ;
for i = 1:m % finding indexes of all first and last non-zero values in picture
index_lasts(i) = find(B(:,i),1,'last') ;
index_frsts(i) = find(B(:,i),1,'first') ;
end
[ql,rl] = quorem(sym(index_lasts),sym(n)) ; % getting the n-related part of the index. q is the m-related.
[qf,rf] = quorem(sym(index_frsts),sym(n)) ;
rf(rf == 0) = n ; % setting as n all "0" indexes (non-valid for array idnexing)
rl(rl == 0) = n ;
%if you want mean of data acquired :
r_average = rf+rl/2 ;
%plotting results.
f = figure ;
f.Position = [50 50 1700 350];
imagesc(A)
hold on
plot(1:m,rf,"linewidth",2,"color","r")
plot(1:m,rl,"linewidth",2,"color","k")
Your data is quite noisy : consider applying on picture a finely selected convolution matrix to reduce local extremas from noise.
카테고리
도움말 센터 및 File Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


