From bce0239122c59e5630331aa9db4246cffcaa2c22 Mon Sep 17 00:00:00 2001 From: "Jake W. Ireland" Date: Mon, 28 Sep 2020 22:50:25 +1300 Subject: [PATCH] use dict for feature types for readability (closes #24) --- src/AdaBoost.jl | 2 +- src/HaarLikeFeature.jl | 12 ++++++------ src/Utils.jl | 20 ++++++++++---------- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/AdaBoost.jl b/src/AdaBoost.jl index 86cd721e4..7c9ab41cd 100755 --- a/src/AdaBoost.jl +++ b/src/AdaBoost.jl @@ -235,7 +235,7 @@ function _create_features( """) end - for feature in feature_types # (feature_types are just tuples) + for feature in values(feature_types) # (feature_types are just tuples) feature_start_width = max(min_feature_width, feature[1]) for feature_width in range(feature_start_width, stop=max_feature_width, step=feature[1]) feature_start_height = max(min_feature_height, feature[2]) diff --git a/src/HaarLikeFeature.jl b/src/HaarLikeFeature.jl index 77bf47660..b4c2d1d69 100755 --- a/src/HaarLikeFeature.jl +++ b/src/HaarLikeFeature.jl @@ -7,7 +7,7 @@ include("IntegralImage.jl") -feature_types = [(1, 2), (2, 1), (3, 1), (1, 3), (2, 2)] +feature_types = Dict{String, Tuple{Integer, Integer}}("two_vertical" => (1, 2), "two_horizontal" => (2, 1), "three_horizontal" => (3, 1), "three_vertical" => (1, 3), "four" => (2, 2)) abstract type HaarFeatureAbstractType end # abstract type AbstractHaarLikeObject <: HaarFeatureAbstractType end @@ -61,29 +61,29 @@ function get_score(feature::HaarLikeObject, int_img::Array) score = 0 faceness = 0 - if feature.feature_type == feature_types[1] # two vertical + if feature.feature_type == feature_types["two_vertical"] first = sum_region(int_img, feature.top_left, (feature.top_left[1] + feature.width, Int(round(feature.top_left[2] + feature.height / 2)))) second = sum_region(int_img, (feature.top_left[1], Int(round(feature.top_left[2] + feature.height / 2))), feature.bottom_right) score = first - second faceness = 1 - elseif feature.feature_type == feature_types[2] # two horizontal + elseif feature.feature_type == feature_types["two_horizontal"] first = sum_region(int_img, feature.top_left, (Int(round(feature.top_left[1] + feature.width / 2)), feature.top_left[2] + feature.height)) second = sum_region(int_img, (Int(round(feature.top_left[1] + feature.width / 2)), feature.top_left[2]), feature.bottom_right) score = first - second faceness = 2 - elseif feature.feature_type == feature_types[3] # three horizontal + elseif feature.feature_type == feature_types["three_horizontal"] first = sum_region(int_img, feature.top_left, (Int(round(feature.top_left[1] + feature.width / 3)), feature.top_left[2] + feature.height)) second = sum_region(int_img, (Int(round(feature.top_left[1] + feature.width / 3)), feature.top_left[2]), (Int(round(feature.top_left[1] + 2 * feature.width / 3)), feature.top_left[2] + feature.height)) third = sum_region(int_img, (Int(round(feature.top_left[1] + 2 * feature.width / 3)), feature.top_left[2]), feature.bottom_right) score = first - second + third faceness = 3 - elseif feature.feature_type == feature_types[4] # three vertical + elseif feature.feature_type == feature_types["three_vertical"] first = sum_region(int_img, feature.top_left, (feature.bottom_right[1], Int(round(feature.top_left[2] + feature.height / 3)))) second = sum_region(int_img, (feature.top_left[1], Int(round(feature.top_left[2] + feature.height / 3))), (feature.bottom_right[1], Int(round(feature.top_left[2] + 2 * feature.height / 3)))) third = sum_region(int_img, (feature.top_left[1], Int(round(feature.top_left[2] + 2 * feature.height / 3))), feature.bottom_right) score = first - second + third faceness = 4 - elseif feature.feature_type == feature_types[5] # four + elseif feature.feature_type == feature_types["four"] # top left area first = sum_region(int_img, feature.top_left, (Int(round(feature.top_left[1] + feature.width / 2)), Int(round(feature.top_left[2] + feature.height / 2)))) # top right area diff --git a/src/Utils.jl b/src/Utils.jl index b5d9d2cff..d7793486e 100755 --- a/src/Utils.jl +++ b/src/Utils.jl @@ -221,7 +221,7 @@ function reconstruct(classifiers::AbstractArray, img_size::Tuple) for c in classifiers # map polarity: -1 -> 0, 1 -> 1 polarity = ((1 + c.polarity)^2)/4 - if c.feature_type == feature_types[1] # two vertical + if c.feature_type == feature_types["two_vertical"] for x in 1:c.width sign = polarity for y in 1:c.height @@ -231,7 +231,7 @@ function reconstruct(classifiers::AbstractArray, img_size::Tuple) image[c.top_left[2] + y, c.top_left[1] + x] += 1 * sign * c.weight end end - elseif c.feature_type == feature_types[2] # two horizontal + elseif c.feature_type == feature_types["two_horizontal"] sign = polarity for x in 1:c.width if x >= c.width/2 @@ -241,7 +241,7 @@ function reconstruct(classifiers::AbstractArray, img_size::Tuple) image[c.top_left[1] + x, c.top_left[2] + y] += 1 * sign * c.weight end end - elseif c.feature_type == feature_types[3] # three horizontal + elseif c.feature_type == feature_types["three_horizontal"] sign = polarity for x in 1:c.width if iszero(mod(x, c.width/3)) @@ -251,7 +251,7 @@ function reconstruct(classifiers::AbstractArray, img_size::Tuple) image[c.top_left[1] + x, c.top_left[2] + y] += 1 * sign * c.weight end end - elseif c.feature_type == feature_types[4] # three vertical + elseif c.feature_type == feature_types["three_vertical"] for x in 1:c.width sign = polarity for y in 1:c.height @@ -261,7 +261,7 @@ function reconstruct(classifiers::AbstractArray, img_size::Tuple) image[c.top_left[1] + x, c.top_left[2] + y] += 1 * sign * c.weight end end - elseif c.feature_type == feature_types[5] # four + elseif c.feature_type == feature_types["four"] sign = polarity for x in 1:c.width if iszero(mod(x, c.width/2)) @@ -435,7 +435,7 @@ function generate_validation_image(image_path::AbstractString, classifiers::Abst for c in classifiers # map polarity: -1 -> 0, 1 -> 1 polarity = ((1 + c.polarity)^2)/4 - if c.feature_type == feature_types[1] # two vertical + if c.feature_type == feature_types["two_vertical"] for x in 1:c.width sign = polarity for y in 1:c.height @@ -445,7 +445,7 @@ function generate_validation_image(image_path::AbstractString, classifiers::Abst boxes[c.top_left[2] + y, c.top_left[1] + x] += 1 * sign * c.weight end end - elseif c.feature_type == feature_types[2] # two horizontal + elseif c.feature_type == feature_types["two_horizontal"] sign = polarity for x in 1:c.width if x >= c.width/2 @@ -455,7 +455,7 @@ function generate_validation_image(image_path::AbstractString, classifiers::Abst boxes[c.top_left[1] + x, c.top_left[2] + y] += 1 * sign * c.weight end end - elseif c.feature_type == feature_types[3] # three horizontal + elseif c.feature_type == feature_types["three_horizontal"] sign = polarity for x in 1:c.width if iszero(mod(x, c.width/3)) @@ -465,7 +465,7 @@ function generate_validation_image(image_path::AbstractString, classifiers::Abst boxes[c.top_left[1] + x, c.top_left[2] + y] += 1 * sign * c.weight end end - elseif c.feature_type == feature_types[4] # three vertical + elseif c.feature_type == feature_types["three_vertical"] for x in 1:c.width sign = polarity for y in 1:c.height @@ -475,7 +475,7 @@ function generate_validation_image(image_path::AbstractString, classifiers::Abst boxes[c.top_left[1] + x, c.top_left[2] + y] += 1 * sign * c.weight end end - elseif c.feature_type == feature_types[5] # four + elseif c.feature_type == feature_types["four"] sign = polarity for x in 1:c.width if iszero(mod(x, c.width/2))