使用OpenCV提取手掌纹

-4 投票
2 回答
77 浏览
提问于 2025-04-13 13:38

现在我想做一个程序,从手掌中提取主要的掌纹,但我不知道该如何处理一些噪音。

第一步:

import cv2
img = cv2.imread('palm.png', -1)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
smooth = cv2.GaussianBlur(gray, (95, 95), 0)
# divide gray by morphology image
division = cv2.divide(gray, smooth, scale=192)
cv2.imshow('division', division)
cv2.waitKey(0)
cv2.destroyAllWindows()

输出

然后把它转换成黑白图像:

# white color mask
img = cv2.imread('gray.png')
image = cv2.cvtColor(img, cv2.COLOR_BGR2HLS)
lower = np.uint8([0, 185, 0])
upper = np.uint8([255, 255, 255])
white_mask = cv2.inRange(image, lower, upper)
# gray color mask
lower = np.uint8([169, 169, 169])
upper = np.uint8([198, 198, 198])
gray_mask = cv2.inRange(image, lower, upper)
# combine the mask
mask = cv2.bitwise_or(white_mask, gray_mask)
cv2.imshow("mask", mask)
cv2.waitKey(0)
cv2.destroyAllWindows()

我得到了这个:

输出

我该如何去除这些噪音和一些无关的线条,特别是去掉拇指周围的噪音。这样我就可以提取出手上的主要线条:

一

最后,可以提取出三条最重要的线条,如下所示:

一

2 个回答

0

你可以试试下面的方法。在这里,我尝试只获取手掌区域,并使用了Canny边缘检测来找到手掌的印记。你可以根据自己的需要调整阈值。希望这对你有帮助。

import cv2
import numpy as np

def process_image(image_path):
    # Read the image and apply filter to remove noise , convert to gray scale
    image_org = cv2.imread(image_path)
    image = cv2.GaussianBlur(image_org, (5, 5), 0)
    grayscale_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    #Apply threshold to remove the back ground and get only the palm
    ret, thresholded_image = cv2.threshold(grayscale_image, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) #
    contours, hierarchy = cv2.findContours(thresholded_image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    contour_image = np.zeros_like(thresholded_image)
    cv2.drawContours(contour_image, contours, -1, (255), thickness=cv2.FILLED)

    #Do some noise removal and gap filling methods and get the mask for the palm
    contour_image = cv2.bitwise_not(contour_image)
    contour_image = cv2.erode(contour_image, None, iterations=1)  # Erode to remove noise
    final_mask = cv2.dilate(contour_image, None, iterations=25)  # dilate to close gaps

    #Apply the mask to get only the palm
    hand_image = cv2.bitwise_and(grayscale_image,grayscale_image,mask = final_mask)

    #Apply canny edge detection to get the edges in palm
    #The threshold you can try different values and check.
    palm_edges = cv2.Canny(hand_image, 25, 60)

    return palm_edges

result = process_image("palm.jpg")
cv2.imwrite("palm_result.jpg",result)

输出结果:

在这里输入图片描述

0

我想你可以用边缘检测算法来找到手掌的裂缝。我最近用Canny边缘检测算法从漫画图片中提取草图。如果你能好好优化一下图片,这个算法加上一点卷积神经网络(CNN),就能只找到手掌了。

撰写回答