简体   繁体   English

跟踪图像中的一条线 MATLAB

[英]Trace a line in an image MATLAB

I am putting together a program to calculate some stuff with oscilloscope outputs, but as the program works now, I just import the image into MATLAB and then use ginput to find the coordinates of various regions on the generated curve.我正在编写一个程序来计算一些带有示波器输出的东西,但是由于程序现在可以工作,我只是将图像导入到 MATLAB 中,然后使用 ginput 在生成的曲线上找到各个区域的坐标。

Is there a way that I can take, say, this image:有没有一种方法可以让我拍摄这张图片:

正弦波] (http://imgur.com/IlSDDLK) ![正弦波

and have ginput or something similar automatically trace along the bright green curve and store the x,y coordinates to separate arrays (perhaps by being able to discriminate between the color of the curve and the background color)?并让 ginput 或类似的东西沿着亮绿色曲线自动跟踪并将 x,y 坐标存储到单独的数组中(也许通过能够区分曲线的颜色和背景颜色)? That way I can instead use an actual plot of the x,y coordinates of the curve in the picture, instead of needing to actually use the image in the data analysis.这样我就可以使用图片中曲线的 x,y 坐标的实际绘图,而不需要在数据分析中实际使用图像。

The closest I've been able to get is just using [x,y]=ginput to mash the mouse button along the curve and generate a massive array, but my fingers need rest!我能得到的最接近的只是使用[x,y]=ginput沿着曲线混合鼠标按钮并生成一个巨大的数组,但我的手指需要休息!

Thanks!谢谢!

Take a look at this看看这个

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

Now you can plot the points:现在您可以绘制点:

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

Resulting with结果与在此处输入图片说明

If you are troubled by the fact that the line is thick (ie, multiple y values for each x) you can simply take the mean如果您对线很粗(即每个 x 有多个 y 值)而感到困扰,您可以简单地取平均值

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

Visualizing the line可视化线

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

在此处输入图片说明


If you are after the grid as well, then如果你也在追求网格,那么

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

To determine the grid points we seek x such that many grid points gy has the same gx为了确定网格点,我们寻找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

Same for y: y 也一样:

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

Remove duplicates:删除重复项:

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

Create the grid points创建网格点

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

Now the whole picture:现在全图:

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

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM