MATLAB: How to labels each cell in image? i upload the input images and the output masks which i wants from the input image.
i want to label(separate each cell) from the given image
Related Solutions
Convert to grayscale and then threshold and find the bounding box
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures.
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;format compact;fontSize = 20;% Read in the color image.
folder = pwd;baseFileName = 'm2.bmp';% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);if ~exist(fullFileName, 'file') % Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file') % Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName); uiwait(warndlg(errorMessage)); return; endendrgbImage = imread(fullFileName);% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows, columns, numberOfColorBands] = size(rgbImage);% Display the original color image.
subplot(2, 3, 1);imshow(rgbImage, []);axis on;title('Original Color Image', 'FontSize', fontSize);drawnow;% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);% Histogram the red channel, but there is a huge spike at 255 so let's not count those.
subplot(2, 3, 2);grayImage = rgb2gray(rgbImage);imshow(grayImage, []);title('Gray Scale version', 'FontSize', fontSize);subplot(2, 3, 3);histogram(grayImage(grayImage<255))grid on;title('Histogram of Gray Scale Image', 'FontSize', fontSize);someThresholdValue = 150;binaryImage = grayImage > someThresholdValue;% Take largest blob
binaryImage = bwareafilt(binaryImage, 1);subplot(2, 3, 4);imshow(binaryImage);grid on;title('Binary Image', 'FontSize', fontSize);% Label the binary image.
labeledImage = bwlabel(binaryImage);props = regionprops(labeledImage, 'BoundingBox');bbox = props.BoundingBoxcroppedImageRGB = imcrop(rgbImage, bbox);subplot(2, 3, 5);imshow(croppedImageRGB);grid on;title('Cropped RGB Image', 'FontSize', fontSize);% Gray scale version of cropped image
croppedImageGray = imcrop(grayImage, bbox);subplot(2, 3, 6);imshow(croppedImageGray);grid on;title('Cropped Gray Scale Image', 'FontSize', fontSize);
Of course it's possible. Try this. Adapt as needed:
% Demo by Image Analyst, February, 2021.
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 = 'original image.JPG';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; endendgrayImage = imread(fullFileName);[rows, columns, numberOfColorChannels] = size(grayImage)if numberOfColorChannels == 3 grayImage = rgb2gray(grayImage);end% Display the test image full size.
subplot(2, 3, 1);imshow(grayImage, []);axis('on', 'image');caption = sprintf('Original 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';%--------------------------------------------------------------------------------------------------------
% Get histogram
subplot(2, 3, 2);imhist(grayImage);grid on;xlabel('Gray Level', 'FontSize', fontSize);ylabel('Count', 'FontSize', fontSize);% Threshold image.
mask = grayImage < 75;% Clean up the mask to get only the stripes we want.
mask(:, 1 : 762) = false;mask(:, 2615 : end) = false;% Get rid any blob not 1500 wide
labeledImage = bwlabel(mask);props = regionprops(labeledImage, 'BoundingBox');allBB = vertcat(props.BoundingBox);widths = allBB(:, 3);wideBlobs = find(widths > 1500);% Extract only those that are wide enough.
mask = ismember(labeledImage, wideBlobs);% Display mask image.
subplot(2, 3, 3);imshow(mask, []);axis('on', 'image');title('Mask', 'FontSize', fontSize, 'Interpreter', 'None');drawnow;hold on;hp = impixelinfo(); % Set up status line to see values when you mouse over the image.% Label each blob with 8-connectivity, so we can make measurements of it
[labeledImage, numberOfBlobs] = bwlabel(mask, 8);% Apply a variety of pseudo-colors to the regions.
coloredLabelsImage = label2rgb (labeledImage, 'hsv', 'k', 'shuffle');% Display the pseudo-colored image.
subplot(2, 3, 4);imshow(coloredLabelsImage);title('Labeled Blobs', 'FontSize', fontSize, 'Interpreter', 'None');% Get all the blob properties. Can only pass in originalImage in version R2008a and later.
blobMeasurements = regionprops(labeledImage, grayImage, 'all');numberOfBlobs = size(blobMeasurements, 1);% For each blob, for each line, get the left edge and right edge then widths.
leftEdges = zeros(1, columns);rightEdges = zeros(1, columns);widths = zeros(columns, numberOfBlobs);for blob = 1 : numberOfBlobs thisBlob = ismember(labeledImage, blob); % Display mask image. subplot(2, 3, 5); imshow(thisBlob, []); axis('on', 'image'); caption = sprintf('Mask of Blob #%d of %d', blob, numberOfBlobs); title(caption, 'FontSize', fontSize, 'Interpreter', 'None'); drawnow; hp = impixelinfo(); % Set up status line to see values when you mouse over the image. for col = 1 : columns thisCol = thisBlob(:, col); t = find(thisCol, 1, 'first'); if ~isempty(t) leftEdges(col) = t; rightEdges(col) = find(thisCol, 1, 'last'); end end widths(:, blob) = (rightEdges - leftEdges)'; subplot(2, 3, 6); plot(widths, '-', 'LineWidth', 1); hold on; grid on; drawnow; legendStrings{blob} = sprintf('Blob #%d', blob);endgrid on;title('Widths', 'FontSize', fontSize, 'Interpreter', 'None');xlabel('Row', 'FontSize', fontSize);ylabel('Width in Pixels', 'FontSize', fontSize);legend(legendStrings);% Display mask image again.
subplot(2, 3, 5);imshow(mask, []);axis('on', 'image');title('Mask', 'FontSize', fontSize, 'Interpreter', 'None');drawnow;hold on;hp = impixelinfo(); % Set up status line to see values when you mouse over the image.fprintf('Done running %s.m\n', mfilename);
Best Answer