This repository has been archived by the owner on Apr 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrowHair.asv
120 lines (91 loc) · 3.8 KB
/
growHair.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
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
function [Img, Volume,Longhair, Bounds, Failed] = growHair(Image,foreHeadTop)
if ~exist('Image','var')
error('Image was not set please specify Input image')
elseif isempty(Image)
error('Selected Image was Empty and can not be used')
elseif ndims(Image) > 3 || isequal(ndims(Image),1)
error('The selected image has the wrong dimensions, only 2D grayscale Images and RGB Images are allowed')
else
Image = im2double(Image);
end
%Setup reference and fixxed parameters
%The image should be scaled to have 100000 pixels
pixRefSize = 100000;
%The seedPoints should be set 20 pixels above the foreHead Top
foreHeadOffset = 20;
%Set the offset of the other 4 seedPoints that will be set in
%regiongrowing
seedSpread = 5;
%Setup image Dimensions
[xDim, yDim, ~] = size(Image);
%Calculate the factor of how much the image would need to be scaled
%to match the pixelVolume of pixRefSize
scaleFactor = pixRefSize / (xDim * yDim);
%Filter the image with a gaussian Filter to smoothen image regions
gauss = gaussianimplem(Image,3);
% Regiongrowing often works better if the image is
% downscaled and the algorythm is way faster
gauss = imresize(gauss,scaleFactor);
%Setup Scaled image dimensions
[scaledXDim, scaledYDim, ~] = size(gauss);
if ~exist('foreHeadTop','var') || isempty(foreHeadTop)
[loc] = get_image_point(gauss);
foreHeadTop(1) = loc(1,2);
foreHeadTop(2) = loc(1,1);
%error('foreHeadTop does not exist, please specify foreHeadTop Point')
elseif ~isequal(ndims(foreHeadTop),2)
error('error Position needs to have 2 Values')
elseif ~boundcheck(foreHeadTop(1),foreHeadTop(2),scaledXDim,scaledYDim)
error('foreHeadTop was out of the Image bounds, try again')
end
%Setup seedPosition above the foreheadPosition
%to try and catch the Hair
seedPos(1) = foreHeadTop(1)- foreHeadOffset - seedSpread ;
seedPos(2) = foreHeadTop(2);%+ foreHeadOffset + seedSpread;
%Check if seedPos is inside the image Bounds
if ~boundcheck(seedPos(1),seedPos(2),scaledXDim,scaledYDim)
error('seedPos exceeds the Image bounds, try again')
end
%Check if image already is grayscale otherwise convert
if isequal(ndims(Image),3)
grayImg = rgb2gray(gauss);
else
grayImg = gauss;
end
%Do Regiongrowing on the image to get binary mask
tic
Binmask = growmyregion(grayImg,seedPos,0.1);
toc
%Measure Regions in Binary Mask
measurements = regionprops(Binmask, 'BoundingBox', 'Area');
%Setup loop seching for the biggest area of the found regions
mainRegion = measurements(1);
if ndims(measurements)/2 > 1
for regionCnt = 2:ndims(measurements)/2
if(measurements(regionCnt*2).Area > mainRegion.Area)
mainRegion = measurements(regionCnt);
end
end
end
boundingVector = mainRegion.BoundingBox;
lenghtratio = boundingVector(4)/boundingVector(3);
Longhair = false;
if lenghtratio > 0.9
Longhair = true;
end
Volume = sum(sum(sum(Binmask)));
Failed = false;
if Volume > (scaledXDim * scaledYDim)/2
Failed = true;
end
resultString = ['Volume:' num2str(Volume) ' LenghtRatio:' num2str(lenghtratio) ' Isfemale:' num2str(Longhair) '::Bounds:' num2str(boundingVector)];
imageSize = size(Image);
fontsize = cast((imageSize(1)/30),'int32');
if fontsize < 1
fontsize = 1;
end
resultWithText = insertText(gauss,[4,4], resultString,'FontSize',fontsize);
resultWithText(:,:,1) = resultWithText(:,:,1)+ Binmask;
Bounds = boundingVector;
Img = resultWithText;
end