how can I remove the black portion in the surrounding from a dermoscopic image of melanoma for better segmentation?

1 view (last 30 days)

Answers (3)

Image Analyst
Image Analyst on 24 Dec 2019
Once you've segmented it to find dark regions, call imclearborder() to remove the dark regions at the corners.
  4 Comments
Image Analyst
Image Analyst on 25 Dec 2019
Edited: Image Analyst on 25 Dec 2019
Invert it, fill it, call imclearborder(), and take the largest single blob (if you want just one):
mask = imfill(~mask, 'holes');
mask = iclearborder(mask);
mask = bwareafilt(mask, 1); % Take largest blob only.

Sign in to comment.


Image Analyst
Image Analyst on 26 Dec 2019
Try the attached code.
0000 Screenshot.png
  9 Comments
Bilal Kashmiri
Bilal Kashmiri on 10 Apr 2022
%% Otsu Thresholding
close all;
clear all;
clc
%%================================================================================================
location = 'B:\Images\*.jpg'; % folder in which your images exists
ds = imageDatastore(location) % Creates a datastore for all images in your folder
while hasdata(ds)
img = read(ds) ; % read image from datastore
figure, imshow(img); % creates a new window for each image
%%=================================================================================================
n=imhist(img); % Compute the histogram
N=sum(n); % sum the values of all the histogram values
max=0; %initialize maximum to zero
%%================================================================================================
for i=1:256
P(i)=n(i)/N; %Computing the probability of each intensity level
end
%%================================================================================================
for T=2:255 % step through all thresholds from 2 to 255
w0=sum(P(1:T)); % Probability of class 1 (separated by threshold)
w1=sum(P(T+1:256)); %probability of class2 (separated by threshold)
u0=dot([0:T-1],P(1:T))/w0; % class mean u0
u1=dot([T:255],P(T+1:256))/w1; % class mean u1
sigma=w0*w1*((u1-u0)^2); % compute sigma i.e variance(between class)
if sigma>max % compare sigma with maximum
max=sigma; % update the value of max i.e max=sigma
threshold=T-1; % desired threshold corresponds to maximum variance of between class
end
end
%%====================================================================================================
bw=im2bw(img,threshold/255); % Convert to Binary Image
figure ,imshow(bw); % Display the Binary Image
b = imclearborder(bw);
figure,imshow(b);
%%====================================================================================================
end
I am using this otsu thresholding now if i want to remove borders i am not able to using imclearborder()
These are few images that i want border to be remove so i can get better segmentation

Sign in to comment.


