<jupyter_start><jupyter_text>Try Skeletonize<jupyter_code>def excise_segmentation(image):
    
    number = image.copy()[78:78+1166, 1276:1276+392]
    serial = image.copy()[2088:2088+448, 1276:1276+292]
   
    clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))
    
    # Serial
    hsv_image = cv2.cvtColor(serial, cv2.COLOR_RGB2HSV)
    h, s, v = cv2.split(hsv_image)
    v = clahe.apply(v)
    blurred = cv2.bilateralFilter(v, 9, 75, 75)
    blurred = cv2.medianBlur(blurred, 5)
    serial_thresholded = cv2.adaptiveThreshold(blurred, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 255, 1)
    
    edged = cv2.Canny(serial_thresholded, 30, 200)
    refCnts = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[1]
    serial_contours = np.dstack([serial_thresholded.copy()] * 3)
    for c in refCnts:
        (x, y, w, h) = cv2.boundingRect(c)
        cv2.rectangle(serial_contours, (x, y), (x + w, y + h), (0, 255, 0), 2)

    # Number
    hsv_image = cv2.cvtColor(number, cv2.COLOR_RGB2HSV)
    h, s, v = cv2.split(hsv_image)
    v = clahe.apply(v)
    blurred = cv2.bilateralFilter(v, 9, 75, 75)
    blurred = cv2.medianBlur(blurred, 5)
    number_thresholded = cv2.adaptiveThreshold(blurred, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 195, 1)
    
    # Binarize
    number_thresholded_canny = number_thresholded.copy()
    number_thresholded = invert(number_thresholded)
    number_thresholded[number_thresholded == 255] = 1
    number_thresholded = np.array(255 * skeletonize(number_thresholded), dtype = np.uint8)
    
    edged = cv2.Canny(number_thresholded_canny, 30, 200)
    refCnts = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[1]
    number_contours = np.dstack([number_thresholded_canny.copy()] * 3)
    for c in refCnts:
        (x, y, w, h) = cv2.boundingRect(c)
        cv2.rectangle(number_contours, (x, y), (x + w, y + h), (0, 255, 0), 2)

    # Barcode
    barcode_thresholded = image.copy()[880:880+1408, 828:828+868]
    barcode = imutils.resize(image.copy()[880:880+1408, 828:828+868], height=500)
    hsv_image = cv2.cvtColor(barcode, cv2.COLOR_RGB2HSV)
    h, s, v = cv2.split(hsv_image)
    gray = clahe.apply(v)
    gradX = cv2.Sobel(gray, ddepth = cv2.CV_32F, dx = 1, dy = 0, ksize = -1)
    gradY = cv2.Sobel(gray, ddepth = cv2.CV_32F, dx = 0, dy = 1, ksize = -1)
    gradient = cv2.subtract(gradX, gradY)
    gradient = cv2.convertScaleAbs(gradient)
    blurred = cv2.medianBlur(gradient, 11)
    (_, thresh) = cv2.threshold(blurred, 225, 255, cv2.THRESH_BINARY)
    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (21, 7))
    closed = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)
    morph = cv2.erode(closed, None, iterations = 11)
    cnts = cv2.findContours(morph.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[1]
    if len(cnts):
        c = sorted(cnts, key = cv2.contourArea, reverse = True)[0]
        (x, y, w, h) = cv2.boundingRect(c)
        # PDF417 correction
        x = x - 10 if (x > 10) else 0
        w = w + 20 if (w < 868 - x - 20) else 868 - x
        # Top and bottom black line in PDF417
        y = y - (int)(0.3 * h) if (y > 0.3 * h) else 0
        h = h + (int)(0.5 * h) if (h < 1408 - y - (int)(0.6 * h)) else 1408 - y        
        hsv_image = cv2.cvtColor(barcode[y:y+h, x:x+w], cv2.COLOR_RGB2HSV)
        h, s, v = cv2.split(hsv_image)
        gray = clahe.apply(v)
        blured = cv2.medianBlur(gray, 3)
        barcode_thresholded = cv2.adaptiveThreshold(blured, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 31, 1)  
    
    # return the segmentationed images
    return serial_thresholded, number_thresholded, barcode_thresholded
    # return serial_contours, number_contours, barcode_thresholded
    # return serial_thresholded, number_contours, barcode_thresholded

# excise_src = cv2.imread("data/premasked/IMG_0676.JPG")
excise_src = cv2.imread("data/premasked/IMG_0549.JPG")
# excise_src = cv2.imread("data/premasked/IMG_0560.JPG")
# excise_src = cv2.imread("data/premasked/IMG_0562.JPG")
# excise_src = cv2.imread("data/premasked/IMG_0595.JPG")
filescount = 1
current_file_index = 0
fig = plt.figure(figsize=(10, 200))
serial_thresholded, number_thresholded, barcode_thresholded = excise_segmentation(excise_src)

build_segmentation_row(filescount, current_file_index, fig, excise_src, serial_thresholded, number_thresholded, barcode_thresholded)
# build_contoured_row(filescount, current_file_index, fig, excise_src, serial_thresholded, number_thresholded, barcode_thresholded)

plt.show()<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup