Finding Maximum Difference Between Element and All Neighboring Elements in An Arary
3 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Hello, I have an array and I am trying to find the maximum difference between an element and all of its neighboring elements for each element. For example if I have... array = [1 2 3; 4 10 5; 6 7 8 ]; I want to find the maximum difference between each element and any neighboring element to get an output with the maximum difference
output = [3 8 7; 6 9 5; 4 3 3]; Any help would be greatly appreciated!
0 Commenti
Risposta accettata
John D'Errico
il 24 Ago 2017
Modificato: John D'Errico
il 24 Ago 2017
If all you want are horizontal and vertical comparisons, then this too easy. Essentially two calls to diff, then a few max calls.
A = [1 2 3; 4 10 5; 6 7 8 ];
[n,m] = size(A);
D1 = abs(diff(A,[],1));
D2 = abs(diff(A,[],2));
D = max(max([zeros(1,m);D1],[D1;zeros(1,m)]),max([zeros(n,1),D2],[D2,zeros(n,1)]));
That yields D as
D
D =
3 8 2
6 8 5
2 3 3
So it looks vaguely like you want to consider diagonal neighbors also. This too would be doable, using a similar scheme. (It can also be done using conv2, as a nice trick.) But then the result that you show is incorrect.
The (1,1) element of A is 1. It has three neighbors, thus {2,4,10}. So the max difference would then be 9, NOT 3.
So unless you can CLEARLY define what you are looking for, giving a correct example, I cannot help you more.
2 Commenti
John D'Errico
il 24 Ago 2017
The code that I showed yields the max of horizontal and vertical differences.
I won't do all of it for you, since you need to understand how this works. But I'll do the tricky part, that which uses conv2.
D3 = abs(conv2(A,[1 0;0 -1]));
D3 = D3(2:end-1,2:end-1)
D3 =
9 3
3 2
D4 = abs(conv2(A,[0 1;-1 0]));
D4 = D4(2:end-1,2:end-1)
D4 =
2 7
4 2
But consider how the code worked that I gave you. Now, can you extend that code, using max again? You will need 4 more max operations.
Essentially, you would shift D3 and D4 in the appropriate directions each time, by appending zeros in each case. Or, you could do it like this:
D(1:end-1,1:end-1) = max(D(1:end-1,1:end-1) , D3);
D(2:end,2:end) = max(D(2:end,2:end) , D3);
Now do the last two parts, using D4.
Più risposte (2)
Image Analyst
il 24 Ago 2017
If you have the Image Processing Toolbox, the best way is to simply call rangefilt():
outputImage = rangefilt(inputImage, ones(3))
Alternatively, you can just subtract the erosion (local min) from the dilation (local max):
outputImage = imdilate(inputImage, ones(3)) - imerode(inputImage, ones(3));
which should do the same thing as rangefilt().
0 Commenti
Walter Roberson
il 24 Ago 2017
Morphological closing can be used to take the maximum of each block in a sliding block manner. Do that and subtract the original value to get the difference between the value and the local maximum. Now do morphological open which is equivalent to local minimum and subtract that from the original to get the difference in the other direction. Now take the maximum of the two to determine the greatest difference.
1 Commento
Image Analyst
il 25 Ago 2017
Some definitions:
A morphological dilation (not closing) is the local max.
A morphological erosion (not opening) is a local min.
A morphological closing is a dilation followed by an erosion, which enlarges and smooths out bright areas.
A morphological opening is an erosion followed by an dilation, which enlarges dark areas.
A closing minus the original is a top hat filter, which finds bright things on a varying background.
An opening minus the original is a bottom hat filter, which finds dark things on a varying background.
There are functions for all of those in the Image Processing Toolbox: imerode(), imdilate(), imclose(), imopen(), imtophat(), imbothat().
Vedere anche
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!