python-3.xmatplotliboptimizationminima

How to calculate the value of the point of minima in the following graph using python


I am trying to find the value of the point of the minima(local and global doesn't matter as there is only one minima in the first graph) in the first graph.How to do that.Point of minima is marked in red. First graph is the smoothened out version of the second graph to prevent the issue of local minimas.

enter image description here

I obtained graph using following steps-

import cv2
from matplotlib import pyplot as plt
green = cv2.imread('5.tiff',1)
a = cv2.calcHist([green],[0],None,[256],[0,256])
blurs = cv2.GaussianBlur(a,(13,13),0)
plt.subplot(2,1,1)
plt.plot(blurs)
plt.subplot(2,1,2)
plt.plot(a)

Solution

  • Basically you can define a local minima as a point where you can go neither left or right without increasing your value. Let me demonstrate this with the help of cos() graph

    import matplotlib.pyplot as plt
    import numpy as np
    
    x = np.arange(1000)
    y = np.cos(x * np.pi / 180)
    plt.plot(x, y)
    

    enter image description here

    The values are stored in the y variable. At every index (except first and last), just check the 2 neighboring values, if both values are bigger then you are currently at a local minima. Here is the code :

    local_min = []
    for i in range(1, len(y)-1):
        if y[i-1] >= y[i] and y[i] <= y[i+1]:
            local_min.append(i)
    print(local_min)
    

    Output:

    [180, 540, 900]