Tracking a line in a MATLAB image

I am compiling a program to calculate some material with the oscilloscope outputs, but as the program runs now, I just import the image into MATLAB and then use ginput to find the coordinates of different areas 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 like that automatically traced by a bright green curve and save the x, y coordinates to separate the arrays (perhaps by distinguishing between the color of the curve and the background color)? Thus, I can instead use the actual graph of the x, y coordinates of the curve in the image, instead of actually using the image in the data analysis.

The closest I could get was just to use [x,y]=ginput to wipe the mouse button along the curve and create a massive array, but my fingers need some rest!

Thanks!

+5
source share
1 answer

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 xy coordinates of green pixels 

Now you can build the points:

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

As a result enter image description here

If you are concerned that the line is thick (i.e. several y values ​​for each x), you can simply take the average value

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

Line visualization

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

enter image description here


If you are after the grid, then

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

To determine the grid points, we look for x so that many grid points of the gy have 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 ); 

Delete duplicates:

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

Create 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

+11
source

All Articles