def test_three_vertical(self): feature = HaarLikeFeature(FeatureType.THREE_VERTICAL, (0, 0), 24, 24, 100000, 1) left_area = ii.sum_region(self.int_img, (0, 0), (24, 8)) middle_area = ii.sum_region(self.int_img, (0, 8), (24, 16)) right_area = ii.sum_region(self.int_img, (0, 16), (24, 24)) expected = 1 if feature.threshold * feature.polarity > left_area - middle_area + right_area else 0 assert feature.get_vote(self.int_img) == expected
def test_three_horizontal(self): feature = HaarLikeFeature(FeatureType.THREE_HORIZONTAL, (0, 0), 24, 24, 100000, 1) left_area = ii.sum_region(self.int_img, (0, 0), (8, 24)) middle_area = ii.sum_region(self.int_img, (8, 0), (16, 24)) right_area = ii.sum_region(self.int_img, (16, 0), (24, 24)) expected = 1 if feature.threshold * feature.polarity > left_area - middle_area + right_area else 0 assert feature.get_vote(self.int_img) == expected
def test_four(self): feature = HaarLikeFeature(FeatureType.THREE_HORIZONTAL, (0, 0), 24, 24, 100000, 1) top_left_area = ii.sum_region(self.int_img, (0, 0), (12, 12)) top_right_area = ii.sum_region(self.int_img, (12, 0), (24, 12)) bottom_left_area = ii.sum_region(self.int_img, (0, 12), (12, 24)) bottom_right_area = ii.sum_region(self.int_img, (12, 12), (24, 24)) expected = 1 if feature.threshold * feature.polarity > top_left_area - top_right_area - bottom_left_area + bottom_right_area else 0 assert feature.get_vote(self.int_img) == expected
def _create_features(img_height, img_width, min_feature_width, max_feature_width, min_feature_height, max_feature_height): print('Creating haar-like features..') features = [] for feature in FeatureTypes: # FeatureTypes are just tuples feature_start_width = max(min_feature_width, feature[0]) for feature_width in range(feature_start_width, max_feature_width, feature[0]): feature_start_height = max(min_feature_height, feature[1]) for feature_height in range(feature_start_height, max_feature_height, feature[1]): for x in range(img_width - feature_width): for y in range(img_height - feature_height): features.append( HaarLikeFeature(feature, (x, y), feature_width, feature_height, 0, 1)) features.append( HaarLikeFeature(feature, (x, y), feature_width, feature_height, 0, -1)) print('..done. ' + str(len(features)) + ' features created.\n') return features
def create_features(imgHeight, imgWidth, minFeatureWidth, maxFeatureWidth, minFeatureHeight, maxFeatureHeight): print('Creating haar like features...') features = [] for feature in FeatureTypes: # FeatureTypes are just tuples featureStartWidth = max(minFeatureWidth, feature[0]) for featureWidth in range(featureStartWidth, maxFeatureWidth, feature[0]): featureStartHeight = max(minFeatureHeight, feature[1]) for featureHeight in range(featureStartHeight, maxFeatureHeight, feature[1]): for x in range(imgWidth - featureWidth): for y in range(imgHeight - featureHeight): features.append( HaarLikeFeature(feature, (x, y), featureWidth, featureHeight, 0.11, -1)) # features.append(HaarLikeFeature( # feature, (x, y), featureWidth, featureHeight, 0, 1)) # features.append(HaarLikeFeature( # feature, (x, y), featureWidth, featureHeight, 0, -1)) print(str(len(features)) + ' features are created!') return features
def test_two_vertical_fail(self): feature = HaarLikeFeature(FeatureType.TWO_VERTICAL, (0, 0), 24, 24, 100000, 1) left_area = ii.sum_region(self.int_img, (0, 0), (24, 12)) right_area = ii.sum_region(self.int_img, (0, 12), (24, 24)) expected = 1 if feature.threshold * -1 > left_area - right_area else 0 assert feature.get_vote(self.int_img) != expected
def compute_feature(box, featureChosen, integralImg): # scaling features boxSize = box[0] # area sideLength = int(np.sqrt(boxSize)) # @ TODO the calFeatures file # featureChosen = features[featureIndex] # features should be from the calFeatures file areaPos_i = box[1] areaPos_j = box[2] sampleSize = 24 scale = sideLength / sampleSize # scaling the i and j of the feature i = int(scale * featureChosen.topLeft[1] + 0.5) # topLeft[1] rows j = int(scale * featureChosen.topLeft[0] + 0.5) # topLeft[0] cols # abs_i and abs_j will be used to calculate the integral image result # indicate the feature position inside the real frame abs_i = areaPos_i + i abs_j = areaPos_j + j # getting the haar feature width and height # we will check on the feature pattern to get the width width = featureChosen.width height = featureChosen.height originArea = width * height # scaling the width and the height of the feature width = int(scale * width + 0.5) height = int(scale * height + 0.5) # scaling the feature pattern one i.e. 1x2 feature if (featureChosen.featureType == FeatureType.TWO_HORIZONTAL): ''' the height of the feature may exceeds the box's size - i as boxSize - i is the maximum side the feature's height can hold ''' ''' the width of the feature may exceeds the box's size - j as boxSize - j is the maximum side the feature's width can hold ''' # we should make sure that width is divisible by 2 after scaling while (width > sideLength - j): width -= 2 # scaling the feature pattern two i.e. 1x3 feature elif (featureChosen.featureType == FeatureType.THREE_HORIZONTAL): ''' the height of the feature may exceeds the box's size - i as boxSize - i is the maximum side the feature's height can hold ''' ''' the width of the feature may exceeds the box's size - j as boxSize - j is the maximum side the feature's width can hold ''' while (width > sideLength - j): width -= 3 # scaling the feature pattern one i.e. 2x1 feature elif (featureChosen.featureType == FeatureType.TWO_VERTICAL): '''p the height of the feature may exceeds the box's size - i as boxSize - i is the maximum side the feature's height can hold ''' # we should make sure that height is divisible by 2 after scaling while (height > sideLength - i): height -= 2 # scaling the feature pattern one i.e. 3x1 feature elif (featureChosen.featureType == FeatureType.THREE_VERTICAL): ''' the height of the feature may exceeds the box's size - i as boxSize - i is the maximum side the feature's height can hold ''' # we should make sure that height is divisible by 3 after scaling while (height > sideLength - i): height -= 3 # scaling the feature pattern one i.e. 2x2 feature else: ''' the width of the feature may exceeds the box's size - j as boxSize - j is the maximum side the feature's width can hold ''' # we should make sure that width is divisible by 2 after scaling while (width > sideLength - j): width -= 2 ''' the height of the feature may exceeds the box's size - i as boxSize - i is the maximum side the feature's height can hold ''' # we should make sure that height is divisible by 2 after scaling while (height > sideLength - i): height -= 2 new_feature = HaarLikeFeature(featureChosen.featureType, (abs_j, abs_i), width, height, featureChosen.threshold, featureChosen.polarity) # print(new_feature) score = new_feature.get_score(integralImg) # rescale the feature to its original scale # multiply the originArea by 2 reScale = originArea / (width * height) featureResult = score * reScale return featureResult, new_feature