Is it possible to freehand mask a DICOM image? I have located a demo from another thread from Image Analyst, however it uses a Matlab demo image. I was wondering if it were possible to locate and use a DICOM image in this demo code for freehand masking? Thanks.
MATLAB: Freehand Masking a DICOM image
freehandmaskingimage processing
Related Solutions
Here's one way:
% Demo to extract and swap bitplanes in a gray scale image.
clc; % Clear the command window.
workspace; % Make sure the workspace panel is showing.
fontSize = 20;% Read in a standard MATLAB gray scale demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');button = menu('Use which demo image?', 'CameraMan', 'Moon', 'Eight', 'Coins', 'Pout');if button == 1 baseFileName = 'cameraman.tif';elseif button == 2 baseFileName = 'moon.tif';elseif button == 3 baseFileName = 'eight.tif';elseif button == 4 baseFileName = 'coins.png';else baseFileName = 'pout.tif';end% Read in a standard MATLAB gray scale demo image.folder = fullfile(matlabroot, '\toolbox\images\imdemos');% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);if ~exist(fullFileName, 'file') % Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file') % Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName); uiwait(warndlg(errorMessage)); return; endendgrayImage = imread(fullFileName);% Get the dimensions of the image. numberOfColorBands should be = 1.
[rows columns numberOfColorBands] = size(grayImage);% Display the original gray scale image.
subplot(2, 1, 1);imshow(grayImage, []);title('Original Grayscale Image', 'FontSize', fontSize);% Get the 3rd bitplane
bitPlane3Image = bitget(grayImage, 3);% Get the 5th bitplane
bitPlane5Image = bitget(grayImage, 5);% Make the 3rd bitplane the 5th one.
grayImage = bitset(grayImage, 3, bitPlane5Image);% Make the 5th bitplane the 3rd one.
grayImage = bitset(grayImage, 5, bitPlane3Image);% Display the original gray scale image.subplot(2, 1, 2);imshow(grayImage, []);title('Modified Grayscale Image', 'FontSize', fontSize);fprintf('Done running %s.m.\n', mfilename);msgbox('Done with demo');
It definitely DOES work, but you need to make your mask in the range 0-1. Here's proof:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clearvars;workspace; % Make sure the workspace panel is showing.
fontSize = 20;format compact;% Read in a standard MATLAB color demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');baseFileName = 'peppers.png';fullFileName = fullfile(folder, baseFileName);% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);if ~exist(fullFileName, 'file') % Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file') % Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName); uiwait(warndlg(errorMessage)); return; endendrgbImage = imread(fullFileName);% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows columns numberOfColorBands] = size(rgbImage);% Display the original color image.
subplot(2, 2, 1);imshow(rgbImage, []);title('Original Color Image', 'FontSize', fontSize);% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]); % Maximize figure.
mask = zeros(rows, columns, 'uint8');mask(30:300, 50:150) = 1;% Normalize to 0 - 1.
mask = mask / 255;% Display the mask.
subplot(2, 2, 2);imshow(mask, []);title('Mask', 'FontSize', fontSize);maskedRgbImage = bsxfun(@times, rgbImage, cast(mask,class(rgbImage)));% Display the mask.subplot(2, 2, 3);imshow(maskedRgbImage, []);title('MaskedImage', 'FontSize', fontSize);
Best Answer