function H = computeH(U,controlPoints,h,iCP,Cval) % Subfunction of the Simulated Annealing program. % Calculates the local energy function associated with a particular % value of a given control point. % Initialize auxiliary variables nOrder = 3; nControlPoints = length(controlPoints); nCBegin = max(1,iCP - nOrder); nCEnd = min(iCP + nOrder, nControlPoints); % Assemble set of control point affected by change. newControlPoints = [ controlPoints(nCBegin:(iCP - 1)); Cval; controlPoints((iCP + 1):nCEnd) ]; % Calculate splines section from control points newContour = formBspline(newControlPoints,h); L = length(newContour); % Calculate potential energy H0 = 0; [nx, ny] = size(U); for i = 1:L, H0 = H0 + U(max(1,min(nx,round(newContour(i)))), (nCBegin-1) * h + i); end % Calculate energy for slope H1 = sum(conv(newContour,[-1 2 -1]).^2); % Calculate overall energy H = H0 + .00015 * H1;