在图像MATLAB中跟踪一条线

时间:2015-04-15 06:06:41

标签: image matlab image-processing

我正在整理一个用示波器输出来计算一些东西的程序,但是由于程序现在可以工作,我只需将图像导入MATLAB,然后使用ginput在生成的曲线上找到各个区域的坐标。

有没有办法可以拍摄这张图片:

sine wave] (http://imgur.com/IlSDDLK) ![sine wave

并且沿着亮绿色曲线自动跟踪ginput或类似物并将x,y坐标存储到单独的数组中(可能通过能够区分曲线的颜色和背景颜色)?这样我就可以使用图片中曲线的x,y坐标的实际绘图,而不需要在数据分析中实际使用图像。

我能得到的最接近的就是使用[x,y]=ginput沿曲线压缩鼠标按钮并生成一个巨大的数组,但我的手指需要休息!

谢谢!

1 个答案:

答案 0 :(得分:11)

看看这个

img = imread('http://i.stack.imgur.com/3GH1x.jpg'); %// read the image
bw = img(:,:,2) > 128; %// pick only green points (2nd RGB channel)
bw(275:end,:) = false; %// discard the lower flat line
[yy xx]=find(bw); %// get the x-y coordinates of green pixels

现在你可以绘制积分:

figure;plot(xx,yy, '.');

结果 enter image description here

如果您对线条较粗(即每个x的多个y值)感到困扰,您可以简单地取平均值

uy = accumarray( xx, yy, [], @mean );
ux = 1:max(xx);

可视化线

figure;imshow( img );hold on; plot(ux,uy,'r','LineWidth',1.5);

enter image description here


如果您也在网格之后,那么

[gy gx] = find( max(img,[],3) < 60); %// get the darkest points

要确定我们寻找的网格点x,以便许多网格点gy具有相同的gx

nx = hist(gx,1:size(img,2));  %// count how many gx per x location
gxx = find(nx > 100 ); %// only those with more than 100 are grid X

同样的y:

ny = hist(gy,1:334); 
gyy = find(ny > 100 );

删除重复项:

gxx( diff([0 gxx]) == 1 ) = [];
gyy( diff([0 gyy]) == 1 ) = [];

创建网格点

[GX GY] = meshgrid(gxx, gyy);

现在全貌:

figure('Name','I MUST award Shai a FAT Bounty for this');
imshow( img );hold on;
plot(ux,uy,'r','LineWidth',1.5); %// the curve
scatter( GX(:), GY(:), 150, '+c'); %// the grid

enter image description here