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
extern crate imgref;
mod integral;
use std::cmp::min;
use integral::*;
pub use imgref::{ImgRef, Img};
pub fn maximum_symmetric_surround_saliency(image: ImgRef<u8>) -> Img<Vec<u16>> {
let integral_img = integral_image(image);
let (width, height) = (image.width, image.height);
let stride = image.stride as u32;
let mut sal_map = Vec::with_capacity(width as usize * height as usize);
for y in 0..height {
let y_size = min(y, height - y);
let y1 = y.saturating_sub(y_size);
let y2 = min(y + y_size, height - 1);
for x in 0..width {
let x_size = min(x, width - x);
let x1 = x.saturating_sub(x_size);
let x2 = min(x + x_size, width - 1);
let area = (x2 - x1 + 1) * (y2 - y1 + 1);
let avg = (integral_img.integral_sum(x1, y1, x2, y2) / area as u32) as u8;
let diff = (avg as i16 - image.buf[(y*stride + x) as usize] as i16) as i32;
sal_map.push((diff*diff) as u16);
}
}
return Img::new(sal_map, width as usize, height as usize);
}