How can I remove a grid from an image without removing objects on the borders of the grid?
15 views (last 30 days)
Show older comments
Thomas Colling
on 28 Jul 2022
Commented: Thomas Colling
on 2 Aug 2022
I am writing a program that is going to count red blood cells from an image produced by a transmission microscope. The image is grayscale by default, and the grid in the image has pixel values in the same range as the cells I am trying to count. Here is my code so far (Note: grayscale image corresponds to 'J' in the code)
%Used to isolate cell masks and count the cells on a 4x4 gridded slide
clc; % Clear command window.
clear; % Delete all variables.
close all; % Close all figure windows except those created by imtool.
imtool close all; % Close all figure windows created by imtool.
workspace; % Make sure the workspace panel is showing.
fontSize = 15;
img = imread('fileName');
figure(1)
imshow(img) %show original image from file
[J, rect] = imcrop(img);%User crops photo to control the analyzed area
figure(2)
imshow(J) %show cropped image
impixelinfo;
%%
BW = J < 90 & J > 40;
figure (3)
imshow(BW);
axis on;
title('Binary Image', 'FontSize', fontSize);
%%
cleanBW = bwareaopen(BW, 100, 4);
cleanBW = bwmorph(cleanBW, 'spur');
cleanBW = bwmorph(cleanBW, 'majority', inf);
figure(4)
imshow(cleanBW);
axis on;
I am new to image processing, so I do not know if there should be more filtering on the original grayscale image, or if I would be able to somehow identify the uniform portions (parts without cells) of the grid markings in the binary image and change them from 1s to 0s. All suggestions are welcome!
0 Comments
Accepted Answer
Image Analyst
on 29 Jul 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 = 16;
markerSize = 40;
%--------------------------------------------------------------------------------------------------------
% READ IN IMAGE
folder = pwd;
baseFileName = 'RBCGridExample.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;
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.
fprintf('It is not really gray scale like we expected - it is color\n');
% Extract the blue channel.
grayImage = rgbImage(:, :, 1);
else
grayImage = rgbImage;
end
%--------------------------------------------------------------------------------------------------------
% Display the gray scale image.
subplot(2, 2, 1);
imshow(grayImage);
impixelinfo;
axis('on', 'image');
title('Original Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Maximize window.
g = gcf;
g.WindowState = 'maximized';
drawnow;
%--------------------------------------------------------------------------------------------------------
% READ IN TEMPLATE IMAGE
baseFileName = 'RBCGridExample template.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
templateImage = imread(fullFileName);
% Convert to logical
templateImage = logical(templateImage);
%--------------------------------------------------------------------------------------------------------
% Display the image.
subplot(2, 2, 2);
imshow(templateImage);
impixelinfo;
axis('on', 'image');
title('Template Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Maximize window.
g = gcf;
g.WindowState = 'maximized';
drawnow;
%--------------------------------------------------------------------------------------------------------
% Get the mean of the image outside the white areas of the template.
% Get the mean of the image only inside the black areas of the template.
meanGL = mean(grayImage(~templateImage))
%--------------------------------------------------------------------------------------------------------
% Set the gray scale image values equal to the mean gray level in
% the white areas of the template.
grayImage(templateImage) = meanGL;
% Display image.
subplot(2, 2, 3);
imshow(grayImage);
axis('on', 'image');
drawnow;
title('Repaired Image', 'FontSize', fontSize, 'Interpreter', 'None');
More Answers (2)
Image Analyst
on 28 Jul 2022
Assuming the grid is in the same position for all images, just use a template to set the grid lines to the mean intensity of the image.
If the grid moves around use radon or hough to find the angle and rotate the image then use the template.
0 Comments
See Also
Categories
Find more on Image Processing Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!