查找信号峰值不正确
Finding peaks of signals are not correct
我有以下图片:
我正在使用希尔伯特变换获取包络线,并试图找到峰值。
使用以下代码我得到以下错误的峰值检测。
基本上我试图根据包络线和峰值来分割字母...但我现在得到了错误的峰值。
def normalize(v):
norm = np.linalg.norm(v)
if norm == 0:
return v
return v / norm
'''
reduces the photo to a vector representing its pixel freuqeuncy at each column
'''
def image_reduce(img):
col_counts = cv2.reduce(img, 0, cv2.REDUCE_SUM, dtype=cv2.CV_32SC1)
column = col_counts.flatten().tolist()
# print("Column counts:\n\n", column)
return column
def slice_digits(image_name):
img = cv2.imread(image_name, 0)
column_frequency = image_reduce(cv2.bitwise_not(img))
column_frequency = normalize(column_frequency)
env = np.abs(sigtool.hilbert(column_frequency))
peaks, _ = find_peaks(env > 0.1, height= 0.51)
plt.plot(env)
plt.scatter(peaks, env[peaks], s=50, c='r')
all_slices = []
for i in range(len(peaks) - 1):
x0, x1 = peaks[i:i + 2]
image_slice = img[:, x0:x1]
print("coords:", x0, x1)
# Now do something with the slice, e.g.
all_slices.append(image_slice)
plt.figure("Slice %d)" % i)
plt.imshow(image_slice)
plt.show()
if __name__ == '__main__':
image = r"c:\ahmed\doc.png"
res_image = slice_digits(image)
只需替换行:
peaks, _ = find_peaks(env > 0.1, height= 0.51)
来自
peaks, _ = find_peaks(env, height = 0.1, width = 4)
给予
我建议您花时间阅读您使用的函数的文档。例如,这里设置 height = 0.51
是无稽之谈,因为高度是:
Required height of peaks. Either a number, None, an array matching x
or a 2-element sequence of the former. The first element is always
interpreted as the minimal and the second, if supplied, as the maximal
required height.
但是,请记住,如果您的信号差异太大,则很难创建可靠的 'calibration' 函数来找到峰值。例如,参数宽度在这里设置为 4,但我建议您尝试所有参数,看看它们的作用。
最后,这总是一个品味问题。左边的第一张照片是双人的。你怎么知道最大值实际上是你想要的?在信号中找到可靠的峰值很棘手。
我有以下图片:
我正在使用希尔伯特变换获取包络线,并试图找到峰值。
使用以下代码我得到以下错误的峰值检测。
基本上我试图根据包络线和峰值来分割字母...但我现在得到了错误的峰值。
def normalize(v):
norm = np.linalg.norm(v)
if norm == 0:
return v
return v / norm
'''
reduces the photo to a vector representing its pixel freuqeuncy at each column
'''
def image_reduce(img):
col_counts = cv2.reduce(img, 0, cv2.REDUCE_SUM, dtype=cv2.CV_32SC1)
column = col_counts.flatten().tolist()
# print("Column counts:\n\n", column)
return column
def slice_digits(image_name):
img = cv2.imread(image_name, 0)
column_frequency = image_reduce(cv2.bitwise_not(img))
column_frequency = normalize(column_frequency)
env = np.abs(sigtool.hilbert(column_frequency))
peaks, _ = find_peaks(env > 0.1, height= 0.51)
plt.plot(env)
plt.scatter(peaks, env[peaks], s=50, c='r')
all_slices = []
for i in range(len(peaks) - 1):
x0, x1 = peaks[i:i + 2]
image_slice = img[:, x0:x1]
print("coords:", x0, x1)
# Now do something with the slice, e.g.
all_slices.append(image_slice)
plt.figure("Slice %d)" % i)
plt.imshow(image_slice)
plt.show()
if __name__ == '__main__':
image = r"c:\ahmed\doc.png"
res_image = slice_digits(image)
只需替换行:
peaks, _ = find_peaks(env > 0.1, height= 0.51)
来自
peaks, _ = find_peaks(env, height = 0.1, width = 4)
给予
我建议您花时间阅读您使用的函数的文档。例如,这里设置 height = 0.51
是无稽之谈,因为高度是:
Required height of peaks. Either a number, None, an array matching x or a 2-element sequence of the former. The first element is always interpreted as the minimal and the second, if supplied, as the maximal required height.
但是,请记住,如果您的信号差异太大,则很难创建可靠的 'calibration' 函数来找到峰值。例如,参数宽度在这里设置为 4,但我建议您尝试所有参数,看看它们的作用。
最后,这总是一个品味问题。左边的第一张照片是双人的。你怎么知道最大值实际上是你想要的?在信号中找到可靠的峰值很棘手。