首页 > 解决方案 > 如何检测图中的峰值数量?

问题描述

我有一个代码可以识别绘图左侧 40% 侧(蓝线左侧)的边缘(峰值和底部)并将它们的幅度相加。问题:我希望代码也分别计算峰值和底部的数量。有什么帮助吗?

(例如代码适用的图像) 代码适用的图像

我的代码:

 # now we will get the sum of the peaks in the given image
# To do this we use opencv's cornerHarris function.
# this function gives the corners in an image which in our case is peaks.
# we get the coordinates of these peaks through the cornerHarris function, 
# and we subtract it from the coordinates of mid line in order to find the
# height of the peak. Then we add all these heights to get the sum of the 
# heights which is our feature for classifying the image.

def give_peak_sum(file):
    image = cv2.imread(file) # opencv's image read function
    image_copy = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) 
    # converts image from
    # BGR color space to RGB color space

    image_dims = image.shape
    x_dim = image_dims[1]

    # converting to gray scale
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    gray = np.float32(gray)

    # detect corners
    dst = cv2.cornerHarris(gray, 2, 3, 0.04)

    # dilate corner image to enhance corner points
    dst = cv2.dilate(dst, None)

    thresh = 0.02*dst.max()

    peak_sum = 0
    mid_line = get_mid_line(image_copy) # using previously defined function

    for j in range(0, dst.shape[0]):
        for i in range(0, dst.shape[1]):
            if (dst[j, i] > thresh and i < 0.4*x_dim):
                peak_sum += abs(j-mid_line)

    return (peak_sum)

标签: pythonplotimage-recognition

解决方案


推荐阅读