Maximum difference between each Element and neighbors in large array
조회 수: 22 (최근 30일)
이전 댓글 표시
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?
댓글 수: 1
Walter Roberson
2017년 8월 25일
This is essentially the same question but a different poster for https://www.mathworks.com/matlabcentral/answers/353974-finding-maximum-difference-between-element-and-all-neighboring-elements-in-an-arary
채택된 답변
Image Analyst
2017년 8월 25일
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);
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/177967/image.png)
댓글 수: 2
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!