Does anyone know how to improve this code?

1 view (last 30 days)
clc
Inputfolder = dir('F:\Projekt Handgeste\Roh\0\*.jpg');
%OutputFolder = 'F:\Projekt Handgeste\FinalBilder\zwischenspeicher\0\';
for i = 1 : length(Inputfolder)
image = Inputfolder(i).name;
img = imread(fullfile(Inputfolder(i).folder,image));
sz=size(img);
r=1;g=2;b=3;y=1;u=2;v=3;
yuv=img;
region=yuv;
for i=1:sz(1)
for j=1:sz(2)
yuv(i,j,y)=(img(i,j,r)+2*img(i,j,g)+img(i,j,b))/4;
yuv(i,j,u)=img(i,j,r)-img(i,j,g);
yuv(i,j,v)=img(i,j,b)-img(i,j,g);
end
end
for i=1:sz(1)
for j=1:sz(2)
if yuv(i,j,u)>20 && yuv(i,j,u)<74
region(i,j,r)=255;
region(i,j,g)=255;
region(i,j,b)=255;
else
region(i,j,r)=0;
region(i,j,g)=0;
region(i,j,b)=0;
end
end
end
out=region;
%filtering
out=im2bw(out);
out=bwareaopen(out,100);
out=imdilate(out,strel('diamond',4));
%retain largest only
res=out;
cc=bwconncomp(res);
arr=(cellfun('length',cc.PixelIdxList));
newLabel=res;
if ~isempty(round(arr))
msz=0;
for i=1:length(arr)
if msz<arr(i:i)
msz=arr(i:i);
index=i;
end
end
labels=labelmatrix(cc);
newLabel=(labels==index);
out=newLabel;
end
out=imfill(out,'holes');
%3 Bilder aufeinander legen
img_new = repmat(out,[1 1 3]);
imgnew = imresize(img_new, [227, 227], 'bilinear');
fName = ['F:\Projekt Handgeste\FinalBilder\zwischenspeicher\0\',num2str(i),'.jpg'];
imwrite(imgnew,fName);
%imwrite(imgnew, fullfile(OutputFolder, 'image.jpg'));
end
How can I improve this code? Does anyone have an idea?

Accepted Answer

Image Analyst
Image Analyst on 12 Jun 2021
Edited: Image Analyst on 12 Jun 2021
Lots of ways. For starters:
  1. Use descriptive variable names. Like not sz but [rows, columns, numberOfColorChannels] = size(rgbImage); No one wants to look at an alphabet soup mess of a code.
  2. Use LOTS of comments. You have far too few of them. I'd triple the number, at least. In my code I make sure I have at least 30% of the lines of code have a comment on them. See attached comment counter program.
  3. Don't use MATLAB function names, like "image" for the names of your variable or it could cause problems. If in doubt or you want to check, use "which -all image"
  4. Use either camelCase or snake_case. So Inputfolder becomes inputFolder. Use lower case for all variable names unless you're starting a new word and in that case capitalize the first letter of the next word.
  5. Use fullfile(folder, baseFileName) instead of messy stuff like ['F:\Projekt Handgeste\FinalBilder\zwischenspeicher\0\',num2str(i),'.jpg']; fullfile() has the advantage of not having to worry about whether your folder has a trailing slash or not or what direction of slashes to use. It figures it out. You can construct a base filename with sprintf(): baseFileName = sprintf('%2.2d.jpg', i);
  6. Don't use i or j as loop iterators since they can be used at the imaginary variable. Use k or row or something else.
  7. Don't use jpg when doing digital image analysis (unless you're trying to figure out how to make the JPG compression artifact less horrible. Use PNG instead - it's become the defacto world standard now for image analysis.
  8. To get separate color channels, use [r, g, b] = imsplit(rgbImage);

More Answers (0)

Community Treasure Hunt

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

Start Hunting!