MATLAB: Newbie looking for advice

advicebad practicegood practiceimage processingnewbie

I've been using Matlab for a little over two months now, and I'm starting to realise how much I'm going to need to use it in future. Basically, I have the following code which reads a time series of images, processes them and separates different areas based on some arbitrary thresholds. The end result is a new series of RGB 'heatmaps' of the different areas and some count values which can be plotted to show the change in size of the areas as the time series progresses:
clear;close all;clc
cd('E:\pics')
im_start=228;
im_stop=266;
o_image=cell([13 3]);
p_image=cell([13 3]);
%Read files and store into a cell array
for im=im_start:im_stop
fileName=(['DSC_0' int2str(im) '.TIF']);
n=im-227;
o_image{n}=imread(fileName);
end
%Light balance and conversion into grayscale, 8x8 blocks, and store into new array
for x=1:numel(o_image)
p_image{x}=rgb2gray(o_image{x});
bg=imopen(p_image{x}, strel('disk', 90));
p_image{x}=p_image{x}-bg;
p_image{x}=imcomplement(p_image{x});
p_image{x}=imadjust(p_image{x});
p_image{x}=imresize(p_image{x},[2096 3896]);
p_image{x}=mat2cell(p_image{x}, repmat(8, 1, size(p_image{x}, 1)/8), repmat(8, 1, size(p_image{x}, 2)/8));
end
%create another new array to hold final stage and declare matrices for
%faster code
m_image=cell(size(p_image));
for x=1:numel(m_image)
m_image{x}=zeros(size(p_image{1}));
end
%calculate means of 8x8 blocks to reduce size of images and store into
%final array
for x=1:numel(m_image)
for y=1:numel(m_image{x})
m_image{x}(y)=mean2(p_image{x}{y});
end
end
%array for storing threshold values
th_array=cell(size(m_image));
for x=1:numel(m_image)
th_array{x}=zeros(size(p_image{1}));
end
%Apply thresholds (RGB)
r_array=th_array;
g_array=th_array;
b_array=th_array;
for x=1:numel(th_array)
for y=1:numel(p_image{x})
if m_image{x}(y)<=110
g_array{x}(y)=255;
elseif m_image{x}(y)>110 && m_image{x}(y)<=152
r_array{x}(y)=255;
elseif m_image{x}(y)>152
b_array{x}(y)=255;
end
end
end
rgb_array=th_array;
for x=1:numel(rgb_array)
rgb_array{x}=cat(3,r_array{x},g_array{x},b_array{x});
end
r_count=cell(size(th_array));
g_count=cell(size(th_array));
b_count=cell(size(th_array));
for x=1:numel(th_array)
f = unique(r_array{x});
r_count{x} = [f,histc(r_array{x}(:),f)];
end
for x=1:numel(th_array)
f = unique(g_array{x});
g_count{x} = [f,histc(g_array{x}(:),f)];
end
for x=1:numel(th_array)
f = unique(b_array{x});
b_count{x} = [f,histc(b_array{x}(:),f)];
end
XX=zeros(39, 1);
YY=zeros(39, 1);
YY2=YY;
YY3=YY;
for x=2:numel(XX)
XX(x)=XX(x-1)+10;
end
for y=1:numel(YY)
YY(y)=r_count{y}(2,2);
YY2(y)=g_count{y}(2,2);
YY3(y)=b_count{y}(2,2);
end
Some of the comments are a bit irrelevant, as I've not updated them with every iteration, and I know that it's considered bad practice to reallocate variables in some of the ways I have. But other than that I was looking for any advice and suggestions on how it could maybe be shortened/optimised and if any parts are considered pointless.

Best Answer

I would not use cells -- at all. Next, I'd add more comments because I don't know what you're doing at every step. And I'm sure some of your double for loops could be vectorized into a single line of code, if I just knew what you were doing. For example the block processing can be done with blockproc() and I'm sttaching demos.
Related Question