-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocessTextFile.m
More file actions
92 lines (82 loc) · 3.64 KB
/
processTextFile.m
File metadata and controls
92 lines (82 loc) · 3.64 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
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
clear;
addpath('./lib');
% pkg load image; % UNCOMMENT THIS LINE IF YOU WANT TO RUN IN OCTAVE. MAKE SURE YOU HAVE IMAGE PACKAGE INSTALLED.
%1. READ AND DISPLAY THE TEXT IMAGE
textImg = fileread('charact1.txt');
img = textTOascii(textImg);
imgStretch = stretchContrast(img);
[row,col] = size(img);
imshow(img); title('Raw image'); input('Press enter to continue');
% 2. THRESHOLD THE IMAGE USING THE MEAN INTENSITY VALUE
[bImg,th] = threshold(img,'mean');
imshow(bImg); title('Segmented binary image'); input('Press enter to continue');
% 3. SEGMENT ALL THE PARTICLES FORM ORIGINAL IMAGE
[labelImg,numLabel] = bwlabel(bImg,8);
props = regionProps(labelImg);
for label = 1:numLabel
cropImg = img(props(label).BoundingBox(1):props(label).BoundingBox(2),props(label).BoundingBox(3):props(label).BoundingBox(4));
imshow(cropImg); title('Individual characters'); input('Press enter to continue');
end
% 4. ROTATE THE CHARACTERS ABOUT THEIR CENTER BY -90 DEGREES
newImg_rot_90 = zeros(row,col,'uint8');
for label = 1:numLabel
cropImg = img(props(label).BoundingBox(1):props(label).BoundingBox(2),props(label).BoundingBox(3):props(label).BoundingBox(4));
rotCropImg = imageRotate(cropImg,-90,'nearestNeighbor');
[rowCrop,colCrop] = size(cropImg);
[rowRotCrop,colRotCrop] = size(rotCropImg);
startRow = int32(props(label).Centroid(1) - rowRotCrop/2);
startCol = int32(props(label).Centroid(2) - colRotCrop/2);
newImg_rot_90(startRow:startRow+rowRotCrop-1,startCol:startCol+colRotCrop-1) = rotCropImg;
end
imshow(newImg_rot_90); title('Rotation by -90 degrees'); input('Press enter to continue');
% 5. ROTATE THE CHARACTERS ABOUT THEIR CENTER BY 35 DEGREES
newImg_rot35 = zeros(row,col,'uint8');
for label = 1:numLabel
cropImg = img(props(label).BoundingBox(1):props(label).BoundingBox(2),props(label).BoundingBox(3):props(label).BoundingBox(4));
rotCropImg = imageRotate(cropImg,35,'nearestNeighbor');
[rowCrop,colCrop] = size(cropImg);
[rowRotCrop,colRotCrop] = size(rotCropImg);
startRow = int32(props(label).Centroid(1) - rowRotCrop/2);
startCol = int32(props(label).Centroid(2) - colRotCrop/2);
newImg_rot35(startRow:startRow+rowRotCrop-1,startCol:startCol+colRotCrop-1) = rotCropImg;
end
imshow(newImg_rot35); title('Rotation by 35 degrees'); input('Press enter to continue');
% 6. FIND THE OUTLINE OF SEGMENTED CHARACTERS
bImgBdry = boundary(bImg);
imshow(bImgBdry); title('Boundary'); input('Press enter to continue');
% 7. FIND THE SKELETON OF SEGMENTED CHARACTERS
bImgSkeleton = skeleton(bImg);
imshow(bImgSkeleton); title('Skeleton'); input('Press enter to continue');
% 8. SCALE AND DISPLAY CHARACTERS 1A2B3C IN SEQUENCE
targetRow = 0;
for label = 1:numLabel
if (props(label).Height>targetRow)
targetRow = props(label).Height;
end
end
finalImg = [];
for label = {2,1,4,3,6,5}
cropImg = img(props(label{1}).BoundingBox(1):props(label{1}).BoundingBox(2),props(label{1}).BoundingBox(3):props(label{1}).BoundingBox(4));
targetCol = int32(props(label{1}).Width * targetRow / props(label{1}).Height);
cropImgScale = imageScale(cropImg,targetRow,targetCol,'bilinear');
finalImg = [finalImg cropImgScale];
end
imshow(finalImg); title('Collated image'); input('Press enter to continue');
% PLOTTING ALL THE IMAGES TOGETHER
subplot(2,4,1), imshow(img)
subplot(2,4,2), imshow(imgStretch)
subplot(2,4,3), imshow(bImg)
subplot(2,4,4), imshow(newImg_rot_90)
subplot(2,4,5), imshow(newImg_rot35)
subplot(2,4,6), imshow(bImgBdry)
subplot(2,4,7), imshow(bImgSkeleton)
subplot(2,4,8), imshow(finalImg)
% PARTICLE LABELS FOR CORRESPONDING ALPHABETS AND DIGITS
% 1 - A
% 2 - 1
% 3 - B
% 4 - 2
% 5 - C
% 6 - 3
% 1 A 2 B 3 C
% 2 1 4 3 6 5