Untitled

 avatar
unknown
plain_text
a year ago
1.7 kB
5
Indexable
def process_fingerprint(img_path, save_dir, img_name):
    # Load the image
    image = cv2.imread(img_path)

    # Convert to grayscale
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    # Enhance contrast and remove noise
    enhanced = cv2.equalizeHist(gray)
    blurred = cv2.GaussianBlur(enhanced, (5, 5), 0)

    # Apply adaptive thresholding to binarize the image
    _, thresh = cv2.threshold(blurred, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)

    # Find contours
    contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

    # Create a blank mask
    mask = np.zeros_like(gray)

    # Find the largest contour (assuming it's the finger)
    if contours:
        largest_contour = max(contours, key=cv2.contourArea)

        # Draw the largest contour on the mask
        cv2.drawContours(mask, [largest_contour], 0, 255, -1)

        # Apply the mask to the original image to get the fingerprint region
        fingerprint = cv2.bitwise_and(gray, gray, mask=mask)

        # Find the contours within the fingerprint region
        fingerprint_contours, _ = cv2.findContours(fingerprint, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

        # Create a blank image for drawing fingerprint contours
        contour_image = np.zeros_like(gray)

        # Draw the fingerprint contours on the blank image
        cv2.drawContours(contour_image, fingerprint_contours, -1, 255, 1)

        # Save the contour image
        contour_img_path = os.path.join(save_dir, f'contour_{img_name}.jpg')
        cv2.imwrite(contour_img_path, contour_image)
        print(f"Contour image saved as '{contour_img_path}'")
    else:
        print("No contours found.")
Editor is loading...
Leave a Comment