How to save binary images png into one tiff format file?
7 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Dear all,
I have saved binary images by this code in format png:
% 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 = 20;
% Specify the folder where the files live.
folder = 'C:/Users/ced0005/DP/DP_framework/DICOM/slicesCT/';
% Check to make sure that folder actually exists. Warn user if it doesn't.
if ~isdir(folder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', folder);
uiwait(warndlg(errorMessage));
return;
end
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(folder, '*.dcm'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(folder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
% Now do whatever you want with this file name,
% such as reading it in as an image array with imread()
grayImage= dicomread(fullFileName);
subplot(1, 2, 1);
imshow(grayImage, []); % Display image.
axis on;
title(baseFileName, 'FontSize', fontSize);
drawnow; % Force display to update immediately.
% Convert to gra scale if necessary.
[rows, columns, numberOfColorChannels] = size(grayImage);
if numberOfColorChannels > 1
% % Máme barevný obraz, musíme ho převést na černobílý = vybereme %zelený kanál
grayImage = grayImage(:, :, 2); % zelený kanál
end
% Do totally unnecessary histogram equalization.
eq_grayImage = histeq(grayImage);%ekvalizace pomocí histogramu obrazu
% Do morphological processing
% Práh pro vytvoření binárního obrazu okolí
thresholdValue = 900;
binaryImage_okoli = grayImage > thresholdValue;
% Odstranění okolí.
binaryImage_okoli = imclearborder(binaryImage_okoli);
% Vyplnění otvorů.
binaryImage_okoli = imfill(binaryImage_okoli, 'holes');
% Vymazání menších otvorů.
binaryImage_okoli = bwareaopen(binaryImage_okoli, 1150);
% Roztažení binárního obrazu pro přesnější segmentaci
se = strel('line',5,100);
binaryImage_okoli= imdilate(binaryImage_okoli,se);
% Display the image.
subplot(1, 2, 2);
imshow(binaryImage_okoli, []);
title('Binarized', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
drawnow;
% Save output image.
baseOutputFileName = sprintf('Binarizace_okoli_%3d.png', k);
fullOutputFileName = fullfile(folder, baseOutputFileName);
imwrite(logical(binaryImage_okoli), fullOutputFileName);
fprintf('Saved %s.\n', fullOutputFileName);
promptMessage = sprintf('Do you want to Continue processing,\nor Quit processing?');
titleBarCaption = 'Continue?';
buttonText = questdlg(promptMessage, titleBarCaption, 'Continue', 'Quit', 'Continue');
if strcmpi(buttonText, 'Quit')
break;
end
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 for Veronika', 'NumberTitle', 'Off')
msgbox('Done with Program');
I would like to save these binary images into one file. And this file has tiff format. Is it possible? Thank you for your answers.
0 comentarios
Respuestas (1)
Walter Roberson
el 18 de Abr. de 2017
imwrite() with 'WriteMode', 'append' can be used with TIFF files.
4 comentarios
Walter Roberson
el 22 de Abr. de 2017
The code correctly writes multiple images to the tif file.
When a TIFF has multiple images, then when you issue a single imread() to read back from the file, you do not get all of the images at one time -- not beside each other, and not as a 3D array or 4D array of images. You have to loop reading the images using indexing.
filename = 'myMultipageFile.tif'
metadata = imfinfo(filename);
num_img = length(metadata);
image_data = imread(filename);
if num_img > 1; image_data(:,:,:,num_img) = 0; end %pre-allocate
for img_num = 2 : num_img
image_data(:,:,:,img_num) = imread(filename, img_num);
end
Ver también
Categorías
Más información sobre Image Segmentation and Analysis en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!