how to preallocate the variable in matlab code?

1 回表示 (過去 30 日間)
Prerna Surbhi
Prerna Surbhi 2016 年 4 月 19 日
コメント済み: Adam 2016 年 4 月 19 日
while implementing kernal based grapg cut method, i am getting error at line: area(1+1)=999999999; suggestion for correcting the error is line 147: the variable 'area' appears to change size on every loop iteration(within a script). consider preallocation for speed. so please suggest me how to preallocate the variable area,in the matlab code?
i am attaching the code below
clear all; close all;
%%%%%%%%%%%%%%%%%%%%%%%Main inputs and parameters%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%Note: The RBF-kernel parameters are given in function kernel RBF.m%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%Example with a color image%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%path = 'Images\Color_image.jpg';
%im = im2double(imread(path));
%alpha=1; %The weight of the smoothness constraint
%k =8; %The number of regions
%%%%%%%Example with a SAR image corrupted with a multiplicative noise%%%%%%
%%%%%%%%%%%%%%%%Uncomment the following to run the example)%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
path = 'Images\Sar_image.tif';
im = im2double(imread(path));
alpha=0.6;
k =4;
%%%%%%%%%%%%%%%%%%%%%%%%%%Example with a brain image%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%Uncomment the following to run the example)%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%path = 'Images\Brain_image.tif';
%im = im2double(imread(path));
%alpha=0.1;
%k =4;
sz = size(im);
Hc=ones(sz(1:2));
Vc=Hc;
i_ground = 0; % rank of the bakground for plotting, 0: the darkest;
%k-1 the brightest; 99: nowhere
diff=10000;
an_energy=999999999;
iter=0;
iter_v=0;
energy_global_min=99999999;
distance = 'sqEuclidean'; % Feature space distance
% Initialization: cluster the data into k regions
tic,
disp('Start kmeans');
data = ToVector(im);
[idx c] = kmeans(data, k, 'distance', distance,'EmptyAction','drop','maxiter',100);
c=c(isfinite(c(:,1)),:);
k=size(c,1);
k_max=k;
kmean_time=toc;
Dc = zeros([sz(1:2) k],'single');
c;
tic
while iter < 5
iter=iter+1;
clear Dc
clear K
c;
for ci=1:k
K=kernel_RBF(im,c(ci,:));
Dc(:,:,ci)=1-K;
end
clear Sc
clear K
%%The smoothness term
Sc = alpha*(ones(k) - eye(k));
gch = GraphCut('open', Dc, Sc, Vc, Hc);
[gch L] = GraphCut('swap',gch);
[gch se de] = GraphCut('energy', gch);
nv_energy=se+de;
gch = GraphCut('close', gch);
if (nv_energy<=energy_global_min)
diff=abs(energy_global_min-nv_energy)/energy_global_min;
energy_global_min=nv_energy;
L_global_min=L;
k_max=k;
nv_energy;
iter_v=0;
% Calculate region Pl of label l
if size(im, 3)==3 % Color image
for l=0:k-1
Pl=find(L==l);
card=length(Pl);
K1=kernel_RBF(im(Pl),c(l+1,1));K2=kernel_RBF(im(Pl),c(l+1,2));K3=kernel_RBF(im(Pl),c(l+1,3));
smKI(1)=sum(im(Pl).*K1); smKI(2)=sum(im(Pl+prod(sz(1:2))).*K2); smKI(3)=sum(im(Pl+2*prod(sz(1:2))).*K3);
smK1=sum(K1);smK2=sum(K2);smK3=sum(K3);
if (card~=0)
c(l+1,1)=smKI(1)/smK1;c(l+1,2)=smKI(2)/smK2;c(l+1,3)=smKI(3)/smK3;
else
c(l+1,1)=999999999;c(l+1,2)=999999999;c(l+1,3)=999999999;
end
end
end
if size(im, 1)==1 % Gray-level image
for l=0:k-1
Pl=find(L==l);
card=length(Pl);
K=kernel_RBF(im(Pl),c(l+1,1));
smKI=sum(im(Pl).*K);
smK=sum(K);
if (card~=0)
c(l+1,1)=smKI/smK;
else
c(l+1,1)=999999999;
end
end
end
c=c(c(:,1)~=999999999,:);
c_global_min=c;
k_global=length(c(:,1));
k=k_global;
else
iter_v=iter_v+1;
%---------------------------------
% Begin updating labels
%---------------------------------
% Calculate region Pl of label l
if size(im, 3)==3 % Color image
for l=0:k-1
Pl=find(L==l);
card=length(Pl);
K1=kernel_RBF(im(Pl),c(l+1,1));K2=kernel_RBF(im(Pl),c(l+1,2));K3=kernel_RBF(im(Pl),c(l+1,3));
smKI(1)=sum(im(Pl).*K1); smKI(2)=sum(im(Pl+prod(sz(1:2))).*K2); smKI(3)=sum(im(Pl+2*prod(sz(1:2))).*K3);
smK1=sum(K1);smK2=sum(K2);smK3=sum(K3);
% Calculate contour Cl of region Pl
if (card~=0)
c(l+1,1)=smKI(1)/smK1;c(l+1,2)=smKI(2)/smK2;c(l+1,3)=smKI(3)/smK3;
else
c(l+1,1)=999999999;c(l+1,2)=999999999;c(l+1,3)=999999999;
area(l+1)=999999999;
end
end
end
if size(im, 3)== 1 % Gray-level image
for l=0:k-1
Pl=find(L==l);
card=length(Pl);
K=kernel_RBF(im(Pl),c(l+1,1));
smKI=sum(im(Pl).*K);
smK=sum(K);
% Calculate contour Cl of region Pl
if (card~=0)
c(l+1,1)=smKI/smK;
else
c(l+1,1)=999999999;
area(l+1)=999999999;
end
end
end
c=c(c(:,1)~=999999999,:);
k=length(c(:,1));
end
end
L=L_global_min;
energy_global_min;
c=c_global_min;
size(c,1)
iter;
%Show the results
if size(im, 3)==3 % Color image
img=zeros(sz(1),sz(2),3);
j=1;
imagesc(im); axis off; hold on;
for i=0:k_max-1
LL=(L_global_min==i);
is_zero=sum(sum(LL));
if is_zero
img(:,:,1)=img(:,:,1)+LL*c(j,1);
img(:,:,2)=img(:,:,2)+LL*c(j,2);
img(:,:,3)=img(:,:,3)+LL*c(j,3);
j=j+1;
end
if i~=i_ground
color=[rand rand rand];
contour(LL,[1 1],'LineWidth',2.5,'Color',color); hold on;
end
end
figure(2);
imagesc(img); axis off;
end
if size(im, 3)==1 % Gray-level image
img=zeros(sz(1),sz(2));
j=1;
imagesc(im); axis off; hold on; colormap gray;
for i=0:k_max-1
LL=(L_global_min==i);
is_zero=sum(sum(LL));
if is_zero
img(:,:,1)=img(:,:,1)+LL*c(j,1);
j=j+1;
end
if i~=i_ground
color=[rand rand rand];
contour(LL,[1 1],'LineWidth',2.5,'Color',color); hold on;
end
end
figure(2);
imagesc(img); axis off;
end

