我有以下使用hilbert变换进行的峰检测,该峰获取信号的包络,然后从其中检测出峰。
我想从峰1到峰2,从峰2到峰3,从峰3到峰4切掉那些峰,依此类推。
这是示例图片:
那是过程的结果
那是我的代码:
import cv2
import numpy as np
from PIL import Image
from scipy import signal
from math import factorial
from matplotlib import pyplot as plt
from scipy.signal import savgol_filter
import scipy.signal.signaltools as sigtool
from sklearn.preprocessing import normalize
from scipy.signal import find_peaks, peak_widths, find_peaks_cwt
from scipy.signal import argrelextrema
# ---------------------------------------Functions---------------------------------------------------#
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 histogram_plot(convluted_word, peaks, fit):
plt.plot(convluted_word)
plt.plot(fit, 'r')
plt.plot(peaks, fit[peaks], "x")
plt.show()
# ---------------------------------------Main Code Flow---------------------------------------------------#
# ---------------------------an example on how to use this package----------------------------------------#
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))
square_sig = (env > 0.1)
square_sig = square_sig.astype(float)
square_sig = np.divide(square_sig, 15.0)
square_sig = np.where((column_frequency > 0), 0.1, 0)
peaks, _ = find_peaks(env > 0.1)
plt.plot(env)
plt.scatter(peaks, env[peaks], s = 50, c = 'r')
edges = np.nonzero(np.diff(square_sig))[0]
plt.scatter(edges, env[edges], c = 'g')
plt.show()
all_slices = []
for i in range(len(peaks) - 1):
x0, x1 = peaks[i:i + 2]
image_slice = img[x0:x1]
# Now do something with the slice, e.g.
cv2.imshow("slice",image_slice)
all_slices.append(image_slice)
# used for debugging
#histogram_plot(column_frequency, peaks, square_sig)
# segements the picture
#listt, image_final = char_slicer(edges, img)
plt.show()
# display result
#return image_final
if __name__ == '__main__':
image = r"c:\ahmed\doc.png"
res_image = slice_digits(image)
我想要的是这样的
答案 0 :(得分:0)
要从列表中peaks
中的坐标中切出图像,可以使用:
all_slices = []
for i in range(len(peaks)-1):
x0, x1 = peaks[i:i+2]
image_slice = img[:, x0:x1]
# Now do something with the slice, e.g.
all_slices.append(image_slice)
对于您的具体情况,完整列表为
import cv2
import numpy as np
import matplotlib.pyplot as plt
import scipy.signal.signaltools as sigtool
from scipy.signal import find_peaks, peak_widths, find_peaks_cwt
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 normalize(v):
norm = np.linalg.norm(v)
if norm == 0:
return v
return v / norm
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))
square_sig = (env > 0.1)
square_sig = square_sig.astype(float)
square_sig = np.divide(square_sig, 15.0)
square_sig = np.where((column_frequency > 0), 0.1, 0)
peaks, _ = find_peaks(env > 0.1)
plt.plot(env)
plt.scatter(peaks, env[peaks], s=50, c='r')
edges = np.nonzero(np.diff(square_sig))[0]
plt.scatter(edges, env[edges], c='g')
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)