<jupyter_start><jupyter_text>Init function<jupyter_code>def getGradient(gray, x = 0, y = 0, useGradient = True):
    if useGradient:
        grad = cv2.Sobel(gray, ddepth=cv2.CV_32F, dx=x, dy=y, ksize=3)

        '''
            take absolute value of gradient to use negative gradient
        '''
        grad = np.absolute(grad)

        '''
            Normalization of gradient
        '''
        (minVal, maxVal) = (np.min(grad), np.max(grad)) 
        if maxVal - minVal > 0:
            grad = (255 * ((grad - minVal) / float(maxVal - minVal))).astype("uint8")
        else:
            grad  = np.zeros(gray.shape, dtype = "uint8")

    else:
        grad = cv2.adaptiveThreshold(   gray,
                                        255,
                                        cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
                                        cv2.THRESH_BINARY_INV,
                                        11,
                                        2)

    return grad<jupyter_output><empty_output><jupyter_text>Process image<jupyter_code>img = cv2.imread('assets/1.jpg', 0)
vis = cv2.imread('assets/1.jpg')
fig, ax = plt.subplots(figsize=(10, 10))
ax.imshow(img, cmap='gray')
plt.show()
bin_img = getGradient(img, x = 1, useGradient = True)
fig, ax = plt.subplots(figsize=(15, 15))
ax.imshow(bin_img, cmap='gray')
plt.show()
verp = np.sum(bin_img, axis=1) / 255
drawedverp = tl.getDrawProjectionVer(img, verp)
bigImg1 = tl.concat_hor((img, drawedverp))
fig, ax = plt.subplots(figsize=(15, 15))
ax.imshow(bigImg1, cmap='gray')
plt.show()
bigImg1 = tl.concat_hor((bin_img, img))
fig, ax = plt.subplots(figsize=(15, 15))
ax.imshow(bigImg1, cmap='gray')
plt.show()<jupyter_output><empty_output><jupyter_text>Find filter size<jupyter_code>half = int(np.max(verp) / 2)
sliceLine = drawedverp[:,(half-1):(half+1)]
contours, hierarchy = cv2.findContours(cv2.cvtColor(   sliceLine, 
                                                       cv2.COLOR_BGR2GRAY), 
                                                       cv2.RETR_EXTERNAL, 
                                                       cv2.CHAIN_APPROX_SIMPLE)
print(half)
heights = []
for cnt in contours:
    x,y,w,h = cv2.boundingRect(cnt)
    heights.append(h)
medianHeight = int(np.median(np.asarray(heights)) * 1.5)
print("medianHeight", medianHeight)
drawedverp = cv2.line(drawedverp, 
                      (half,0), 
                      (half,drawedverp.shape[0]), 
                      (0,0,255), 
                      1)<jupyter_output><empty_output><jupyter_text>Convolve peaks<jupyter_code>kernel = medianHeight
verpConvolved = np.convolve(verp, np.ones((kernel,))/kernel, mode='same')
drawedverpconv = tl.getDrawProjectionVer(img, verpConvolved)<jupyter_output><empty_output><jupyter_text>Find peaks<jupyter_code>bandP1ranges = []
peaks = []
c1 = 0.2
c2 = 0.3
while np.max(verpConvolved) > 40:
    ybm = np.argmax(verpConvolved)

    yb0 = tl.findb0(verpConvolved, 
                    ybm, 
                    c1 * verpConvolved[ybm])
    yb1 = tl.findb1(verpConvolved, 
                    ybm, 
                    c2 * verpConvolved[ybm])

    if yb1 - yb0 > medianHeight:
        bandP1ranges.append((yb0,yb1))
        peaks.append((int(verpConvolved[ybm]), ybm))

    verpConvolved[yb0:yb1] = 0
# draw peaks
for peak in peaks:
    cv2.circle(drawedverpconv, peak, 5, (255,0,0), -1)
# draw bands
bandsImg = np.zeros(vis.shape, dtype = np.uint8)
for band in bandP1ranges:
    yt, yb = band
    bandsImg[yt:yb] = [0,255,0]
vis = cv2.addWeighted(vis, 0.6, bandsImg, 0.4, 0)
drawedverpconv = cv2.addWeighted(drawedverpconv, 0.6, bandsImg[:, 0:len(drawedverpconv[0]), ...], 0.4, 0)
bigImg1 = tl.concat_hor((vis, bin_img, drawedverp, drawedverpconv))
fig, ax = plt.subplots(figsize=(20, 20))
ax.imshow(bigImg1, cmap='gray')
plt.show()<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup