CEB
CEB

Reputation: 106

Difficulties to get a number of peak values from a 2-D array

I'm working on matrix calculation and get to know the different block of peak values marked "circle" in attached plot [e.g. 2*2 matrix] of the 2D array [as shown in Fig. 01]. Fig. 01: Peaks 2d array

The another form of representation of Fig. 01 look like as Fig. 02. Fig. 02: Plot of a 2D array

I visited the link Peak detection in a 2D array and use the following script

#NOTE: I get the code from the link above
from scipy import *
from operator import itemgetter  
n = 3  # how many fingers are we looking for
#d = loadtxt("paw.txt")
#data = [DATA of a 2d array]
width, height = data.shape
# Create an array where every element is a sum of 2x2 squares.
fourSums = data[:-1,:-1] + data[1:,:-1] + data[1:,1:] + data[:-1,1:]

# Find positions of the fingers.
# Pair each sum with its position number (from 0 to width*height-1),
pairs = zip(arange(width*height), fourSums.flatten())
# Sort by descending sum value, filter overlapping squares

def drop_overlapping(pairs):
    no_overlaps = []
    def does_not_overlap(p1, p2):
        i1, i2 = p1[0], p2[0]
        r1, col1 = i1 / (width-1), i1 % (width-1)
        r2, col2 = i2 / (width-1), i2 % (width-1)
        return (max(abs(r1-r2),abs(col1-col2)) >= 2)
    for p in pairs:
        if all(map(lambda prev: does_not_overlap(p,prev), no_overlaps)):
            no_overlaps.append(p)
    return no_overlaps

pairs2 = drop_overlapping(sorted(pairs, key=itemgetter(1), reverse=True))

# Take the first n with the heighest values
positions = pairs2[:n]

# Print results print(data, "\n")
for i, val in positions:
    row = i // (width-1)
    column = i % (width-1)
    print("sum = %f @ %d,%d (%d)" % (val, row, column, i))
    print(data[row:row+2,column:column+2])

The above script provides 2 adjacent matrix[2*2] of the same peaks.

## 01: Local matrix of the peak
sum = 0.002482 @ 128,190 (65342)
[[0.0006202  0.00062187]
 [0.00061926 0.00062093]]
## 02: Local matrix of the peak
sum = 0.002479 @ 128,191 (65343)
[[0.00062187 0.0006184 ]
 [0.00062093 0.00061746]]

Also, I've tried with varying 'n' and no. of rows and columns but it gives me different local matrix for the same peak (probably I missed something) which does not match with my expectation. I want to find local peaks as shown in figure. What modification do I need to do or is there any simple method exist to work with? Can anybody help me in this regard?

Upvotes: 1

Views: 476

Answers (1)

CEB
CEB

Reputation: 106

ok, I hope this could simply be done by using the "scikit-Image" module.

#data = 2-d array

If I want to find 'N' number of peaks or local maxima, I choose num_peaks='N' and peak_local_max will find the number of peaks.

peaks_position = peak_local_max(data, min_distance=20, num_peaks=2)

Problem: peak_local_max works fine showing the coordinates of the local maxima but can not return the 'VALUE' of the local maxima!!

Upvotes: 1

Related Questions