how can i detect the ROI from the Image ?
4 views (last 30 days)
Show older comments
ROI is Tumor in the MRI. I want to extract the roi alone and have to display it?
4 Comments
Jan
on 3 Apr 2017
@sam CP and Cedric pton: I do not understand the reason to set flags. The posted image does not allow to recognize the person. It does not contain any comments, no date of birth e.g. The code does not contain "private" details also.
Therefore I've removed both flags. But if you see a real problem, please explain this in detail or ask the admins to clean this thread thread (follow the "Contact us" button and add a link to this thread). Thanks.
Hesham Alghodhaifi
on 13 Jun 2017
Hi Sam CP, Did you find an answer for your question? I want to extract the ROI voxel values.
Accepted Answer
Image Analyst
on 31 Mar 2017
Try this.
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 = 15;
%===============================================================================
% Get the name of the image the user wants to use.
baseFileName = 'tumor.jpg'; % 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(1, 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')
hold on;
drawnow;
% Threshold to get pixels in the range
tumor = (grayImage >= 155) & (grayImage < 189);
% Extract the largest blob;
tumor = bwareafilt(tumor, 1);
% Display the image.
subplot(1, 2, 2);
imshow(tumor, []);
axis on;
caption = sprintf('Tumor Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
By the way, kmeans is not a good way to detect tumors.
6 Comments
Image Analyst
on 2 Apr 2017
There are SO many problems with that it will take me a while to fix it. Like using an RGB image, using "input" as the name of a variable, computing the median filtered image but never using it, and so on.
More Answers (1)
Image Analyst
on 2 Apr 2017
Sam, another problem I found in your code was that you did not realize that kmeans can produce a different cluster index for the tumor each time you run it. So I had to figure out what label it was at and then extract that. Here is the corrected code:
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 = 15;
% Get the name of the image the user wants to use.
folder = pwd;
filename = 'brain2.jpg';
% [filename, folder] = uigetfile({'*.jpg';'*.bmp'},'Select MRI');
inputimage = fullfile(folder, filename);
inputImage = imread(inputimage);
% Display the image
subplot(2, 2, 1);
imshow(inputImage,[]);
axis on image;
title('Input Image','fontsize',fontSize);
% Convert to gray scale if needed.
[rows, columns, numberOfColorChannels] = size(inputImage);
if numberOfColorChannels == 3
fprintf('That was a color image. I am converting it to grayscale.\n');
inputImage = rgb2gray(inputImage);
end
% 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;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Compute the median filtered image.
medianFilteredImage = medfilt2(inputImage);
subplot(2, 2, 2);
imshow(medianFilteredImage,[]);
axis on image;
title('Median Filtered Image','FontSize', fontSize);
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Define some number of clusters that you know will definitely be there.
numberOfClusters = 5;
% Do kmeans clustering on the median filtered image.
grayLevels = double(medianFilteredImage(:));
[clusterIndexes, clusterCenters] = kmeans(grayLevels, numberOfClusters,...
'distance', 'sqEuclidean', ...
'Replicates', 2);
labeledImage = reshape(clusterIndexes, rows, columns);
subplot(2, 2, 3);
imshow(labeledImage,[])
title('Kmeans Clustering','FontSize', fontSize);
axis on image;
% colorbar
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
%===============================================================================
% Now kmeans can give a different index to the tumor in each run,
% so we'll assume the tumor is the brightest class.
% Find the brightest class.
[maxValue, indexOfMaxValue] = max(clusterCenters)
% Get pixels that are labeled as the tumor.
tumor = labeledImage == indexOfMaxValue;
% Extract the largest blob;
tumor = bwareafilt(tumor, 1);
% Fill holes.
tumor = imfill(tumor, 'holes');
% Display the image.
subplot(2, 2, 4);
imshow(tumor, []);
axis on image;
title('Tumor Binary Mask Image','FontSize', fontSize);
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
Again, I do not recommend this method as a good one for finding tumors. It's only use would be as a student exercise in how to use kmeans() on a 2-D array of values, not as an illustration of a good image segmentation method, because it's not.
9 Comments
Jan
on 12 Oct 2017
Edited: Jan
on 12 Oct 2017
[MOVED from flag] abderrahim khatabi wrote:
Hello; i try your code to segmente the tumor but gave me this error: Undefined function 'bwareafilt' for input arguments of type 'double'. how can i slove this problem. thank you in advance.
@abderrahim khatabi; Please use flags only to inform editors and admins, that a contribution might conflict with the terms of use. Thanks.
Image Analyst
on 13 Oct 2017
bwareafilt() was only introduced in R2014b. You must have an earlier version so you should use bwareaopen() instead. See the documentation for how to use it.
See Also
Categories
Find more on Biomedical Imaging 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!