-
Notifications
You must be signed in to change notification settings - Fork 0
/
WIA3_ruler.m
71 lines (59 loc) · 2.39 KB
/
WIA3_ruler.m
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
62
63
64
65
66
67
68
69
70
71
%%%%%%%%%
% This program continues with previous steps. It allows user to pick
% two points on ruler sticker in the image and convert length in pixels to
% values in centimeters.
%
% ---Xiang Mao modified on Apr 4th, 2010---
%%%%%%%
clear all
close all
clc
%Will:Need to change these varaiables so they're not hard-coded but inputs!
%%Will:
ROOTPATH = 'C:\Documents and Settings\Xiang Mao\My Documents\MATLAB\temporary save 04-Apr-2010\'; % the folder where subfolder for each image been saved
subgroup = 's*.jpg';
sFolders = dir(fullfile([ROOTPATH],subgroup));
nff = size(sFolders,1)
newPATH = ['C:\Documents and Settings\Xiang Mao\My Documents\MATLAB\temp_ruler_' date '\']; % the folder for saving data
imagePATH = ['C:\Documents and Settings\Xiang Mao\My Documents\MATLAB\']; % the path for all original images
mkdir(newPATH);
rulertxt = ['ruler_' date '.txt']; % the text file for save data
fid_w = fopen([newPATH rulertxt],'w');
fprintf(fid_w,'%s\t\n','ImageName/Pixel Per Centimeter/x(1)/x(2)/y(1)/y(2)/Orignal Ruler unit');
%%Will:
for ii = 1:nff
ii
iName = sFolders(ii).name
iName_s = iName(1:(find(iName(:)=='.')-1));
[Y]= imread([imagePATH iName]);
imshow(Y); impixelinfo;
%this section can probably be automated by finding all black lines on a
%white background
disp('zoom in and then hit "enter"')
pause
disp('click on two points on the ruler to define one centimeter (or one inch), and then hit "enter"\n')
[xr,yr]=ginput(2);
zoom out
xd = abs(xr(1)-xr(2));
yd = abs(yr(1)-yr(2));
cd = (xd^2+yd^2)^.5;
ruler = '';
%Will:maybe use an OCR library to infer this from the image. Something that
%Will:would work is here:
%Will:http://www.mathworks.com/matlabcentral/fileexchange/18169-optical-character-recognition-ocr
while isempty(ruler)
ut = input('Is the unit in inches? (enter "inch" if yes, otherwise enter "cm")\n','s');
if strcmp(ut,'inch')
ruler = 'inches'
ppc = cd/2.54
elseif strcmp(ut,'cm')
ruler = 'cm'
ppc = cd
end
end
%Will:probably easier to save with a .mat file instead of printing and
%Will:reading from text!
fprintf(fid_w,'%s\t%g\t%g\t%g\t%g\t%g\t%s\t\n',iName, ppc, xr(1),xr(2),yr(1),yr(2),ruler);
close all
end
fclose(fid_w);