我不确定这是多么普遍,但你可以使用 numpy argmax 得到最大的位置(如 这个 )在你的情况下。你应该避免循环,因为这将非常慢,更好地使用numpy函数。我导入了你的图像并使用了黄色通道中200或更多栏杆的截止标准,
numpy
argmax
import cv2 import numpy as np import matplotlib.pyplot as plt #This loads the canny image you uploaded image = cv2.imread('uojHJ.jpg') #Trim off the top taskbar trimimage = image[100:, :,0] #Use argmax with 200 cutoff colour in one channel maxindex = np.argmax(trimimage[:,:]>200, axis=0) #Plot graph plt.plot(trimimage.shape[0] - maxindex) plt.show()
这看起来如下: