-
Notifications
You must be signed in to change notification settings - Fork 0
/
processFrame.asv
90 lines (68 loc) · 2.64 KB
/
processFrame.asv
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
% Process: Search yellow objects, crop the largest object out of the
% original image and try to read characters
function res = processFrame(frame, charImgs)
startT = toc;
frameRed = frame(:, :, 1);
frameGreen = frame(:, :, 2);
frameBlue = frame(:, :, 3);
selectColors = toc - startT; % Timing
% Lines from graphs
plate = frameRed < 1.33 * frameGreen + 50 & frameRed > frameGreen - 30 ...
& frameBlue < 2 * frameRed / 3 ...
& frameBlue < 2 * frameGreen / 3 + 10 ...
& frameGreen > 2 * frameRed / 3 ...
& (frameRed + frameGreen + frameBlue) > 60; % No dark colors
colorSegr = toc - selectColors - startT;
% Close image
plate = imopen(plate, ones(5));
plate = imclose(plate, ones(10));
close = toc - colorSegr - startT;
% Find index of largest object in labeled image
[L, num] = bwlabel(plate);
stats = regionprops(L, 'Area', 'BoundingBox');
areas = [stats.Area];
[~, sortArIdx] = sort(areas, 'Descend');
bboxes = [stats.BoundingBox];
bbox = [];
areabbox = toc - close - startT;
% Find the largest object with rectangular bounding box
for i = 1:num
maxSizeIdx = sortArIdx(i);
bbox = round(bboxes(maxSizeIdx*4 - 3: maxSizeIdx*4)); % Get bbox of that object
if bbox(3) > 2 * bbox(4) && bbox(3) < 5 * bbox(4) % Check if proportions of plate
break; % Then it's probably the license plate, so stop the loop
end;
end;
findlargest = toc - areabbox - startT;
if (isempty(bbox))
res = '';
return;
end;
% Show isolated plate
% imshow(frameRed .* uint8(plate) + frameRed / 3);
% rectangle('Position', bbox, 'EdgeColor', 'red');
% Get red channel of cropped image
% gray = rgb2gray(frame);
plate = frameRed(bbox(2) + 1:bbox(2) + bbox(4) - 1, bbox(1) + 1:bbox(1) + bbox(3) - 1);
crop = toc - findlargest - startT;
% Create binary image by thresholding with a little lower than the mean value
plate = plate ~= 0 & plate < mean(mean(plate)) * 0.9;
% imshow(plate);
meansegr = toc - crop - startT;
res = '';
% If the image isn't empty, try to read it
if ~isempty(plate)
% Read characters which are present on Dutch license plates
result = ocr(plate, 'CharacterSet', '-0123456789BDFGHJKLNMPRSTVXYZ');
% Sort the character confidences
[sortedConf, sortedIndex] = sort(result.CharacterConfidences, 'descend');
% If there are at least 6 characters recognized, return them
if (length(sortedIndex) >= 6)
res = strtrim(result.Text(:)');
% Show text box
% Iname = insertObjectAnnotation(frame, 'rectangle', bbox, res);
% imshow(Iname);
end;
end;
reading = toc - meansegr - startT;
% statistics = ['Total: ' num2str(toc - startT) ', Reading: ' num2str(reading) ' - Bbox: ' num2str(bbox)]