Page 215 - MATLAB Recipes for Earth Sciences
P. 215
210 8 Image Processing
interest are entered by moving a cursor or cross hair and clicking the mouse
button. The result is a two-dimensional array of xy data, such as longitudes
and latitudes of the points of a polygon or the coordinates of the objects of
interest in an area.
The function ginput contained in the standard MATLAB toolbox pro-
vides graphical input using a mouse on the screen. It is generally used to
select points such as specific data points from a figure created by a arbitrary
graphics function such as plot. The function is often used for interactive
plotting, i.e., the digitized points appear on the screen after they were select-
ed. The disadvantage of the function is that it does not provide coordinate
referencing on an image. Therefore, we use a modified version of the func-
tion that allows to reference an image to an arbitrary rectangular coordinate
system. Save the following code in a text fi le minput.m.
function data = minput(imagefile)
% Specify the limits of the image
xmin = input('Specify xmin! ');
xmax = input('Specify xmax! ');
ymin = input('Specify ymin! ');
ymax = input('Specify ymax! ');
% Read image and display
B = imread(imagefile);
a = size(B,2); b = size(B,1);
imshow(B);
% Define upper left and lower right corner of image
disp('Click on lower left and upper right cr, then <return>')
[xcr,ycr] = ginput;
XMIN=xmin-((xmax-xmin)*xcr(1,1)/(xcr(2,1)-xcr(1,1)));
XMAX=xmax+((xmax-xmin)*(a-xcr(2,1))/(xcr(2,1)-xcr(1,1)));
YMIN=ymin-((ymax-ymin)*ycr(1,1)/(ycr(2,1)-ycr(1,1)));
YMAX=ymax+((ymax-ymin)*(b-ycr(2,1))/(ycr(2,1)-ycr(1,1)));
% Digitize data points
disp('Click on data points to digitize, then <return>')
[xdata,ydata] = ginput;
XDATA = XMIN + ((XMAX-XMIN)*xdata / size(B,2));
YDATA = YMIN + ((YMAX-YMIN)*ydata / size(B,1));
data(:,1) = XDATA; data(:,2) = YDATA;
The function minput has four parts. In the first part, the user enters the lim-
its of the coordinate axis as the reference for the image. Next, the image is
imported into the workspace and displayed on the screen. The third part uses
ginput to define the upper left and lower right corners of the image. The re-
lationship between the coordinates of the two corners on the fi gure window
and the reference coordinate system is used to compute the transformation
for all points digitized in the fourth part.