-
Notifications
You must be signed in to change notification settings - Fork 1
/
CheckFrameForMold.m
137 lines (93 loc) · 3.38 KB
/
CheckFrameForMold.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
function moldFound = CheckFrameForMold(frameToCheck, referenceImage)
% %edge detect and subtract
% reducedImage = FindSubtractedEdge(frameToCheck, referenceImage);
% figure;
% imshow(reducedImage);
%
% %remove invalid objects from image
% cleanedImage = EliminateInvalidObjects(reducedImage, 1000, 10);
%
% figure;
% imshow(cleanedImage);
% figure;
moldFound = false;
for i = 1:length(frameToCheck)
%pause(1);
filteredImage = FilterImage(frameToCheck{i}, referenceImage{i});
%count the remaining objects in view
objCount = CountObjects(filteredImage);
%returns true if more objects were found than were expected
if objCount > 1
moldFound = true;
%break out of loop if one mold was found
break;
end
end
end
function filteredImage = FilterImage(frameToCheck, referenceImage)
%edge detect and subtract
reducedImage = FindSubtractedEdge(frameToCheck, referenceImage);
imshow(reducedImage);
title('subtracted')
%pause(1);
%rejoin objects broken from subtraction
rejoinedImage = EnlargeObjects(reducedImage, 3);
imshow(rejoinedImage);
title('rejoined')
pause(1);
%remove all invalid objdects from image
cleanedImage = EliminateInvalidObjects(rejoinedImage, 450, 350);
imshow(cleanedImage);
title('cleaned')
%%pause(1);
%enlarge remaining structures
%enhancedImage = EnlargeObjects(cleanedImage, 3);
%imshow(enhancedImage);
%TODO: remove remaining invalid structures
%return cleaned image
filteredImage = cleanedImage;
end
function returnImage = EnlargeObjects(image, radius)
structureElement = strel('disk', radius, 0);
%structureElement = strel('square', 5); alternate dialation option
returnImage = imdilate(image, structureElement);
end
function objCount = CountObjects(image)
CC = bwconncomp(image);
objCount = CC.NumObjects();
end
function cleanedImage = EliminateInvalidObjects(image, maxObjSize, minObjSize)
cleanedImage = image;
CC = bwconncomp(cleanedImage);
pixelArray = cellfun(@numel, CC.PixelIdxList);
%identify invalid structures
invalidStructures = find(pixelArray > maxObjSize | pixelArray <= minObjSize);
%use to print the number of invalid structures found
%size(invalid_structures)
%remove invalid structures
for k = 1:length(invalidStructures)
invalid_pix = CC.PixelIdxList{invalidStructures(k)};
%every invalid pixel gets set to black
cleanedImage(invalid_pix) = 0;
end
end
function returnImage = FindSubtractedEdge(image,refImg)
%TODO: subtract out last image too?
imshow(image);
%sharpen the image
sharpImage = imsharpen(image);
imshow(sharpImage);
title('sharp');
%pause(1);
%sharpen the reference image
sharpRef = imsharpen(refImg);
%find the edges of the image to subtract from
im = edge(sharpImage);
%find the edges of the reference image(image to subtract)
rim = edge(sharpRef);
%Enlarge all objects in reference image to get rid of more noise
rim = EnlargeObjects(rim, 3);
%return the result of the subtraction
returnImage = im - rim;
returnImage = imfill(returnImage, 'holes');
end