Can I get automatic Nail Image segmentation code?

when input hand image is given,the output must contain segmented nail image

4 Commenti

"when input hand image is given,the output must contain segmented nail image"
Okay!
On a serious note - This seems like a HW assignment, given the exact same question has been asked just a few minutes ago - https://in.mathworks.com/matlabcentral/answers/2065571-i-want-to-nails-semantic-segmentation-using-the-nail-image-from-fingers
Show us what you have tried yet.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clearvars;
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 16;
fprintf('Beginning to run %s.m ...\n', mfilename);
%-----------------------------------------------------------------------------------------------------------------------------------
% Read in image.
folder = [];
baseFileName = 'leaf1.jpeg';
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% The file doesn't exist -- didn't find it there in that folder.
% Check the entire search path (other folders) for the file by stripping off the folder.
fullFileNameOnSearchPath = baseFileName; % No path this time.
if ~exist(fullFileNameOnSearchPath, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
% It's not an RGB image! It's an indexed image, so read in the indexed image...
rgbImage = imread(fullFileName);
[rows, columns, numberOfColorChannels] = size(rgbImage)
% Display the test image.
subplot(2, 2, 1);
imshow(rgbImage, []);
axis('on', 'image');
caption = sprintf('Image : "%s"', baseFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Set up figure properties:
% Enlarge figure to full screen.
hFig1 = gcf;
hFig1.Units = 'Normalized';
hFig1.WindowState = 'maximized';
% Get rid of tool bar and pulldown menus that are along top of figure.
% set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
hFig1.Name = 'Demo by Image Analyst';
[mask, maskedRGBImage] = createMask(rgbImage);
% Take just the largest regions:
mask = bwareafilt(mask, 1);
% Fill Holed.
mask = imfill(mask, 'holes');
% Display the initial mask image.
subplot(2, 2, 2);
imshow(mask, []);
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
axis('on', 'image');
title('Mask', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
% Mask the image using bsxfun() function to multiply the mask by each channel individually. Works for gray scale as well as RGB Color images.
maskedRgbImage = bsxfun(@times, rgbImage, cast(mask, 'like', rgbImage));
% Display the final masked image.
subplot(2, 2, 3);
imshow(maskedRgbImage, []);
axis('on', 'image');
title('Masked Image', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Display the final masked image of the background by inverting the mask.
% Mask the image using bsxfun() function to multiply the mask by each channel individually. Works for gray scale as well as RGB Color images.
backgroundImage = bsxfun(@times, rgbImage, cast(~mask, 'like', rgbImage));
subplot(2, 2, 4);
imshow(backgroundImage, []);
axis('on', 'image');
title('Background Image', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
%-------------------------------------------------------------------------------------------------------------
% Make measurements
props = regionprops(mask, 'Area', 'Centroid')
allAreas = [props.Area];
xyCentroids = vertcat(props.Centroid);
subplot(2, 2, 2);
hold on;
for k = 1 : length(props)
x = xyCentroids(k, 1);
y = xyCentroids(k, 2);
txt = sprintf(' (x, y) = (%.1f, %.1f). Area = %d', ...
x, y, allAreas(k));
text(x, y, txt, 'Color', 'r', 'FontWeight', 'bold');
plot(x, y, 'r+', 'MarkerSize', 25, 'LineWidth', 2);
end
% Get boundary.
boundaries = bwboundaries(mask);
boundaries = boundaries{1}; % Extract from cell.
x = boundaries(:, 2);
y = boundaries(:, 1);
plot(x, y, 'r-', 'LineWidth', 3);
fprintf('Done running %s.m ...\n', mfilename);
msgbox('Done!');
function [BW,maskedRGBImage] = createMask(RGB)
%createMask Threshold RGB image using auto-generated code from colorThresholder app.
% [BW,MASKEDRGBIMAGE] = createMask(RGB) thresholds image RGB using
% auto-generated code from the colorThresholder app. The colorspace and
% range for each channel of the colorspace were set within the app. The
% segmentation mask is returned in BW, and a composite of the mask and
% original RGB images is returned in maskedRGBImage.
% Auto-generated by colorThresholder app on 01-Nov-2020
%------------------------------------------------------
% Convert RGB image to chosen color space
I = rgb2hsv(RGB);
% Define thresholds for channel 1 based on histogram settings
channel1Min = 0.183;
channel1Max = 0.400;
% Define thresholds for channel 2 based on histogram settings
channel2Min = 0.000;
channel2Max = 1.000;
% Define thresholds for channel 3 based on histogram settings
channel3Min = 0.000;
channel3Max = 1.000;
% Create mask based on chosen histogram thresholds
sliderBW = (I(:,:,1) >= channel1Min ) & (I(:,:,1) <= channel1Max) & ...
(I(:,:,2) >= channel2Min ) & (I(:,:,2) <= channel2Max) & ...
(I(:,:,3) >= channel3Min ) & (I(:,:,3) <= channel3Max);
BW = sliderBW;
% Initialize output masked image based on input image.
maskedRGBImage = RGB;
% Set background pixels where BW is false to zero.
maskedRGBImage(repmat(~BW,[1 1 3])) = 0;
end
we tried to study this code and implement in our code but it did not work. so can you provide any guidance
We are different person working on same project

Accedi per commentare.

Risposte (1)

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 Commenti

Thank You for the guidance sir, but still the output is not proper.Is there any possibility for guiding in training machine learning model
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

Accedi per commentare.

Categorie

Scopri di più su Deep Learning Toolbox in Centro assistenza e File Exchange

Richiesto:

il 2 Gen 2024

Commentato:

il 2 Gen 2024

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by