I am working on to pre process my ultrasound images using loop.But i couldnt save the output of the preprocessed images from the loop.

2 次查看(过去 30 天)
myDir = uigetdir('*.m'); %gets directory
myFiles = dir(fullfile(myDir,'*.png')); %gets all png files in struct
for k = 1:length(myFiles)
baseFileName = myFiles(k).name;
fullFileName = fullfile(myDir, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
%[imData, Fs] = imread(fullFileName);
myFiles.name;
filename = myFiles.name;
img = imread(filename);
img = im2gray(img); % need to work on the right image
% use the actual image dimensions
[nrows,ncols,~] = size(img);
for row = 1:nrows
for col = 1:ncols
% image origin is NW corner, so a line which points from SW to NE
% actually has negative slope!
if row < (-0.77*col + 253.64)
img(row,col) = 0; % i'm using gray so it's easy to see
end
end
end
%imshow(img)
%filename = '181.png';
%img = imread(filename);
%img = im2gray(img); % need to work on the right image
% use the actual image dimensions
[nrows,ncols,~] = size(img);
for row = 1:nrows
for col = 1:ncols
% image origin is NW corner, so a line which points from SW to NE
% actually has negative slope!
if row < (0.77*col - 238.31)
img(row,col) = 0; % i'm using gray so it's easy to see
end
end
end
cropped=img(5:370,31:609);
imshow(img)
imwrite(cropped, filename, 'png') ;
end

回答(1 个)

Sulaymon Eshkabilov
The code is overwriting the already existing file with a changed size and recalling it again. Thuis, the file name change was necessary. Here is the corrected code:
clearvars
myDir = uigetdir('*.m'); %gets directory
myFiles = dir(fullfile(myDir,'*.png')); %gets all png files in struct
for k = 1:length(myFiles)
baseFileName = myFiles(k).name;
fullFileName = fullfile(myDir, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
%[imData, Fs] = imread(fullFileName);
FName= myFiles.name;
img = imread(FName);
img = im2gray(img); % need to work on the right image
% use the actual image dimensions
[nrows,ncols,~] = size(img);
for row = 1:nrows
for col = 1:ncols
% image origin is NW corner, so a line which points from SW to NE
% actually has negative slope!
if row < (-0.77*col + 253.64)
img(row,col) = 0; % i'm using gray so it's easy to see
end
end
end
%imshow(img)
%filename = '181.png';
%img = imread(filename);
%img = im2gray(img); % need to work on the right image
% use the actual image dimensions
[nrows,ncols,~] = size(img);
for row = 1:nrows
for col = 1:ncols
% image origin is NW corner, so a line which points from SW to NE
% actually has negative slope!
if row < (0.77*col - 238.31)
img(row,col) = 0; % i'm using gray so it's easy to see
end
end
end
% New and corrected images aer writtedn under different file names, e.g.
% NEW1.png from 181.png, NEW2.png from 182.png, NEW3.png from 183.png
FName = strcat(['NEW', num2str(k), '.png']);
cropped=img(5:370,31:609);
imshow(img)
imwrite(cropped, FName) ;
end

类别

Help CenterFile Exchange 中查找有关 Environment and Settings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by