Extracting Raw Data From an Image in MATLAB

I was going through some old code and ran across this little gem of a MATLAB script, so I thought I’d post it here for posterity. 

The script lets you specify an image file to view and lets you click on each data point until you’re finished.  Then it exports the (x,y) coordinates of all of the points that you clicked.  This is indispensable if you have a graph from a paper that you would like to analyze further but don’t have the original data file.

% digitize.m
function [x,y] = digitize(imgname)

[img,map] = imread(imgname);
image(img);
colormap(map);

Left = input('Enter the left X coordinate: ');
Right = input('Enter the right X coordinate: ');
Bottom = input('Enter the bottom Y coordinate: ');
Top = input('Enter the top Y coordinate: ');

disp('Click on the lower-left corner');
[localLeft,localBottom] = ginput(1);
disp('Click on the upper-right corner');
[localRight,localTop] = ginput(1);

disp('Begin digitizing points. Click right mouse button when finished.');
b = 0; bold = 0;
while(b~=3)
   [xp,yp,b] = ginput(1);
   if (b == 1)
      if (bold ~= 0)
         line([xold; xp], [yold; yp]);
         x = [x; xp];
         y = [y; yp];
      else
         x = xp;
         y = yp;
      end
      xold = xp; yold = yp; bold = b;
   end
end

x = (x - localLeft) / (localRight-localLeft) * (Right-Left) + Left;
y = (y - localBottom) / (localTop - localBottom) * (Top-Bottom) + Bottom;

Credit goes to Brian Usner, wherever you are :)


You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

AddThis Social Bookmark Button

2 Responses to “Extracting Raw Data From an Image in MATLAB”

  1. How nostalgic! I remember pulling a copy of this file off of the old MATLAB FTP server years ago. It was indispensable as I was working with data that only lived in hard copies.

    Thanks for the memories :)

  2. I had to do this many times when I did my thesis, though the memories aren’t pleasant! It got so tedious I created Dagra. Similar idea, but you use Bezier curves to trace the data. You usually only need a few control points to trace quite complex curves. There’s a .m file included to load the traced data straight into Matlab.

Leave a Reply