Muhammad Ibtihaj Tahir
Muhammad Ibtihaj Tahir

Reputation: 636

Global and Local Maximas of the histogram

I'm trying to segment the football field. I'm following a research paper that suggests

  1. Convert to HSV
  2. Grab the Hue Channel
  3. Generate Histogram
  4. Grab Global and local maxima (Global max can be max(hist))
  5. If a local maxima is of 20% of the Global Maxima, Consider it

enter image description here

The issue is that I don't know how to get Local Maximas. I'm trying to capture a list of peaks in the histogram.

I have tried it on MatLab, it works quite well but I need to do it in python. I have tried libraries like peakutils but nothing is giving me desired results.

def field_area_mask(image):

    # Convert to HSV
    hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)

    # Capture the Hue Channel
    hue = hsv[:, :, 0]

    # Generate Histogram
    hist = cv2.calcHist([hue],[0],None,[256],[0,256])

    # Capture range
    hist_range = hist[:121] # 0-120
    hist_range = hist_range.reshape(1, -1)[0]
    Hmax = max(hist_range)
    plt.plot(hist_range)

    HiMax = [] # Need to populate the local maxima's list

Upvotes: 0

Views: 822

Answers (2)

Muhammad Ibtihaj Tahir
Muhammad Ibtihaj Tahir

Reputation: 636

I was able to find the peaks using the scipy.signal's method argrelextrema. I used it with np.greater.

peaks = argrelextrema(hist_range, np.greater)

enter image description here

Upvotes: 1

baci
baci

Reputation: 2588

See numpy.diff

From 1st derivative you can find stationary points. From 2nd derivative you can tell if these points are local max or min.

Example: given your histogram

[1, 2, 1, 3, 7, 10, 7, 2] // 0-based index

1st derivative is

[1, -1, 2, 4, 3, -3, -5] // 1-based index

sign changes at 2nd, 3rd and 6th element.

2nd derivative is

[-2, 3, 2, -1, -6, -2] // 2-based index

sign for 2nd element is (-), local max

sign for 3rd element is (+), local min

sign for 6th element is (-), local max

global max is 10, so your threshold is %20 * 10 = 2. 2nd element shall be accepted as a local max.

Upvotes: 1

Related Questions