Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 不训练模型的人指甲分割_Python_Opencv_Image Processing_Computer Vision - Fatal编程技术网

Python 不训练模型的人指甲分割

Python 不训练模型的人指甲分割,python,opencv,image-processing,computer-vision,Python,Opencv,Image Processing,Computer Vision,我想遮住人的指甲(指甲是白色的,包括手在内的所有东西都是黑色的)。我做简单的图像操作,然后在平滑图像后进行Canny边缘检测,然后找到轮廓,使内部轮廓呈现白色,这将是指甲 我的问题是,当涂指甲时,很容易检测到,但是当没有涂指甲时,它变得非常复杂,程序必须获得50幅图像并将输出保存到某个文件夹 我对如何进行感到困惑,如果有人做了类似的事情,我将不胜感激 import cv2 import numpy as np import matplotlib.pyplot as plt def displa

我想遮住人的指甲(指甲是白色的,包括手在内的所有东西都是黑色的)。我做简单的图像操作,然后在平滑图像后进行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)
下面是一个很好的结果:

即使手指涂有粉色油漆,也会产生一些不良结果:


好吧,这两张图片中的光线和比例问题很大。但一个可能的解决方案是分割颜色通道并查找斑点

然后可以使用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()

祝你好运

始终张贴未经过滤的原始图片,以便人们可以分析问题。