Description
I have an image of a finger (with green background) and want to extract the finger nail as a feature. My goal is to describe the contours of the fingernail with functions. However the function part I haven't tried yet and I'm sure I can figure out by myself. I struggle to extract the fingernail and would like to get help by you. You can find images at the end of the post.
What I have done so far:
- load the image
- resize the image for less computational effort
- process it (blur, remove green background, convert into gray scale
- extract the fingernail from the image (how?)
I tried to do an circle detection or ellipse detection. The circle detection using hough transform doesn't recognize the fingernail. Same for the ellipse detection (apart from the fact that it took 2 mins and that is way too long to wait). Now my question is: Is there an easy way of solving the problem and extract the fingernail?
I also used edge detection / contour detection to extract the fingernail, however it was way too inaccurate and not helpful.
My dream would be to additionally separate the gray/darker part at the beginning of the fingernail, but I did not manage to do that and therefore gave up on this part. However if you know a good and easy way I would love to hear it.
The important code fragments:
# imports
# helper functions
def remove_green(img):
empty_img = np.zeros_like(img)
RED, GREEN, BLUE = (2, 1, 0)
reds = img[:, :, RED]
greens = img[:, :, GREEN]
blues = img[:, :, BLUE]
# loop over the image, pixel by pixel
tmpMask = (greens < 35) | (reds > greens) | (blues > greens)
img[tmpMask == 0] = (0, 0, 0) # remove background from original picture
empty_img[tmpMask] = (255, 255, 255) # mask with finger in white
return img, empty_img
# main function
# load and process
image = cv2.imread(imagePath, 1) # load
image = cv2.resize(image, None, fx=0.3, fy=0.3) # resize
image = cv2.GaussianBlur(image, (3, 3), 0)
no_green_image, mask_finger = remove_green(image) # remove green
gray = cv2.cvtColor(no_green_image, cv2.COLOR_BGR2GRAY) # gray scalEd
gray_mask_finger = cv2.cvtColor(mask_finger, cv2.COLOR_BGR2GRAY)
# refine edges
kernel = np.ones((5, 5), np.uint8)
gray_mask_finger = cv2.morphologyEx(gray_mask_finger, cv2.MORPH_GRADIENT, kernel)
detect_nail(gray_mask_finger)
# here I struggle
Images
The starting image:

Removed green & convert into gray:

Contours:

