Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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!

like image 412
bieberman Avatar asked Apr 15 '15 06:04

bieberman


People also ask

How do I find the lines of an image in Matlab?

Find lines in the image using the houghlines function. lines = houghlines(BW,theta,rho,P,'FillGap',5,'MinLength',7); Create a plot that displays the original image with the lines superimposed on it. figure, imshow(rotI), hold on max_len = 0; for k = 1:length(lines) xy = [lines(k).

How do I draw a line on an image in Matlab?

Display the image. imshow(I); Draw a Line ROI on the image. roi = drawline('Color','r');

What is Bwareafilt Matlab?

example. BW2 = bwareafilt( BW , range ) extracts all connected components (objects) from the binary image BW , where the area of the objects is in the specified range , producing another binary image BW2 . bwareafilt returns a binary image BW2 containing only those objects that meet the criteria.


1 Answers

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

like image 94
Shai Avatar answered Sep 21 '22 10:09

Shai