MATLAB: How can i detect the ROI from the Image

ctImage Processing ToolboxkmeansmedicalStatistics and Machine Learning Toolboxtumor

ROI is Tumor in the MRI. I want to extract the roi alone and have to display it?

Best Answer

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.