Trace a line in an image MATLAB

2020-06-19 17:59发布

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.

Is there a way that I can take, say, this image:

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

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)? 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.

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!

Thanks!

1条回答
家丑人穷心不美
2楼-- · 2020-06-19 18:23

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 enter image description here

If you are troubled by the fact that the line is thick (i.e., multiple y values for each x) you can simply take the mean

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

Visualizing the line

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

enter image description here


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

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:

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

enter image description here

查看更多
登录 后发表回答