I am trying to implement an algorithm that detects regions in an image, i.e. starting from a seed pixel, neighboring pixels should be added to the region, highest values first.
I am looking for a data structure to serve as a queue to which the neighboring pixels can be added. It should be able to
- index into a matrix (the image), to find the highest-value pixel
- remove points efficiently
- add an element, if not already present
Here is what I have come up with
% given:
% I image (2d matrix)
% x, y seed point
list = [];
region = zeros(size(I));
while [...]
% Add all neighbors of (x, y) to list
if x > 1
% neighboring pixel to the left
ind = sub2ind(size(I), x - 1, y);
if length(find(list == ind)) == 0
list(end+1) = ind;
end
end
% similarly for all other neighbors
% [...]
% Find max in I among points in list
[dummy, list_max_i] = max(I(list));
max_i = list(list_max_i);
[x, y] = ind2sub(size(I), max_i);
% remove from list
list(list_max_i) = [];
region(x, y) = 1;
end
But I would like to replace list
by a data structure, which is better suited for adding and removing elements. Any ideas?