g_roi.m 1.24 KB
Newer Older
Daniel Bourgault's avatar
Daniel Bourgault committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
function g_roi(imgFname);

% G_ROI is a function to determine regions of interest in an image.
%
% This function allows the user to define a series of polygons 
% on an image in order to define regions of interest (roi) for 
% image stabilization. The result is a 2D matrix roi with 1s and 0s.
% Just follow ths instructions.
%
% Input:  imgFname: the image filename
% Output: The 2D matrix roi written to filename roi.mat
%
% Author: Daniel Bourgault - 2011
%
%

im = imread(imgFname);
imagesc(im);
colormap(gray);
hold on;

[m n p] = size(im);

roi(1:m,1:n) = 0.0;

I = repmat([1:m]',1,n);
J = repmat([1:n],m,1);

answer='y';
while answer=='y'

  display(' ');
  input('  Position the image as you wish before defining a polygon. Press ENTER when ready.');
  
  display(' ');
  display('  Make a polygon with the mouse. Press ENTER when done with this polygon.')
  display('  You will have the option to make more polygons when done with this one.')
  
  [px py] = ginput;

  px(end+1) = px(1);
  py(end+1) = py(1);
  
  plot(px,py,'r');
  
  roiTmp = inpolygon(I,J,py,px);
  
  display(' ');
  answer = input('  Enter another polygon (y/n)? ','s');
  
  roi = roi + roiTmp;
  
end

ij = find(roi > 1);
roi(ij) = 1;

figure(2);
imagesc(roi);

save roi.mat roi imgFname