Image Analyst
Image Analyst on 10 Apr 2022
Again, it won't help you segment the melanoma any better if you get rid of the dark, surrounding background somehow. However if you insist, you can replace it with the average color of the skin like this:
% Demo by Image Analyst, April 10, 2022.
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 = 'melanoma.jpg';
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~isfile(fullFileName)
% 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
fullFileName = fullFileNameOnSearchPath;
end
rgbImage = imread(fullFileName);
[rows, columns, numberOfColorChannels] = size(rgbImage)
% Display the image.
subplot(2, 2, 1);
imshow(rgbImage, []);
axis('on', 'image');
caption = sprintf('RGB 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';
%--------------------------------------------------------------------------------------------------------
% Segment (mask) the image.
grayImage = rgb2gray(rgbImage);
% For some reason, the gray scale image has 255 on all edges.
% Remove that so the filling won't fill the entire image.
grayImage(1,:) = 0;
grayImage(end,:) = 0;
grayImage(:, 1) = 0;
grayImage(:, end) = 0;
mask = imbinarize(grayImage);
subplot(2, 2, 2);
imshow(mask, []);
axis('on', 'image');
title('Initial Mask', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Get the mean color of the masked area
[r, g, b] = imsplit(rgbImage);
meanr = mean(r(mask))
meang = mean(g(mask))
meanb = mean(b(mask))
% Get mask of skin only.
mask = imfill(mask, 'holes'); % Fill holes.
skinMask = bwareafilt(mask, 1); % Take largest blob only.
% Display image.
subplot(2, 2, 3);
imshow(skinMask, []);
axis('on', 'image');
title('Skin Mask', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
backgroundMask = ~skinMask; % Will be the dark corners and edges.
% Display image.
subplot(2, 2, 4);
imshow(backgroundMask, []);
axis('on', 'image');
title('Background Mask', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Make the background the same color as the skin
r(backgroundMask) = meanr;
g(backgroundMask) = meang;
b(backgroundMask) = meanb;
rgbImage2 = cat(3, r, g, b);
% Display image.
subplot(2, 2, 4);
imshow(rgbImage2, []);
axis('on', 'image');
title('Repaired Image', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
  4 Comments
JovanS
JovanS on 22 Sep 2022
hello, I tried to use your code and it didn't work. I read that I have to use the Color Thresholder to get thresholds that work for my image. I have these two imagew but i don't know how to find thresholds. I would appreciate it if you could help me. @Image Analyst
Image Analyst
Image Analyst on 22 Sep 2022
@Ioanna St this seems to work.
% Demo by Image Analyst
% Initialization Steps.
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 = 18;
markerSize = 40;
fprintf('Beginning to run %s.m ...\n', mfilename);
%-----------------------------------------------------------------------------------------------------------------------------------
% Read in image.
folder = [];
baseFileName = 'color_wh.jpg';
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~isfile(fullFileName)
% 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
fullFileName = fullFileNameOnSearchPath;
end
rgbImage = imread(fullFileName);
[rows, columns, numberOfColorChannels] = size(rgbImage)
% Display the image.
subplot(2, 2, 1);
imshow(rgbImage, []);
axis('on', 'image');
caption = sprintf('RGB 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';
%--------------------------------------------------------------------------------------------------------
% Segment (mask) the image.
[mask, maskedRGBImage] = createMask(rgbImage);
% Take largest blob only.
mask = bwareafilt(mask, 1);
% Fill any possible holes.
mask = imfill(mask, 'holes');
% Get masked image again with new mask
% Mask image by multiplying each channel by the mask.
maskedRgbImage = rgbImage .* cast(mask, 'like', rgbImage); % R2016b or later. Works for gray scale as well as RGB Color images.
subplot(2, 2, 2);
imshow(mask, []);
axis('on', 'image');
title('Melanoma Mask', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Get the mean color of the masked area
[r, g, b] = imsplit(rgbImage);
meanr = mean(r(mask))
meang = mean(g(mask))
meanb = mean(b(mask))
% Get mask of skin only.
skinMask = ~mask;
% Display image.
subplot(2, 2, 3);
imshow(skinMask, []);
axis('on', 'image');
title('Skin Mask', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
subplot(2, 2, 4);
imshow(maskedRgbImage, []);
axis('on', 'image');
title('Masked Image', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
%================================================================================================================
% Get mask of melanoma.
function [BW,maskedRGBImage] = createMask(RGB)
%createMask Threshold RGB image using auto-generated code from colorThresholder app.
% [BW,MASKEDRGBIMAGE] = createMask(RGB) thresholds image RGB using
% auto-generated code from the colorThresholder app. The colorspace and
% range for each channel of the colorspace were set within the app. The
% segmentation mask is returned in BW, and a composite of the mask and
% original RGB images is returned in maskedRGBImage.
% Auto-generated by colorThresholder app on 22-Sep-2022
%------------------------------------------------------
% Convert RGB image to chosen color space
I = rgb2hsv(RGB);
% Define thresholds for channel 1 based on histogram settings
channel1Min = 0.000;
channel1Max = 1.000;
% Define thresholds for channel 2 based on histogram settings
channel2Min = 0.000;
channel2Max = 1.000;
% Define thresholds for channel 3 based on histogram settings
channel3Min = 0.000;
channel3Max = 0.544;
% Create mask based on chosen histogram thresholds
sliderBW = (I(:,:,1) >= channel1Min ) & (I(:,:,1) <= channel1Max) & ...
(I(:,:,2) >= channel2Min ) & (I(:,:,2) <= channel2Max) & ...
(I(:,:,3) >= channel3Min ) & (I(:,:,3) <= channel3Max);
BW = sliderBW;
% Initialize output masked image based on input image.
maskedRGBImage = RGB;
% Set background pixels where BW is false to zero.
maskedRGBImage(repmat(~BW,[1 1 3])) = 0;
end

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!