How to save binary images png into one tiff format file?

조회 수: 6 (최근 30일)
Veronika
Veronika 2017년 4월 18일
댓글: Walter Roberson 2017년 4월 22일
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.

답변 (1개)

Walter Roberson
Walter Roberson 2017년 4월 18일
imwrite() with 'WriteMode', 'append' can be used with TIFF files.
  댓글 수: 4
Veronika
Veronika 2017년 4월 19일
Okay, thank you so much. I added these raws here:
clc;
close all;
clear;
workspace;
format long g;
format compact;
fontSize = 20;
folder = 'C:/Users/ced0005/DP/DP_framework/DICOM/slicesCT/';
if ~isdir(folder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', folder);
uiwait(warndlg(errorMessage));
return;
end
filePattern = fullfile(folder, '*.dcm');
theFiles = dir(filePattern);
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(folder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
grayImage= dicomread(fullFileName);
subplot(1, 2, 1);
imshow(grayImage, []);
axis on;
title(baseFileName, 'FontSize', fontSize);
drawnow;
[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
eq_grayImage = histeq(grayImage);
% 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);
subplot(1, 2, 2);
imshow(binaryImage_okoli, []);
title('Binarizovaný obraz', 'FontSize', fontSize);
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
drawnow;
baseOutputFileName = sprintf('Binarizace_okoli_%3d.png', k);
fullOutputFileName = fullfile(folder, baseOutputFileName);
imwrite(logical(binaryImage_okoli), fullOutputFileName);
fprintf('Uloženo %s.\n', fullOutputFileName);
*if k == 1
imwrite(binaryImage_okoli,'myMultipageFile.tif')
else
imwrite(binaryImage_okoli,'myMultipageFile.tif','writemode','append')
end*
end
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
set(gcf, 'Toolbar', 'none', 'Menu', 'none');
set(gcf, 'Name', 'Binarizace testovacích CT skenů', 'NumberTitle', 'Off')
Is it right or not? Because I still see only one image in this myMultipageFile.tif.
Walter Roberson
Walter Roberson 2017년 4월 22일
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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by