Skip to content

Commit 92921af

Browse files
committed
Tweak Aggregate color_distance
1 parent 688d93f commit 92921af

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

visionmagic/src/aggregation.rs

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,15 @@ impl ProcessorTrait for Processor {
9898
(*otheri, Self::color_distance(myself, other))
9999
}).collect();
100100
votes.sort_by_key(|v| v.1);
101-
let diff = votes[0].1 as f64 / 10000.0;
102-
if (myself.area() < self.params.min_size as usize / 16) ||
103-
(diff < self.params.deviation && myself.area() < self.params.min_size as usize) ||
104-
(diff < self.params.deviation * 2.0 && myself.area() < self.params.min_size as usize / 4) ||
105-
(diff < self.params.deviation / 2.0 && myself.area() < self.params.min_size as usize * 4) ||
106-
(diff < self.params.deviation / 4.0) {
107-
self.merge_into(myselfi, votes[0].0);
101+
if !votes.is_empty() {
102+
let diff = votes[0].1 as f64 / 10000.0;
103+
if (myself.area() < self.params.min_size as usize / 16) ||
104+
(diff < self.params.deviation && myself.area() < self.params.min_size as usize) ||
105+
(diff < self.params.deviation * 2.0 && myself.area() < self.params.min_size as usize / 4) ||
106+
(diff < self.params.deviation / 2.0 && myself.area() < self.params.min_size as usize * 4) ||
107+
(diff < self.params.deviation / 4.0) {
108+
self.merge_into(myselfi, votes[0].0);
109+
}
108110
}
109111
}
110112
self.counter += 1;
@@ -143,7 +145,24 @@ impl Processor {
143145
}
144146

145147
fn color_distance(myself: &Aggregate, other: &Aggregate) -> i32 {
146-
(10000.0 * super::segmentation::Processor::color_diff_hsv(myself.color, other.color)) as i32
148+
let mycolor = myself.color;
149+
let otcolor = other.color;
150+
(10000.0 * Self::color_diff_hsv(mycolor, otcolor)) as i32
151+
}
152+
153+
fn color_diff_hsv(a: Color, b: Color) -> f64 {
154+
let a = a.to_hsv();
155+
let b = b.to_hsv();
156+
return 1.5 * wrap(a.h, b.h) + 1.25 * (a.v - b.v).abs() + 0.75 * (a.s - b.s).abs();
157+
158+
fn wrap(x: f64, y: f64) -> f64 {
159+
let d = (x - y).abs();
160+
if d < 0.5 {
161+
d
162+
} else {
163+
1.0 - d
164+
}
165+
}
147166
}
148167

149168
fn get_agg(&self, index: AggregateIndex) -> &Aggregate {

visionmagic/src/segmentation.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,13 @@ impl ProcessorTrait for Processor {
110110
}
111111

112112
impl Processor {
113-
pub fn color_distance(myself: &Cluster, other: &Cluster) -> i32 {
113+
fn color_distance(myself: &Cluster, other: &Cluster) -> i32 {
114114
let mycolor = myself.residue_color();
115115
let otcolor = other.residue_color();
116116
(10000.0 * Self::color_diff_hsv(mycolor, otcolor)) as i32
117117
}
118118

119-
pub fn color_diff_hsv(a: Color, b: Color) -> f64 {
119+
fn color_diff_hsv(a: Color, b: Color) -> f64 {
120120
let a = a.to_hsv();
121121
let b = b.to_hsv();
122122
return 2.0 * wrap(a.h, b.h) + (a.s - b.s).abs() + (a.v - b.v).abs();

0 commit comments

Comments
 (0)