【发布时间】: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