Scan exactly 1 pixel around a location and determine max value
Show older comments
If I have a list of coordinates xf,yf identifying a bright object as in the image (red dots only)

[xf,yf] =
4 10
7 5
7 15
10 10
13 5
13 15
16 10
It doesn't always correspond to the max intensity of the object. How do I go about scanning round the red dot by 1 pixel and then return the max value. I would need to keep the same order as the original xf,yf data.
Accepted Answer
More Answers (1)
There are many ways you could do this (e.g using loops). Here is a possible solution using bsxfun:
imagewidth = 100; %for e.g. required for bounds checks
imageheight = 200; %for e.g. required for bounds checks
img = randi(256, imageheight, imagewidth) - 1; %for e.g.
xy = [
4 10
7 5
7 15
10 10
13 5
13 15
16 10]; %xy = [xf yf]
%create cartesian product of offsets [-1 0 1] in both x and y direction
[dx, dy] = ndgrid(-1:1);
%create 3d matrix of pixel coordinates by using offsets dx and dy
pixels = bsxfun(@plus, xy, reshape([dx, dy], 1, 2, []));
%each (:, :, k) matrix correspond to the xy matrix shifted by [dx(k), dy(k)]
%make sure that pixels are within image bounds:
pixels = max(pixels, 1);
pixels = min(pixels, repmat([imagewidth, imageheight], size(xy, 1), 1, numel(dx)));
%get maximum along third dimension of image pixels indexed by pixels:
maxpix = max(img(pixels(:, [2 1], :)), [], 3)
Categories
Find more on Image Processing Toolbox in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!