Scan exactly 1 pixel around a location and determine max value

1 view (last 30 days)
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

Image Analyst
Image Analyst on 22 Jan 2016
Edited: Image Analyst on 22 Jan 2016
Call imdilate() which is a function that identifies the local max in a window:
maxImage = imdilate(grayImage, true(3));
To find the max at any particular (row, column) location from your list of red spots (in 1-D arrays called "xf" and "yf"), do
for k = 1 : length(xf)
thisRow = yf(k);
thisCol = xf(k);
fprintf('The Max in the 3x3 neighborhood of row=%d, col=%d is %d\n',...
thisRow , thisCol, maxImage(thisRow, thisCol));
end

More Answers (1)

Guillaume
Guillaume on 22 Jan 2016
Edited: Guillaume on 22 Jan 2016
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)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!