-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomphist.m
executable file
·55 lines (55 loc) · 2.84 KB
/
comphist.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
function out = comphist(image, xsize, ysize, s, ref, ref2)
% COMPHIST Compute a fixed-window transform of an image into a competitive
% histogram score
% IMAGE is the image to be scored
% XSIZE is the width of the sliding window in pixels
% YSIZE is the height of the sliding window in pixels
% S is the resolution in window position, in pixels. Higher values
% correspond to a lower-resolution scan and less accurate results
% REF is a 32*32*32 hsv normalized histogram generated by hist3dhsv() on
% the set of pixels formed from instances of the target class.
% REF2 is the corresponding normalized histogram for pixels that are NOT
% part of target class instances
%
% OUT is a grayscale image of the same dimensions as IMAGE, with each
% pixel containing a score from the comparison of REF and REF2 across
% all passes. Positive scores mean the pixel is more likely to belong
% to class REF, while negative scores mean the pixel is more likely
% to belong to class REF2.
% Note: many combinations of XSIZE, YSIZE, and S mean that pixels at the
% bottom or very right of the image are not captured by any scan.
% These pixels are set in OUT to a highly negative value, i.e. one
% indicating non-membership in REF.
dims = size(image);
out = zeros(dims(1), dims(2));
nums = zeros(dims(1),dims(2));
%window starts at 1 and ends when window reaches bottom if image
for x1 = 1:s:(dims(1) - xsize + 1)
%window starts at 1 and ends when window reaches right of image
for y1 = 1:s:(dims(2) - ysize + 1)
%calculate x and y ends of window. 4 wide means add 3 etc
x2 = x1 + (xsize - 1);
y2 = y1 + (ysize - 1);
%calculate 3d hsv histogram for this window
data = hist3dhsv(double(image(x1:x2,y1:y2,:)) / 255);
%calculate squared difference of REF from window
scores1 = data - ref;
scores1 = scores1 .* scores1;
%calculate squared difference of REF2 from window
scores2 = data - ref2;
scores2 = scores2 .* scores2;
%out is updated, with every pixel in the window being
%decreased if the window deviates from REF, and increased
%if the window deviates from REF2
out(x1:x2,y1:y2) = out(x1:x2,y1:y2) - sum(scores1(:)) + sum(scores2(:));
%track of how many windows contributed to each pixel's score
nums(x1:x2,y1:y2) = nums(x1:x2,y1:y2) + 1;
end
end
%normalize score using number of windows for each pixel
out = out ./ nums;
%normalize so that largest value is 1.
out = out ./ max(max([out(:) -out(:)]))
%no windows results in nan. Set these far below 0 threshold.
out(isnan(out)) = -1;
end