# how to reduce run time of this binary 2D region growing code?

조회 수: 2(최근 30일)
John 2015년 6월 9일
답변: Drew Davis 2015년 6월 10일
The following code is part of an image processing segmentation algorithm that needs to run for 1500 images. Also, following segmentation, features are extracted from image objects. The feature set is still being determined so multiple experiments over the data set are being conducted. This code is too expensive, and needs to be optimized. Either the algorithm needs to be changed, some modifications to the code for memory storage/access, or the code needs to be compiled using mcc. If the last option is the correct path to take, how are compiled codes used in MATLAB?
function [grownx,object,numberOfObjects] = growLMRtoday(Original,centroids)
% initialize variables
[N M L] = size(Original);
N2 = 2*N; M2 = 2*M;
originalx = 10^6*ones(N2,M2); % use originalx for testing
originalx(N:N2-1,M:M2-1) = Original;
avgInt = ceil(mean(Original(:)));
% begin loop over all objects
num_objects = size(centroids,1); % loop over the number of objects
object = struct;
for w = 1:num_objects
w
eightN = 0;
sy = round(centroids(w,2)+N-1); sx = round(centroids(w,1)+M-1); % create seed points for first object
object(w).pixels = [sy sx];
m=1;
while( m==1 )
% create simplified and neighborhood variables
sym1 = sy-1; sxm1 = sx-1;
syp1 = sy+1; sxp1 = sx+1;
% calculate distance between seed and 8-connected neighborhood
seedIntensity = originalx(sy,sx);
d1 = abs(seedIntensity - originalx(sym1,sxm1));
x = object(w).pixels;
inObject = neighborRemoval(x,sym1,sxm1);
if (d1 < avgInt && inObject == 1)
object(w).pixels = [object(w).pixels; sym1 sxm1];
end
d2 = abs(seedIntensity - originalx(sym1,sx));
inObject = neighborRemoval(x,sym1,sx);
if (d2 < avgInt && inObject == 1)
object(w).pixels = [object(w).pixels; sym1 sx];
end
d3 = abs(seedIntensity - originalx(sym1,sxp1));
inObject = neighborRemoval(x,sym1,sxp1);
if (d3 < avgInt && inObject == 1)
object(w).pixels = [object(w).pixels; sym1 sxp1];
end
d4 = abs(seedIntensity - originalx(sy,sxm1));
inObject = neighborRemoval(x,sy,sxm1);
if (d4 < avgInt && inObject == 1)
object(w).pixels = [object(w).pixels; sy sxm1];
end
d5 = abs(seedIntensity - originalx(sy,sxp1));
inObject = neighborRemoval(x,sy,sxp1);
if (d5 < avgInt && inObject == 1)
object(w).pixels = [object(w).pixels; sy sxp1];
end
d6 = abs(seedIntensity - originalx(syp1,sxm1));
inObject = neighborRemoval(x,syp1,sxm1);
if (d6 < avgInt && inObject == 1)
object(w).pixels = [object(w).pixels; syp1 sxm1];
end
d7 = abs(seedIntensity - originalx(syp1,sx));
inObject = neighborRemoval(x,syp1,sx);
if (d7 < avgInt && inObject == 1)
object(w).pixels = [object(w).pixels; syp1 sx];
end
d8 = abs(seedIntensity - originalx(syp1,sxp1));
inObject = neighborRemoval(x,syp1,sxp1);
if (d8 < avgInt && inObject == 1)
object(w).pixels = [object(w).pixels; syp1 sxp1];
end
eightN = eightN+1; % count number of 8 neighborhoods analyzed
% end region growing for object w
if ( eightN == length(object(w).pixels) )
break
end
sy = object(w).pixels(eightN+1,1); sx = object(w).pixels(eightN+1,2);
end
end
numberOfObjects = length(object);
% plot all objects in new image
grow = zeros(N2,M2);
for w = 1:numberOfObjects
for n = 1:length(object(w).pixels)
grow(object(w).pixels(n,1),object(w).pixels(n,2))=1; % use w here for gradient of objects
end
end
grown = grow(N:N2-1,M:M2-1);
grownx = grown.*Original;
end
function[inObject] = neighborRemoval(x,testy,testx)
nn = size(x,1);
membership = [];
for ii = 1:nn
if (x(ii,:) == [testy,testx])
membership = [membership; 1];
end
end
inObject = isempty(membership);
end

댓글을 달려면 로그인하십시오.

### 답변(1개)

Drew Davis 2015년 6월 10일
The MATLAB compiler is typically used when you want to deploy programs as standalone applications. If your goal is to compile your function as to reduce time of execution in MATLAB, consider using MATLAB coder to generate MEX functions (compiled MATLAB functions) which may accelerate the run-time of your function.
You may also consider using parfor from the Parallel Computing Toolbox if your algorithm qualifies to increase performance.

댓글을 달려면 로그인하십시오.

### Community Treasure Hunt

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

Start Hunting!

Translated by