필터 지우기
필터 지우기

using threshold on 3D images to creat mask for training Unet

조회 수: 4 (최근 30일)
Aisha Al Ghurabi
Aisha Al Ghurabi 2024년 4월 21일
댓글: Walter Roberson 2024년 4월 22일
I am using this code for 128x128x128 3D mirco CT image
% Open input and output files
fid3DOCT = fopen('SampleA_128x3_#1.raw','r');
fid3DOCTout = fopen('SampleA_128x3_#1_segmented.raw','w');
% Define image dimensions
nx = 128;
ny = 128;
nz = 128;
% Read input image
A = fread(fid3DOCT,nx*ny*nz, 'uint8');
A = uint8(reshape(A,nx,ny,nz));
%Define thresholds for segmentation
threshold1 = 48; % Threshold for class 1
threshold2 = 58; % Threshold for class 2
% You can adjust these thresholds as needed
% Apply segmentation
outputImage = zeros(size(A)); % Initialize segmented image
outputImage(A <= threshold1) = 1; % Class 1
outputImage(A > threshold1 & A <= threshold2) = 2; % Class 2
outputImage(A > threshold2) = 3; % Class 3
% Write segmented image to output file
for i = 1:nz
fwrite(fid3DOCTout, outputImage(:,:,i), 'uint8');
end
but what I get is a black image
I attached the image I am working on it is actually .raw but I saved as .png so I can attach it
Can you please see why the result is black
  댓글 수: 2
Walter Roberson
Walter Roberson 2024년 4월 21일
You might want to switch to using multibandread for the reading section.
Walter Roberson
Walter Roberson 2024년 4월 21일
You will get out a file that is uint8 1, 2 or 3. That is going to look black unless you use imagesc()

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

답변 (1개)

Thomas
Thomas 2024년 4월 21일
Your segmentation routine is correct. However, the issue is that the visualization of a uint8 or double composed of index values of [1, 2, 3] will 'appear' black without scaling. For example using your raster image:
im = imread('SampleA_128x3_#1.png');
%Define thresholds for segmentation
threshold1 = 48; % Threshold for class 1
threshold2 = 58; % Threshold for class 2
% Apply segmentation
outputImage = zeros(size(im)); % Initialize segmented image
outputImage(im <= threshold1) = 1; % Class 1
outputImage(im > threshold1 & im <= threshold2) = 2; % Class 2
outputImage(im > threshold2) = 3; % Class 3
% display thresholded image with scaling
imagesc(outputImage);
  댓글 수: 2
Aisha Al Ghurabi
Aisha Al Ghurabi 2024년 4월 22일
I get it but I want to save the segmented image so I can use it as a mask for training the Unet
Walter Roberson
Walter Roberson 2024년 4월 22일
Change to
outputImage = zeros(size(A), 'uint8'); % Initialize segmented image
and imwrite() outputImage

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

카테고리

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

제품


릴리스

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by