-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathocrGroundTruth.m
More file actions
65 lines (56 loc) · 2.01 KB
/
ocrGroundTruth.m
File metadata and controls
65 lines (56 loc) · 2.01 KB
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
% Performs OCR on the input bounding box frame by frame of the video and
% gets the ground truth distance traveled.
% Input: Top left coordinates, bottom right coordinates.
function ground_truth = ocrGroundTruth(frames, box)
% Add the OCR directory for this task.
addpath './OCR';
if nargin < 2
% Determine the boundingbox.
imshow(frames(:,:,:,1));
[x, y] = ginput(2);
box = int32([y x]);
end
% Load templates
load('OCR/templates.mat');
global templates
num_letters=size(templates,2);
ground_truth = [];
for num_frame=1:size(frames, 4)
% Get I from the frames.
I = frames(:, :, :, num_frame);
% Extract only the bounding box.
I = I(box(1, 1):box(2,1), box(1,2):box(2,2), :);
% Convert to gray scale
im=rgb2gray(I);
% Convert to BW
threshold = graythresh(im);
im = ~im2bw(im,threshold);
% Remove all object containing fewer than 30 pixels
im = bwareaopen(im,30);
word = [];
%Separate the text into individual lines. Since we have a bounding
%box, only process the first line.
[first_line, ~]=lines(im);
% Label and count connected components
[labels, num_components] = bwlabel(first_line);
for n=[2 4]
% Find the each component.
[i,j] = find(labels==n);
if isempty(i)
continue;
end
% Extract letter
n1 = first_line(min(i):max(i),min(j):max(j));
% Resize letter (same size of template)
n1 = imresize(n1,[42 24]);
% Convert image to text.
letter = read_letter(n1, num_letters);
%Only add numbers to the the word.
if double(letter) > 47 && double(letter) < 58
% Concatenate the letter to the word.
word=[word letter];
end
end
ground_truth = [ground_truth; str2double(word)];
end
end