-
Notifications
You must be signed in to change notification settings - Fork 93
/
regionShapeValidation.m
executable file
·72 lines (61 loc) · 2.08 KB
/
regionShapeValidation.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
function [ hBlobs_valid, hBlobs_centroid ] = regionShapeValidation( hBlobs, H, W, min_size, aspectratio, arearatio, iscuboid )
%REGIONSHAPEVALIDATION Summary of this function goes here
% check whether to find a cuboid or rectangle fitting based on
% segmentation shape
numSeg = length(hBlobs);
invalidMask = false(H,W);
if iscuboid
invalidMask(:,1) = true; invalidMask(:,W) = true;
invalidMask(1,:) = true; invalidMask(H,:) = true;
invalidMask(round(H/2), 1) = true;
invalidMask(round(H/2), round(W/4)) = true;
invalidMask(round(H/2), round(W/2)) = true;
invalidMask(round(H/2), round(3*W/4)) = true;
invalidMask(round(H/2), round(W)) = true;
invalidMask = bwdist(invalidMask)<10;
end
SE = strel('disk', 5, 4);
SS = strel('disk', 2, 4);
hBlobs_valid = false(numSeg, 1);
hBlobs_centroid = zeros(numSeg,2);
parfor bid = 1:length(hBlobs)
% fprintf('%d/%d\n', bid, length(hBlobs));
if hBlobs{bid}.size<min_size
continue;
end
seg = false(H,W);
rect = hBlobs{bid}.rect;
seg(rect(1):rect(3),rect(2):rect(4)) = hBlobs{bid}.mask;
binaryMap = imdilate( seg, SE);
binaryMap = imerode( binaryMap, SE);
binaryMap = imerode( binaryMap, SS);
binaryMap = imdilate( binaryMap, SS);
binaryMap = imfill(binaryMap, 'hole');
CC = bwconncomp(binaryMap);
valid = true(CC.NumObjects,1);
for j = 1:CC.NumObjects
if length(CC.PixelIdxList{j})<min_size
valid(j) = false;
end
if any(invalidMask(CC.PixelIdxList{j}))
valid(j) = false;
end
end
CC.NumObjects = sum(valid);
CC.PixelIdxList = CC.PixelIdxList(valid);
if CC.NumObjects ~= 1
continue;
end
R = regionprops( CC, 'BoundingBox', 'Area', 'ConvexArea', 'Centroid');
boundingbox = R(1).BoundingBox;
asrt = boundingbox(4)/boundingbox(3);
if asrt>aspectratio || asrt<1/aspectratio
continue;
end
if R(1).Area/(R(1).ConvexArea+0.0001)<arearatio
continue;
end
hBlobs_valid(bid) = true;
hBlobs_centroid(bid,:) = R(1).Centroid;
end
end