I want to remove small spots from a greyscale image without affecting the rest of the image

조회 수: 31 (최근 30일)
See the attached image for reference. I want to remove the white spot encircled in the image without affecting much the rest of the image. I dont mind losing the dot smaller than the one in the circle but nothing bigger.
I tried bwareaopen but it converts my image to binary which I do not want! I want to keep it in grayscale but remove the dots. thanks for any help!
  댓글 수: 2
Julian Hapke
Julian Hapke 2017년 6월 15일
I'd suggest you use a binary array of that image as a mask. look for areas in the binary image that are connected and determine the size of those connected regions. if the region in below a threshold, remove it. use
imbinarize
Julian Hapke
Julian Hapke 2017년 6월 15일
편집: Julian Hapke 2017년 6월 15일
had to read about bwareaopen, seems exactly what you need. you can then use the cleaned binary image as an index to set the pixels of your original image to black or whatever grey you want there.

댓글을 달려면 로그인하십시오.

채택된 답변

Image Analyst
Image Analyst 2017년 6월 16일
This code will do it:
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 = 25;
%===============================================================================
% Get the name of the image the user wants to use.
baseFileName = 'eeewvwvwv.PNG'; % Assign the one on the button that they clicked on.
% Get the full filename, with path prepended.
folder = []; % Determine where demo folder is (works with all versions).
fullFileName = fullfile(folder, baseFileName);
%===============================================================================
% Read in a demo image.
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.
% Use weighted sum of ALL channels to create a gray scale image.
grayImage = rgb2gray(grayImage);
% ALTERNATE METHOD: Convert it to gray scale by taking only the green channel,
% which in a typical snapshot will be the least noisy channel.
% grayImage = grayImage(:, :, 2); % Take green channel.
end
% Display the image.
subplot(2, 2, 1);
imshow(grayImage, []);
axis on;
caption = sprintf('Original Gray Scale Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo();
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% 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.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
drawnow;
% Binarize the image by thresholding.
mask = grayImage > 30;
% Find the areas
props = regionprops(logical(mask), 'Area');
allAreas = sort([props.Area])
% Extract only blobs larger than 25.
mask = bwareaopen(mask, 25);
% Get rid of white frame around outside border.
mask = imclearborder(mask);
subplot(2, 2, 2);
imshow(mask);
axis on;
axis image; % Make sure image is not artificially stretched because of screen's aspect ratio.
title('Binary Image Mask', 'fontSize', fontSize);
drawnow;
% Mask image to produce a masked gray scale image.
maskedGrayImage = grayImage; % First initialize.
maskedGrayImage(~mask) = 0; % Now mask
subplot(2, 2, 3);
imshow(maskedGrayImage);
axis on;
axis image; % Make sure image is not artificially stretched because of screen's aspect ratio.
title('Masked Gray Scale Image', 'fontSize', fontSize);
drawnow;
  댓글 수: 3
SatyaPrakash Gupta
SatyaPrakash Gupta 2020년 4월 3일
편집: Image Analyst 2020년 4월 3일
Hi,
I would like to keep the white area and remove the black area, how can i do that ?
Image Analyst
Image Analyst 2020년 4월 3일
SatyaPrakash. First read this link, then start your own question and attach your image. If you have a binary image you cannot remove the black area, unless you just want a 1-D vector of all 1's, because an image must remain rectangular. You can't have an all-white image with "ragged" edges - it doesn't make sense.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by