How to remove small black dots in images?

8 views (last 30 days)
AZ
AZ on 27 Jan 2023
Answered: Image Analyst on 27 Jan 2023
Hi, is it possible to digitally edit this picture to erase the black dots there are within the target area of this image (without affecting the black marks delimitating the target)?
The picture is already being processed in a code in Matlab, and these dots are introducing noise to some calculations being performed, so if anyone could guide me on how to edit the image in order to erase these dots would be very heplful.
Thanks

Answers (1)

Image Analyst
Image Analyst on 27 Jan 2023
First of all, get control over your lighting - it's horrible. Make it more uniform. Then try this code:
% 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 TEST IMAGE
folder = [];
baseFileName = 'small black dots.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
grayImage = 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(grayImage)
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 = rgb2gray(grayImage);
end
%--------------------------------------------------------------------------------------------------------
% Display the image.
subplot(2, 2, 1);
imshow(grayImage, []);
impixelinfo;
axis('on', 'image');
title('Original Gray Scale Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Update 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(grayImage)
% Maximize window.
g = gcf;
g.WindowState = 'maximized';
drawnow;
%--------------------------------------------------------------------------------------------------------
% Display histogram
subplot(2, 2, 2);
histogram(grayImage);
grid on;
title('Histogram of Image', 'FontSize', fontSize, 'Interpreter', 'None');
%--------------------------------------------------------------------------------------------------------
% Threshold to create mask
lowThreshold = 0;
highThreshold = 70;
% 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, highThreshold, grayImage)
mask = grayImage >= lowThreshold & grayImage <= highThreshold;
xline(highThreshold, 'Color', 'r', 'LineWidth', 2)
subplot(2, 2, 3);
imshow(mask);
impixelinfo;
axis('on', 'image');
title('Initial Mask Image', 'FontSize', fontSize, 'Interpreter', 'None');
%--------------------------------------------------------------------------------------------------------
% Find the areas of all the blobs.
props = regionprops(mask, 'Area');
allAreas = sort([props.Area], 'ascend')
% Get a mask of blobs only with areas of 1 to 100 pixels.
mask = bwareafilt(mask, [1, 100]);
subplot(2, 2, 4);
imshow(mask);
impixelinfo;
axis('on', 'image');
title('Final Mask Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Fill in those regions with surrounding gray levels.
% First dilate it a bit.
mask = imdilate(mask, true(3));
% Now smear in surrounding gray levels
correctedImage = regionfill(grayImage, mask);
hFig = figure;
imshow(correctedImage, []);
impixelinfo;
axis('on', 'image');
title('Corrected Gray Scale Image', 'FontSize', fontSize, 'Interpreter', 'None');
hFig.WindowState = 'maximized';

Products

Community Treasure Hunt

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

Start Hunting!