Can I get automatic Nail Image segmentation code?
6 views (last 30 days)
Show older comments
when input hand image is given,the output must contain segmented nail image![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1580346/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1580346/image.png)
4 Comments
Image Analyst
on 2 Jan 2024
Isn't this a duplicate post to https://www.mathworks.com/matlabcentral/answers/2065571-i-want-to-nails-semantic-segmentation-using-the-nail-image-from-fingers#comment_3014971?
Are you and Mithun the same person or just in the same class?
Answers (1)
Ayush
on 2 Jan 2024
Hi @Yahya
I understand that you need an automatic Nail Image segmentation code which takes hand image as an input, to produce segmented nail image. Here is the conceptual code for that:
function segmented_nails = segment_nails(image_path)
% Read the image
hand_image = imread(image_path);
% Convert the image to YCbCr color space
YCbCr_img = rgb2ycbcr(hand_image);
Cb = YCbCr_img(:,:,2);
Cr = YCbCr_img(:,:,3);
% These thresholds are just starting points and may require fine-tuning
cb_min = 100; % Lower Cb threshold (adjust as needed)
cb_max = 140; % Upper Cb threshold (adjust as needed)
cr_min = 140; % Lower Cr threshold (adjust as needed)
cr_max = 175; % Upper Cr threshold (adjust as needed)
% Create a binary mask based on the adjusted thresholds
binary_mask = (Cb >= cb_min) & (Cb <= cb_max) & (Cr >= cr_min) & (Cr <= cr_max);
% Morphological operations to clean up the segmentation
binary_mask = imfill(binary_mask, 'holes');
binary_mask = bwareaopen(binary_mask, 50); % Remove small objects
binary_mask = imdilate(binary_mask, strel('disk', 5));
% Extract the segmented nails
segmented_nails = hand_image;
for i = 1:3
channel = segmented_nails(:,:,i);
channel(binary_mask == 0) = 0;
segmented_nails(:,:,i) = channel;
end
% Display the original and segmented images
subplot(1, 2, 1);
imshow(hand_image);
title('Original Image');
subplot(1, 2, 2);
imshow(segmented_nails);
title('Segmented Nails');
end
Note that even with adjustment, simple color-based thresholding is a heuristic approach and may not work perfectly in all cases. For more robust segmentation, you might consider training a machine learning model, such as a convolutional neural network (CNN), on a dataset of hand images with labeled nails.
Thanks,
Ayush
2 Comments
Ayush
on 2 Jan 2024
You may need your dataset for train such a model. However, I can help you with the conceptual code for creating and training a CNN model.
% Define the network input size and number of classes
inputSize = [256, 256, 3]; % Example input size (height, width, channels)
numClasses = 2; % Example number of classes (e.g., nail, background)
% Create the CNN for segmentation
% This function is defined in below code snippet
lgraph = createNailSegmentationCNN(inputSize, numClasses);
% visualize the network
analyzeNetwork(lgraph)
% Load your dataset (assuming imageDatastore and pixelLabelDatastore are prepared)
imageDir = 'path/to/images';
labelDir = 'path/to/labels';
% Create an imageDatastore for the images
imds = imageDatastore(imageDir);
% Create a pixelLabelDatastore for the labels
classNames = ["background", "nail"]; % Define class names as per your dataset
labelIDs = [0, 255]; % Define label IDs as per your dataset
pxds = pixelLabelDatastore(labelDir, classNames, labelIDs);
% Define training options
options = trainingOptions('sgdm', ...
'InitialLearnRate', 1e-3, ...
'MaxEpochs', 20, ...
'MiniBatchSize', 8, ...
'Shuffle', 'every-epoch', ...
'VerboseFrequency', 2, ...
'Plots', 'training-progress');
% Train the network
[net, trainInfo] = trainNetwork(imds, pxds, lgraph, options);
% After training, use the trained network to segment new images
newImage = imread('path/to/new/image.jpg');
C = semanticseg(newImage, net);
% Visualize the segmentation result
B = labeloverlay(newImage, C, 'Colormap', [0 1 0; 1 0 0], 'Transparency',0.4);
figure, imshow(B), title('Segmented Image');
Function to create the CNN:
function lgraph = createNailSegmentationCNN(inputSize, numClasses)
% Define the layers of the network
layers = [
imageInputLayer(inputSize, 'Name', 'input', 'Normalization', 'none')
convolution2dLayer(3, 64, 'Padding', 'same', 'Name', 'conv1_1')
reluLayer('Name', 'relu1_1')
convolution2dLayer(3, 64, 'Padding', 'same', 'Name', 'conv1_2')
reluLayer('Name', 'relu1_2')
maxPooling2dLayer(2, 'Stride', 2, 'Name', 'pool1')
convolution2dLayer(3, 128, 'Padding', 'same', 'Name', 'conv2_1')
reluLayer('Name', 'relu2_1')
convolution2dLayer(3, 128, 'Padding', 'same', 'Name', 'conv2_2')
reluLayer('Name', 'relu2_2')
maxPooling2dLayer(2, 'Stride', 2, 'Name', 'pool2')
% Add more layers as needed for your application
% Final layers for segmentation
convolution2dLayer(1, numClasses, 'Padding', 'same', 'Name', 'convFinal')
softmaxLayer('Name', 'softmax')
pixelClassificationLayer('Name', 'pixelClassification')
];
% Create a layer graph from the layer array
lgraph = layerGraph(layers);
end
Thanks,
Ayush
See Also
Categories
Find more on Image Data Workflows in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!