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.

4 views (last 30 days)
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

Answers (1)

Sulaymon Eshkabilov
Sulaymon Eshkabilov on 3 Feb 2023
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

Categories

Find more on Images in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!