-
Notifications
You must be signed in to change notification settings - Fork 0
/
Q2.m
143 lines (92 loc) · 3.8 KB
/
Q2.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
%% Display the original image on screen.
original_image = imread("hello_world.jpg");
original_image = rgb2gray(original_image);
figure(1);
imshow(original_image, [], 'InitialMagnification','fit');
%% Create an image which is a sub-image of the original image comprising the middleline – HELLO, WORLD.
words_image = original_image(1/3*size(original_image, 1) : 2/3*size(original_image, 1), 5 : 1/2*size(original_image, 2));
figure(2);
imshow(words_image, [], 'InitialMagnification','fit');
%% Create a binary image from Step 2 using thresholding.
[best_threshold, binary_image] = gray2binary_otsu(words_image, 256);
binary_image = 1 - binary_image;
figure(3);
imshow(binary_image, [], 'InitialMagnification','fit');
% binary_image = gray2binary_bernsen(words_image, 7, 10, best_threshold);
% binary_image = 1 - binary_image;
% figure(4);
% imshow(binary_image, [], 'InitialMagnification','fit');
binary_image = gray2binary_naive(words_image, 100);
binary_image = 1 - binary_image;
figure(4);
imshow(binary_image, [], 'InitialMagnification','fit');
% binary_image = gray2binary_niblack(words_image, 9, 0.1);
% binary_image = 1 - binary_image;
% figure(5);
% imshow(binary_image, [], 'InitialMagnification','fit');
% binary_image = gray2binary_kittler(words_image);
% binary_image = 1 - binary_image;
% figure(5);
% imshow(binary_image, [], 'InitialMagnification','fit');
%% Determine a one-pixel thin image of the characters.
thin_image = thin_zhangSuen(binary_image);
figure(5);
imshow(thin_image, [], 'InitialMagnification','fit');
thin_image = thin_rosenfeld(binary_image);
figure(6);
imshow(thin_image, [], 'InitialMagnification','fit');
%% Determine the outline(s) of characters of the image.
sobel_threshold = 3.7;
outline_image = detector_sobel(binary_image, sobel_threshold);
figure(7);
imshow(outline_image, [], 'InitialMagnification','fit');
%` Prewitt detector
prewitt_threshold = 2.8;
outline_image = detector_prewitt(binary_image, prewitt_threshold);
figure(8);
imshow(outline_image, [], 'InitialMagnification','fit');
% matlab自带的 canny 边缘检测函数
ed = edge(binary_image, 'canny', 0.5);
figure(9);
imshow(ed, [], 'InitialMagnification','fit');
% Roberts detector
outline_image = detector_roberts(binary_image);
figure(10);
imshow(outline_image, [], 'InitialMagnification','fit');
% % Laplace
% laplace_threshold = 2.8;
% binary_image = binary_image * 255;
% outline_image = detector_laplace(binary_image, laplace_threshold);
% figure(10);
% imshow(outline_image, [], 'InitialMagnification','fit');
%% Segment the image to separate and label the different characters.
% classical connected components algorithm
label_matrix = label_classical(binary_image);
label_img = label2rgb(label_matrix, 'jet', 'w', 'shuffle');
figure(11);
imshow(label_img, [], 'InitialMagnification','fit');
% region seeds growing
label_matrix = label_rsg(binary_image);
label_img = label2rgb(label_matrix, 'jet', 'w', 'shuffle');
figure(12);
imshow(label_img, [], 'InitialMagnification','fit');
%%
% seperate
[img_num, s] = img_seperate(label_matrix);
s=s(2:11);
for i = 1 : length(s)
s(i).Image=1-imbinarize(imresize(s(i).Image,[128,128]));
figure(12+i);
imshow(s(i).Image, [], 'InitialMagnification','fit');
end
%% Train the (conventional) unsupervised classification method of your choice
% (i.e., self-ordered maps (SOM), k-nearest neighbors (kNN), or support vector machine (SVM)) to
% recognize the different characters ("H", "E”, “L”, “O”, “W”, “R”, “D”). You should
% use 75% of the dataset to train your classifier, and the remaining 25% for validation
% (testing). Then, test your trained classifier on each characters in image 1, reporting
% the final classification results.
% train_knn(5);
result=[];
for i=1:length(s)
result=[result,train_knn(5,s(i).Image,2)];
end