Clear Filters
Clear Filters

Maximum difference between each Element and neighbors in large array

22 views (last 30 days)
I have an array (1024x1024). For each element, I want to find the difference between that element and all 8 neighboring elements. From there, I want to take the max difference and write it to a new array in that element's position.
For example, for array = [40 50 130; 20 10 30; 100 90 80] the output would be 120 in location (2,2)- maximum difference between the middle element (10) and all outer elements is 120 (130-10). This needs to be run through all elements in the 1024x1024 array. I'm not sure how to do this.
For each "center" element, I was attempting to identify neighbors (codes like "neighbor1 = array(middleElement,middleElement+1") and then subtract all 8 neighbors from the center element. I am running into some key issues: --elements on the corners/edges will need different codes because they don't have 8 neighbors;is there a way around this? --I can assign neighbors in a 3x3 by defining the location of the middle element and adding (-1, 0, 1) to the middle element's (x,y) location. I do not know the most efficient way to assign neighbors through all 1024x1024 elements.
Could anyone please help me with these issues?

Accepted Answer

Image Analyst
Image Analyst on 25 Aug 2017
Try this:
grayImage = imread('moon.tif');
subplot(1, 2, 1);
imshow(grayImage, []);
title('Original Image', 'FontSize', 20);
% Find local mins and maxes within sliding window.
localMaxImage = imdilate(grayImage, ones(3));
localMinImage = imerode(grayImage, ones(3));
% Find max intensity difference from center to brighter and darker images.
brighter = localMaxImage - grayImage;
darker = grayImage - localMinImage;
% Find the max difference regardless whether brighter or darker.
maxDiff = max(brighter, darker);
subplot(1, 2, 2);
imshow(maxDiff, []);
title('Max Diff Image', 'FontSize', 20);
  2 Comments
Emily Pendleton
Emily Pendleton on 25 Aug 2017
Thank you so much! This is exactly what I needed. My next question is can this same code be applied to a 3-d array (1024x1024x30)? I want to take into account the neighboring 26 elements for each element and write the max difference to a new 3-d array.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!