-
Notifications
You must be signed in to change notification settings - Fork 1
/
ImageProcessingIndividual.m
159 lines (129 loc) · 5.68 KB
/
ImageProcessingIndividual.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
%% Program Calculates the Equivalent Diameter and Area of a Bubble using Image Processing
tic % Start timer.
clc; % Clear command window.
clearvars; % Get rid of variables from prior run of this m-file.
fprintf('Processing the Images...\n'); % Message sent to command window.
workspace; % Make sure the workspace panel with all the variables is showing.
imtool close all; % Close all imtool figures.
format long g;
format compact;
% File Search
imFolder = '/MATLAB Drive/ImageProcessing/PERM'; % Specify the folder where the files live.
imFolder = '/MATLAB Drive/ImageProcessing/PERM'; % Specify the folder where the files live.
% Folder availablity check
if ~isfolder(imFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s\nPlease specify a new folder.', imFolder);
uiwait(warndlg(errorMessage));
imFolder = uigetdir(); % Ask for a new one.
if imFolder == 0
% User clicked Cancel
return;
end
end
filePattern = fullfile(imFolder, '*.bmp');
%% To get rid of the ASCII sorting
files = dir(filePattern);
numfiles = size(files,1); % Find number of files
numdelim = 1; % Number of delimiters
delims = ['.']; % Delimiters used
filenums=[ [1:numfiles]' zeros(numfiles,1) ];
for i=1:numfiles % Cycles through list of files
rem=files(i).name;
for j=1:numdelim % Cycles through the filename delimiters
[token,rem] = strtok(rem,delims);
filenums(i,j+1) = str2num(token);
end
end
filenums = sortrows(filenums,[2:numdelim+1]);
for i=1:numfiles
sortedfiles{i,1} = files(filenums(i,1)).name;
end
theFiles = cell2struct(sortedfiles,'name',numfiles); % Converting the cell to a structure
[files(1:numel(theFiles)).name] = theFiles.name; % Copying the content of a structure to another one
%%
for z = 1 : length(files)
baseFileName = files(z).name;
fullFileName = fullfile(files(z).folder, baseFileName);
fprintf(1, 'Now processing %s\n', fullFileName);
% Image Processing Starts
FileName = baseFileName;
TrueColorImage = imread(FileName);
GrayImage = rgb2gray(TrueColorImage); % Convert it to grayscale
GrayImage = medfilt2(GrayImage); % Median Noise Reduction
GrayImage = imcomplement(GrayImage);
GrayImage = imfill(GrayImage, 'holes');
GrayImage = imcomplement(GrayImage);
Threshold = 70; % Parametric
BinaryImage = GrayImage < Threshold; % Choosing balck images. Uses > for bright images
BinaryImage = imfill(BinaryImage, 'holes');
% Bug Correction generated by Median Filter
BinaryImage(1:2 , 1:2) = 0;
BinaryImage(1:2 , end-1:end) = 0;
BinaryImage(end-1:end , 1:2) = 0;
BinaryImage(end-1:end , end-1:end) = 0;
% Labeling each blob
LabeledImage = bwlabel(BinaryImage, 8); % The label matrix using 8-connected objects
% Get all the information about the shapes
BlobMeasure = regionprops(LabeledImage, GrayImage, 'all');
NumberOfBlobs = size(BlobMeasure, 1);
% Plotting the borders of all the blobs
%set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
figure(1), imshow(GrayImage)
axis image; % Make sure image is not artificially stretched because of screen's aspect ratio.
hold on;
Boundaries = bwboundaries(BinaryImage);
NumberOfBoundaries = size(Boundaries, 1);
for k = 1 : NumberOfBoundaries
ThisBoundary = Boundaries{k};
plot(ThisBoundary(:,2), ThisBoundary(:,1), 'r', 'LineWidth', 2);
end
hold off;
TextFontSize = 7; % Used to control size of "blob number" labels put atop the image.
LabelShiftX = -7; % Used to align the labels in the centers of the blobs.
BlobECD = zeros(1, NumberOfBlobs);
BlobEqCirleDia = zeros(1, NumberOfBlobs);
BlobEqCirleArea = zeros(1, NumberOfBlobs);
BlobEqElMajor = zeros(1, NumberOfBlobs);
BlobEqElMinor = zeros(1, NumberOfBlobs);
BlobEqElArea = zeros(1, NumberOfBlobs);
for k = 1 : NumberOfBlobs
BlobCentroid = BlobMeasure(k).Centroid; % Get centroid one at a time
BlobEqCirleDia(k) = BlobMeasure(k).EquivDiameter/44; % Get Circle Diameter
BlobEqCirleArea(k) = pi*((BlobEqCirleDia(k))^2)/4; % Get Circle Area
BlobEqElMajor(k) = BlobMeasure(k).MajorAxisLength/44; % Elipse Major Length
BlobEqElMinor(k) = BlobMeasure(k).MajorAxisLength/44; % Elipse Major Length
BlobEqElArea(k) = pi*BlobEqElMajor(k)*BlobEqElMinor(k)/4; % Elipse Area
text(BlobCentroid(1) + LabelShiftX, BlobCentroid(2), num2str(k), 'FontSize', TextFontSize, 'FontWeight', 'Bold','Color', 'w');
end
saveas(gcf,sprintf('Processed Image %02d.png',z));
figure(2)
% To Print on a table and combine figures
BlobNumber = [1:k]';
Circle_Diameter = BlobEqCirleDia';
Circle_Area = BlobEqCirleArea';
Elipse_Area = BlobEqElArea';
T = table(BlobNumber,Circle_Diameter,Circle_Area,Elipse_Area);
h = subplot(3,1,2);
hPos = get(h, 'Position');
uit = uitable('Data',T{:,:},'ColumnName',T.Properties.VariableNames,...
'RowName',T.Properties.RowNames,'Units', 'Normalized', 'Position', hPos);
set(h, 'Visible', 'Off')
uit.ColumnName = {'Blob #','Circle Diameter [mm]','Circle Area [mm^2]','Ellipse Area [mm^2]'};
saveas(uit,sprintf('Processed Data %02d.png',z));
DatafileName = sprintf('Processed Data %02d.dat',z);
% To save on a table
fileID = fopen(DatafileName,'w');
fprintf(fileID,'%6s %23s %21s %22s\n','Blob #','Circle Diameter [mm]','Circle Area [mm^2]','Ellipse Area [mm^2]');
formatSpec = '%3d %18.2f %22.2f %22.2f\n';
[nrows,ncols] = size(T);
for row = 1:nrows
fprintf(fileID,formatSpec,T{row,:});
end
fclose(fileID);
if z == length(theFiles)
zip('Processed',{'*.png','*.dat'});
else
continue
end
end
toc