如何在python中找到拐点?

kha*_*udj 6 python arrays opencv

我有一个 RGB 图像的直方图,它表示三个分量 R、G 和 B 的三个曲线。我想找到每条曲线的拐点。我使用二阶导数来找到它们,但我不能,二阶导数不会取消其返回空值。那么如何找到拐点呢?有没有其他方法可以找到它们?

在此处输入图片说明

import os, cv2, random
import numpy as np
import matplotlib.pyplot as plt
import math
from sympy import *

image = cv2.imread('C:/Users/Xers/Desktop/img.jpg')

CHANNELS = ['r', 'g', 'b']

for i, channel in enumerate( CHANNELS ):
    

  histogram = cv2.calcHist([image], [i], None, [256], [0,256])

  histogram = cv2.GaussianBlur( histogram, (5,5), 0)

  plt.plot(histogram, color = channel)

     
  x= plt.xlim([0,256])
  y = plt.ylim([0, 24000])


  derivative1= np.diff(histogram, axis=0)
  derivative2= np.diff(derivative1, axis=0)

  inf_point = np.where ( derivative2 == 0)[0]
  print(inf_point)
plt.show()
Run Code Online (Sandbox Code Playgroud)

nor*_*ok2 13

您的代码有两个数值性质的问题:

  • 数据似乎不够连续,无法依赖从两个后续np.diff()应用程序计算的二阶导数
  • 即使是这样,它的可能性0也非常小

为了解决第一点,您应该平滑您的直方图(例如,在直方图本身上使用统一或高斯滤波器)。

要解决第二点,不是寻找 ,而是寻找 == 0正到负(反之亦然)的转换点。


给你一些可能的方法的最小例子:

import numpy as np
import matplotlib.pyplot as plt
from scipy.ndimage import gaussian_filter1d


np.random.seed(0)

# generate noisy data
raw = np.cumsum(np.random.normal(5, 100, 1000))
raw /= np.max(raw)

# smooth
smooth = gaussian_filter1d(raw, 100)

# compute second derivative
smooth_d2 = np.gradient(np.gradient(smooth))

# find switching points
infls = np.where(np.diff(np.sign(smooth_d2)))[0]

# plot results
plt.plot(raw, label='Noisy Data')
plt.plot(smooth, label='Smoothed Data')
plt.plot(smooth_d2 / np.max(smooth_d2), label='Second Derivative (scaled)')
for i, infl in enumerate(infls, 1):
    plt.axvline(x=infl, color='k', label=f'Inflection Point {i}')
plt.legend(bbox_to_anchor=(1.55, 1.0))
Run Code Online (Sandbox Code Playgroud)

阴谋