【问题标题】:Human fingernail segmanation without training a model无需训练模型的人指甲分割
【发布时间】:2023-03-22 21:07:01
【问题描述】:

我想遮盖人的指甲(指甲是白色的,包括手在内的所有东西都是黑色的)。我进行简单的图像操作,然后在平滑图像后进行 Canny 边缘检测,然后找到轮廓以使内部轮廓为白色,这将是指甲。

我的问题是,当指甲被涂上时很容易检测到,但是当没有油漆时它变得非常复杂,程序必须获取 50 张图像并将输出保存到某个文件夹。

我对如何继续感到困惑,如果有人做过类似的事情,我将不胜感激。

import cv2
import numpy as np
import matplotlib.pyplot as plt

def display_img(img):
    fig = plt.figure(figsize = (12,10))
    ax = fig.add_subplot(111)
    plt.imshow(img,cmap='gray')

img = cv2.imread('nail2.png')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

blur = cv2.blur(gray,ksize=(1,1))
kernel = np.ones((5,5),np.uint8)
display_img(blur)

med = np.median(gray)

gradient = cv2.Laplacian(blur,cv2.CV_64F)
gradient = cv2.convertScaleAbs(gradient)
plt.imshow(gradient,'gray')
lower = int(max(0,0.7*med))
upper = int(min(255,1.3*med))

edges = cv2.Canny(blur,lower,upper)

display_img(edges)

edges = cv2.GaussianBlur(edges, (11, 11), 0)  # smoothing before applying  threshold
display_img(edges)

image, contours, hierarchy = cv2.findContours(edges, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)

# Create empty array to hold internal contours
image_internal = np.zeros(image.shape)

# Iterate through list of contour arrays
for i in range(len(contours)):
    # If third column value is NOT equal to -1 than its internal
    if hierarchy[0][i][3] != -1:
        
        # Draw the Contour
        cv2.drawContours(image_internal, contours, i, 255, -1)

display_img(image_internal)

下面是一个不错的结果:

即使手指有粉红色的油漆,结果也会很糟糕:

【问题讨论】:

  • 总是发布未经过滤的原始图片,以便人们分析问题。

标签: python opencv image-processing computer-vision


【解决方案1】:

嗯,这两张图片存在很大的光线和比例问题。但一种可能的解决方案是分割颜色通道并寻找斑点。

然后你可以使用 blob 参数进行分段。

您可以在这里尝试的代码:

import cv2
import numpy as np

fra = cv2.imread('nails.png')
height, width, channels = fra.shape

src = cv2.medianBlur(fra, 21)
hsv = cv2.cvtColor(src, cv2.COLOR_BGR2HSV_FULL)

mask = cv2.inRange(hsv, np.array([0, 0, 131]), np.array([62, 105, 255]))
mask = cv2.erode(mask, None, iterations=8)
mask = cv2.dilate(mask, None, iterations=8)

params = cv2.SimpleBlobDetector_Params()
params.filterByArea = True
params.minArea = int((height * width) / 500)
params.maxArea = int((height * width) / 10)

params.filterByCircularity = True
params.minCircularity = 0.5
params.filterByConvexity = True
params.minConvexity = 0.5
params.filterByInertia = True
params.minInertiaRatio = 0.01

detector = cv2.SimpleBlobDetector_create(params)
key_points = detector.detect(255 - mask)

vis = cv2.bitwise_and(hsv, hsv, mask=mask)
vis = cv2.addWeighted(src, 0.2, vis, 0.8, 0)
cv2.drawKeypoints(vis, key_points, vis, (0, 0, 255), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
for kp in key_points:
    cv2.drawMarker(vis, (int(kp.pt[0]), int(kp.pt[1])), color=(0, 255, 0), markerType=cv2.MARKER_CROSS, thickness=3)

cv2.imshow("VIS", vis)
cv2.imwrite('nails_detected.png', vis)
cv2.waitKey(0)
cv2.destroyAllWindows()

祝你好运!

【讨论】:

    猜你喜欢
    • 2018-10-21
    • 2019-05-14
    • 2023-01-01
    • 2022-06-23
    • 2019-10-30
    • 2017-06-17
    • 2018-07-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多