採用された回答

Adam
Adam 2016 年 4 月 19 日
That's way too much unformatted code to look through everything, but
area = zeros(k,1);
will pre-allocated the variable 'area' (or alternatively zeros(1,k) if you specifically want a row vector).
Pre-allocation is a speed improvement suggestion though. You say you are getting an error, which is a different matter if it is actually an error and making a speed improvement won't fix an error.
  2 件のコメント
Prerna Surbhi
Prerna Surbhi 2016 年 4 月 19 日
thanks for your advise, successfully solved the preallocation problem, but getting error at another line
Adam
Adam 2016 年 4 月 19 日
This is completely independent of the question you are asking at the top. But since Guillaume is already responding to this problem below I won't double up on giving the same advice to that too!

サインインしてコメントする。

その他の回答 (2 件)

Guillaume
Guillaume 2016 年 4 月 19 日
Since your loop runs from 0 to k and you're using the loop index + 1 to assign to area, its maximum size is going to be k columns, thus:
area = zeros(1, k);
However, if you get an error, preallocation in itself is not going to make it go away. Please give the exact text of the error message.
  2 件のコメント
Prerna Surbhi
Prerna Surbhi 2016 年 4 月 19 日
thanks for your advise, successfully solved the preallocation problem, but getting error at another line
Guillaume
Guillaume 2016 年 4 月 19 日
Well, the error message is clear, you don't have a function ToVector. Either it does not exist or the folder where that function exist is not on matlab's path.
If the former, create that function, if the latter, add that function's folder to matlab's path.

サインインしてコメントする。


Bjorn Gustavsson
Bjorn Gustavsson 2016 年 4 月 19 日
It is often simple, sometimes tedious other times impossible to preallocate the exact size of your variables. In this case I cant easily see. What you do is before the loop (if possible):
area = zeros(1,max_of_number_l);
If possible another trick is to run for-loops from the largest index downwards:
for i1 = max_i:-1:1,
area(i1) = sum(randn(1,10));
end
A more exhaustive description of allocation issues in matlab you can find here: Preallocation performance
HTH
  1 件のコメント
Prerna Surbhi
Prerna Surbhi 2016 年 4 月 19 日
thanks for your advise, successfully solved the preallocation problem, but getting error at another line

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeImage Segmentation and Analysis についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by