所以我试图用这段代码做的是找到图像线上低于某个阈值的所有像素。然而,问题是这段代码是在一个双 for 循环中执行的(是的,我知道 :( ),每个像素一次,所以它非常慢。我想知道我是否还有什么可以做的。
一些技巧会很棒,因为我对 MATLAB 优化很陌生,而且我只知道基础知识(尽量不要使用循环,或者在内部函数中多次调用脚本等)。如果这不起作用,我可能不得不求助于 MEX 文件,这对于我小组中的其他研究人员来说将更难维护。谢谢!
for y = 1:y_len
for x = 1:x_len
%//...do stuff to calc slope and offset for the line,
%//this can be vectorized pretty easily.
yIndices = xIndices.*slope + offset;
yIndices = round(yIndices);
yIndices = yIndices + 1;
xIndices = xIndices + 1;
valid_points = (yIndices <= 308) & (yIndices > 0);
%this line is bottle necking----------------------------------------
valid_points = yIndices(valid_points)+(xIndices(valid_points)-1)*308;
%-------------------------------------------------------------------
valid_points = valid_points(phaseMask_R(valid_points));
t_vals = abs(phase_R(valid_points)-currentPhase);
point_vals = [XsR(valid_points);YsR(valid_points)] - 1;
matchedPtsCoordsR = point_vals(:,(t_vals<phaseThreshold) |(abs(192-t_vals)<phaseThreshold));
matchedIndex = size(matchedPtsCoordsR,2);
if(matchedIndex ==0)
continue
end
centersMinMaxR = zeros(1,matchedIndex);
cmmIndexR = 1;
for a = 1:matchedIndex;
if(a==1)
avgPosition = matchedPtsCoordsR(:,a);
centersMinMaxR(1,1) =1;
else
currentPosition = matchedPtsCoordsR(:,a);
%also very slow----------------------------------------------
distance = sum(abs(currentPosition-avgPosition));
%------------------------------------------------------------
if(distance>4) % We are now likely in a different segment.
centersMinMaxR(2,cmmIndexR) = a-1;
cmmIndexR = cmmIndexR + 1;
centersMinMaxR(1,cmmIndexR) = a;
end
avgPosition = matchedPtsCoordsR(:,a);
end
end
centersMinMaxR(2,cmmIndexR) = a;
centersR = round(sum(centersMinMaxR)/2);
%//...do stuff with centersR
%//I end up concatenating all the centersR into a
%//large vector arrray with the start and end of
%//each segment.