How to arrange pixels for appropriate threshold

3 views (last 30 days)
I have two mammography image .
I want to segment tumor area and for this using threshold method.
But image intensities are different for every image so it affect my result badly.
I tried to calculate mean intensity of image with using "meanIntensity = mean(grayImage(:)) " and if condition statement according this value for threshold, but it doesn't work again in a right way.
I mean mdb005 image I am using threshold > 155 to get good result but for mdb013 I have to choose >210 or other values. (İf I choose >155 it shows huge for mdb013)
But if I choose >210 this time mdb005 tumor dissappear.
What to do for these two image to segment exact tumor automatically.
I used @Image Analystcodes in the past questions and combine with other codes and created this codes.
images attached
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 = 'pat00002_1-1-1-1-2-10-1.mp4.cover.png';
baseFileName = 'mdb005.png';
% Get the full filename, with path prepended.
folder = ['C:\Users\Ali\Desktop\Miaspng']; % 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, 3, 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', 'NumberTitle', 'Off')
drawnow;
meanIntensity = mean(grayImage(:))
if meanIntensity<35
mask = grayImage > 190;
elseif meanIntensity<40
mask = grayImage > 185;
elseif meanIntensity<45
mask = grayImage > 182;
elseif meanIntensity<50
mask = grayImage > 175;
elseif meanIntensity<55
mask = grayImage > 170;
elseif meanIntensity<60
mask = grayImage > 165;
elseif meanIntensity<65
mask = grayImage > 155;
elseif meanIntensity<70
mask = grayImage > 150;
end
% Display the mask image.
subplot(2, 3, 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;
% Find the areas
labeledImage = bwlabel(mask);
props = regionprops(labeledImage, 'Area', 'Centroid');
allAreas = sort([props.Area], 'descend');
allCentroids = [props.Centroid];
centroidsX = allCentroids(1:2:end);
centroidsY = allCentroids(2:2:end);
% Make a margin of 10% of the image size.
marginx = 0.34 * columns;
marginy = 0.15 * rows;
keepers = (centroidsX > marginx & centroidsX < (columns - marginx)) & ...
(centroidsY > marginy & centroidsY < (rows - marginy));
indexes = find(keepers);
% Get a mask with only the keepers in it
newMask = ismember(labeledImage, indexes);
% Display the mask image.
subplot(2, 3, 3);
imshow(newMask);
axis on;
axis image; % Make sure image is not artificially stretched because of screen's aspect ratio.
title('Tags Remove', 'fontSize', fontSize);
drawnow;
% Mask the gray scale image
maskedGrayImage = grayImage; % Initialize.
maskedGrayImage(~newMask) = 0;
% Display the masked grayscale image.
subplot(2, 3, 4);
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;
IM_cb = imclearborder(maskedGrayImage);
BW2 = bwareaopen(IM_cb, 150);
BW_filled = imfill(BW2, 'holes');
subplot(2, 3, 5);
% figure,
imshow(BW_filled);
axis on;
axis image; % Make sure image is not artificially stretched because of screen's aspect ratio.
title('Small Lines Eliminated', 'fontSize', fontSize);
drawnow;
labeledImage = bwlabel(BW_filled);
measurements = regionprops(labeledImage, 'Area');
% Get all the areas
allAreas = [measurements.Area];
[biggestArea, indexOfBiggest] = sort(allAreas, 'descend');
% Extract biggest
biggestBlob = ismember(labeledImage, indexOfBiggest(1));
% Convert to binary
biggestBlob = biggestBlob > 0;
subplot(2, 3, 6);
% figure,
imshow(biggestBlob, []);
axis on;
axis image; % Make sure image is not artificially stretched because of screen's aspect ratio.
title('Final', 'fontSize', fontSize);
drawnow;

Answers (1)

Image Analyst
Image Analyst on 16 May 2021
  10 Comments
Image Analyst
Image Analyst on 6 Jun 2021
Sorry I can't put it on my agenda. I simply don't have enough time to code up all the interesting papers I run across. Plus I don't want to rob you of all the glory you said would come your way once you write it.
Ali Zulfikaroglu
Ali Zulfikaroglu on 7 Jun 2021
Thank u @Image Analyst for everything . You helped a lot already . I will figure out some way

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!