I have an projection profile for an image. It looks like this
.
And the code looks like this
def smooth(y, box_pts):
box = np.ones(box_pts)/box_pts
y_smooth = np.convolve(y, box, mode='same')
return y_smooth
img = cv2.imread(filename)
# RGB-GRAY Conversion
gray = rgb2gray(img)
# Gray to Binary Conversion Ostru Thresholding with Gaussian
blur = cv2.GaussianBlur(gray,(5,5),0)
ret3,thres = cv2.threshold(blur,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
#_,thres= cv2.threshold(gray,140,255,cv2.THRESH_BINARY)
#Counting black pixels per row
counts = np.sum(thres==0, axis=1)
row_number = [i for i in range(thres.shape[0])]
counts = smooth(counts,19)
plt.plot(row_number, counts, label='fit')
plt.xlabel('Row Number')
plt.ylabel('Number of Black Pixels')
plt.title('Horizontal Projection Profile')
I want to find row number for separating different lines. The desired row_number are [0, 227, 381, 547, 687]. I tried finding extrema or local minima using scipy.signal.argrelextrema() But I am also getting other (not desired) minima. So my question is - Is there any possible technique so that I can find these line separator row number?
P.S. - Setting a threshold does not seem to work because for new image projection profile might change so the threshold is.
Any help would be highly appreciated!!!