Use shape Factor to judge cell or bacteria

2 views (last 30 days)
Jiawei Liu
Jiawei Liu on 27 Jan 2022
Answered: Image Analyst on 28 Jan 2022
I want to extract the bacteria and red blood cells in this picture, color all the red blood cells red and the bacteria in the middle blue.I don't know what's wrong with my code, but I haven't been able to separate successfully

Answers (3)

Image Analyst
Image Analyst on 27 Jan 2022
Maybe try imfindcircles()
  2 Comments
Jiawei Liu
Jiawei Liu on 27 Jan 2022
Can you take a look at my code? In order to submit an issue, I wrote very detailed comments. My code can successfully identify the other two images and label and color cells and bacteria. But it can't be used in this picture. I spent 3 weeks and still can't find the problem.
best wish.

Sign in to comment.


yanqi liu
yanqi liu on 27 Jan 2022
yes,sir,may be check segment process,for example,use watermark and so on
clear; close all;
% Pre-processing -----------------------
% Load input image
I = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/875185/image.png');
% figure, imshow(I)
% Covert image to grayscale
I_gray = rgb2gray(I);
% figure, imshow(I_gray)
%Rescale image
B = imresize(I_gray, [512 NaN], 'bilinear');
% figure, imshow(B)
% title('Rescale image')
%Produce histogram before enhancing
% figure,imhist(B,64)
% title('histogram')
% Enhance image before binarisation
J = 255*im2double(B); % converts the intensity image I to double precision
mi = min(min(J)); % find the minimum pixel intensity
ma = max(max(J)); % find the maximum pixel intensity
B_imadjust = imadjust(B,[mi/255; ma/255],[0; 1]);
% Histogram after enhancement
% figure,imhist(B_imadjust,64)
% title('Histogram after enhancement')
% Image Binarisation
B_thresh=graythresh(B_imadjust);
BW = imbinarize(B_imadjust,B_thresh);
b1 = imfill(im2bw(B_imadjust,0.9), 'holes');
% BW = imbinarize(B_imadjust);
% BW = imbinarize(B_imadjust,B_thresh);
figure,imshow(BW)
title('imbinarize')
% Edge detection
B_w = edge(BW,'canny',0.86);
figure,imshow(B_w)
title('Edge detection')
% Simple segmentation
mask = zeros(size(B_imadjust));
mask(25:end-25,25:end-25) = 1;
bw = activecontour(B_imadjust,mask,500);
BW2 = bwareaopen(bw, 700);
figure,imshow(BW2)
title('segmentation')
% Object Recognition
b1 = bwareaopen(b1, 3000);
b2 = bwareaopen(imopen(logical(BW2-b1), strel('disk', 5)), 3000);
L1 = zeros(size(b1)); L1(b1) = 1;
L2 = zeros(size(b2)); L2(b2) = 2;
L = L1 + L2;
result = label2rgb(L,@jet,[.0 .0 .0]);
figure,imshow(result);
  5 Comments
Image Analyst
Image Analyst on 27 Jan 2022
I'd probably try computing the convex hull of all of them with bwconvhull(mask, 'objects') and then use regionprops to get the MajorAxisLength and MinorAxisLength, then compute aspect ratios. High aspect ratio blobs are bacteria, and low aspect ratio blobs are cells.
yanqi liu
yanqi liu on 28 Jan 2022
yes,may be use more regionprops to make rule,or use cnn、svm model to train classify model

Sign in to comment.


Image Analyst
Image Analyst on 28 Jan 2022
Try this:
% Demo by Image Analyst
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 22;
%--------------------------------------------------------------------------------------------------------
% READ IN IMAGE
folder = pwd;
baseFileName = 'bacteria.png';
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
rgbImage = imread(fullFileName);
% Get the dimensions of the image.
% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.
[rows, columns, numberOfColorChannels] = size(rgbImage)
if numberOfColorChannels > 1
% It's not really gray scale like we expected - it's color.
% Extract the blue channel.
grayImage = rgbImage(:, :, 3);
else
grayImage = rgbImage;
end
%--------------------------------------------------------------------------------------------------------
% Display the image.
subplot(2, 3, 1);
imshow(grayImage);
impixelinfo;
axis('on', 'image');
title('Original Gray Scale Image', 'FontSize', fontSize, 'Interpreter', 'None');
hold on
% Maximize window.
g = gcf;
g.WindowState = 'maximized'
drawnow;
%--------------------------------------------------------------------------------------------------------
% Get a histogram
subplot(2, 3, 2);
imhist(grayImage);
grid on;
title('Histogram of Original Gray Scale Image', 'FontSize', fontSize, 'Interpreter', 'None');
%--------------------------------------------------------------------------------------------------------
% Set thresholds for each level.
lowThreshold = 113;
highThreshold = 255;
% Interactively and visually set a threshold on a gray scale image.
% https://www.mathworks.com/matlabcentral/fileexchange/29372-thresholding-an-image?s_tid=srchtitle
% [lowThreshold, highThreshold] = threshold(lowThreshold, 255, grayImage)
% Draw threshold line over histogram.
xline(lowThreshold, 'Color', 'r', 'LineWidth', 2);
%--------------------------------------------------------------------------------------------------------
% Binarize the image to get a mask.
mask = grayImage > lowThreshold;
mask = imfill(mask, 'holes');
% Take blobs larger than 100 pixels in area.
mask = bwareafilt(mask, [100, inf]);
% Display mask image.
subplot(2, 3, 3);
imshow(mask);
impixelinfo;
axis('on', 'image');
drawnow;
caption = sprintf('Threshold = %d pixels', lowThreshold);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
%--------------------------------------------------------------------------------------------------------
% Get the aspect ratios
[labeledImage, numBlobs] = bwlabel(mask);
props = regionprops(labeledImage, 'Area', 'MajorAxisLength', 'MinorAxisLength')
aspectRatios = [props.MajorAxisLength] ./ [props.MinorAxisLength]
% Define an aspect ratio that will select bacteria:
aspectRatioThreshold = 5;
% Get cell mask:
cellMask = ismember(labeledImage, find(aspectRatios < aspectRatioThreshold));
% Display bacteria mask image.
subplot(2, 3, 4);
imshow(cellMask);
impixelinfo;
axis('on', 'image');
drawnow;
title('Cell Mask', 'FontSize', fontSize, 'Interpreter', 'None');
% Get bacteria mask:
bacteriaMask = ismember(labeledImage, find(aspectRatios >= aspectRatioThreshold));
% Display bacteria mask image.
subplot(2, 3, 5);
imshow(bacteriaMask);
impixelinfo;
axis('on', 'image');
drawnow;
title('Bacteria Mask', 'FontSize', fontSize, 'Interpreter', 'None');

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!