Explanation of source code written in Matlab

2 visualizzazioni (ultimi 30 giorni)
Ba Ba Black Sheep!
Ba Ba Black Sheep! il 3 Ott 2017
Commentato: Jan il 5 Ott 2017
What does the following function accomplish?
Can anyone tell me what the following code is doing and how is it doing that?
Is it some kind of region-growing code?
function range=range_gradient(img4corr, p, sz, range, centroid, var, w, tres)
% support function
max_range=size(range,1);
corr_values=zeros(max_range,1);
gauss=gaussiana(centroid(1)-range(1,1),var,max_range);
gauss=gauss./max(gauss);
% calculate the local correlation and weight with the global one and modify them with gaussian
for i=1:max_range
corr_local=correlazione2d_local(img4corr,centroid,range(i,:),sz,0);
corr_global=correlazione2d_local(img4corr,p,range(i,:),sz,0);
corr_values(i,1)= w(1)*corr_global + w(2)*corr_local;
end;
corr_values=corr_values.*gauss(1:max_range)';
% do a search from the centroid
% corr_values
% centroid
up=0;
do=0;
u_index=centroid(1)-range(1,1);
% u_index
d_index=centroid(1)-range(1,1);
% d_index
while( ~(up && do))
if(~up)
if(corr_values(u_index,1)<=tres||u_index<=1)
up=1;
else
u_index=u_index-1;
end;
end;
if(~do)
if(corr_values(d_index,1)<=tres||d_index>=max_range)
do=1;
else
d_index=d_index+1;
end;
end;
end;
range=zeros(max_range,1);
range(u_index:d_index,1)=1;
  2 Commenti
Image Analyst
Image Analyst il 4 Ott 2017
Not sure. Can't you ask the author? The code has very few meaningful comments and cryptic variable names so it's hard to follow. Plus, range() is a built-in function name so they should not use that as a variable name.
Ba Ba Black Sheep!
Ba Ba Black Sheep! il 5 Ott 2017
The following function calls that source code,
function grown_scratch = growing (img4growing, scratch_core, p, sz, max_range, siz)
% GROWING from the scratch skeleton located by a p point and the global correlation matrix identifies the scratch points.
%
% SCRATCH_GROWING = GROWING (IMG, SCRATCH, P, SZ, MAX_RANGE, A)
% IMG: image where the defect is with appropriate padding
% SCRATCH_CORE: binary mask containing scratch skeleton
% P: Starting point from which to start local search
% SZ: size of the square of local correlation
% (SZ X SZ)
% MAX_RANGE: Maximum size expected for scratch thickness
% Returns:
% SCRATCH_GROWING is a mask containing the points considered to be scratch.
[m, n] = size (img4growing);
pad = floor (sz / 2);
c1 = 0.85; % weight of global correlation
c2: = 0.15; % weight of local correlation
% initial value of Gaussian variance
var_gaussian_sx = max_range;
var_gaussian_dx = max_range;
% threshold of correlation
threshold = 0:08;
grown_scratch = zeros (m, n);
leftFlag = 0; % check if I have dragged the left edge of the image
rightFlag = 0; % check if I have dragged the right edge of the image
newLeftPoint = p + 1; % the new point to the left
pdx = p-1; % new point on the right
% max_range is the maximum value of the thickness I want
v = floor (max_range / 2);
while (+ leftFlag rightFlag ~ = 2)
    I have to iterate as long as I have not reached both sides
    if (~ leftFlag)
        newLeftX = newLeftPoint (2) -1;
        % get all Ys where x is 1
        row = find (scratch_core (:, newLeftX) == 1);
        how many Ys have you got?
        s = size (row); % control if in the core at this c there is a point at 1
        If there is only one row, and
        % if the value is between 0 and m.
        if (s (1) == 1 && ((row> 0) && (row <= m)))
            lmin = max (row-v, 1 + pad);
            Imax = min (row + v, m-pad);
            % Range = range_gradient (img (lmin: lmax, s), row-lmin);
            range = zeros (lmax-lmin + 1.2);
            range (:, 2) = newLeftX;
            range (:, 1) = lmin: lmax;
            range = range_gradient (img4growing, p, range, range, [row, newLeftX], var_gaussian_sx, [c1, c2], threshold);
            var_gaussian_sx = max (0.85 * var_gaussian_sx + 0.15 * ((sum (range)) ^ 2), max_range / 10);
            grown_scratch (lmin + 1: lmax + 1, newLeftX) = range;
            newLeftPoint = [row, newLeftX];
        else
            newLeftPoint = [newLeftPoint (1), newLeftX];
        end;
        
        if (newLeftPoint (2) <= 1 || newLeftPoint (1) <= 1 || newLeftPoint (1)> = m)
            leftFlag = 1;
        end;
    end;
        
    if (~ rightFlag)
        newLeftX = pdx (2) +1;
        
        row = find (scratch_core (:, newLeftX) == 1);
        
        s = size (row); % control if in the core at this c there is a point at 1
        
        if (s (1) == 1 && ((row> 0) && (row <= m)))
            lmin = max (row-v, 1 + pad);
            Imax = min (row + v, m-pad);
            % Range = range_gradient (img (lmin: lmax, s), row-lmin);
            range = zeros (lmax-lmin + 1.2);
            range (:, 2) = newLeftX;
            range (:, 1) = lmin: lmax;
            range = range_gradient (img4growing, p, range, range, [row, newLeftX], var_gaussian_dx, [c1, c2], threshold);
            var_gaussian_dx = max (0.85 * var_gaussian_dx + 0.15 * ((sum (range)) ^ 2), max_range / 10);
            grown_scratch (lmin + 1: lmax + 1, newLeftX) = range;
            pdx = [row, newLeftX];
        else
            pdx = [pdx (1), newLeftX];
        end;
                
        if (pdx (2)> = n || pdx (1) <= 1 || pdx (1)> = m)
            rightFlag = 1;
        end;
        
   end;
end;
scratch_growing_h = grown_scratch (1: m, 1: n);
grown_scratch = zero (siz (1), siz (2));
grown_scratch (1: end, 1: end) = scratch_growing_h ((pad + 1): end- (pad), (pad + 1): end- (pad));

Accedi per commentare.

Risposte (1)

Jan
Jan il 4 Ott 2017
As long as the code does not contain meaningful comments and a clear help section, it is doing "something". There is no chance to understand reliably, what this is and how the inputs must be defined to get a proper result. You (or we) can guess a little bit, but this is a waste of time. Do not use this code for a productive code. Rewriting from scratch what you need will be more efficient.
  2 Commenti
Ba Ba Black Sheep!
Ba Ba Black Sheep! il 5 Ott 2017
The following function calls that source code,
function grown_scratch = growing (img4growing, scratch_core, p, sz, max_range, siz)
% GROWING from the scratch skeleton located by a p point and the global correlation matrix identifies the scratch points.
%
% SCRATCH_GROWING = GROWING (IMG, SCRATCH, P, SZ, MAX_RANGE, A)
% IMG: image where the defect is with appropriate padding
% SCRATCH_CORE: binary mask containing scratch skeleton
% P: Starting point from which to start local search
% SZ: size of the square of local correlation
% (SZ X SZ)
% MAX_RANGE: Maximum size expected for scratch thickness
% Returns:
% SCRATCH_GROWING is a mask containing the points considered to be scratch.
[m, n] = size (img4growing);
pad = floor (sz / 2);
c1 = 0.85; % weight of global correlation
c2: = 0.15; % weight of local correlation
% initial value of Gaussian variance
var_gaussian_sx = max_range;
var_gaussian_dx = max_range;
% threshold of correlation
threshold = 0:08;
grown_scratch = zeros (m, n);
leftFlag = 0; % check if I have dragged the left edge of the image
rightFlag = 0; % check if I have dragged the right edge of the image
newLeftPoint = p + 1; % the new point to the left
pdx = p-1; % new point on the right
% max_range is the maximum value of the thickness I want
v = floor (max_range / 2);
while (+ leftFlag rightFlag ~ = 2)
    I have to iterate as long as I have not reached both sides
    if (~ leftFlag)
        newLeftX = newLeftPoint (2) -1;
        % get all Ys where x is 1
        row = find (scratch_core (:, newLeftX) == 1);
        how many Ys have you got?
        s = size (row); % control if in the core at this c there is a point at 1
        If there is only one row, and
        % if the value is between 0 and m.
        if (s (1) == 1 && ((row> 0) && (row <= m)))
            lmin = max (row-v, 1 + pad);
            Imax = min (row + v, m-pad);
            % Range = range_gradient (img (lmin: lmax, s), row-lmin);
            range = zeros (lmax-lmin + 1.2);
            range (:, 2) = newLeftX;
            range (:, 1) = lmin: lmax;
            range = range_gradient (img4growing, p, range, range, [row, newLeftX], var_gaussian_sx, [c1, c2], threshold);
            var_gaussian_sx = max (0.85 * var_gaussian_sx + 0.15 * ((sum (range)) ^ 2), max_range / 10);
            grown_scratch (lmin + 1: lmax + 1, newLeftX) = range;
            newLeftPoint = [row, newLeftX];
        else
            newLeftPoint = [newLeftPoint (1), newLeftX];
        end;
        
        if (newLeftPoint (2) <= 1 || newLeftPoint (1) <= 1 || newLeftPoint (1)> = m)
            leftFlag = 1;
        end;
    end;
        
    if (~ rightFlag)
        newLeftX = pdx (2) +1;
        
        row = find (scratch_core (:, newLeftX) == 1);
        
        s = size (row); % control if in the core at this c there is a point at 1
        
        if (s (1) == 1 && ((row> 0) && (row <= m)))
            lmin = max (row-v, 1 + pad);
            Imax = min (row + v, m-pad);
            % Range = range_gradient (img (lmin: lmax, s), row-lmin);
            range = zeros (lmax-lmin + 1.2);
            range (:, 2) = newLeftX;
            range (:, 1) = lmin: lmax;
            range = range_gradient (img4growing, p, range, range, [row, newLeftX], var_gaussian_dx, [c1, c2], threshold);
            var_gaussian_dx = max (0.85 * var_gaussian_dx + 0.15 * ((sum (range)) ^ 2), max_range / 10);
            grown_scratch (lmin + 1: lmax + 1, newLeftX) = range;
            pdx = [row, newLeftX];
        else
            pdx = [pdx (1), newLeftX];
        end;
                
        if (pdx (2)> = n || pdx (1) <= 1 || pdx (1)> = m)
            rightFlag = 1;
        end;
        
   end;
end;
scratch_growing_h = grown_scratch (1: m, 1: n);
grown_scratch = zero (siz (1), siz (2));
grown_scratch (1: end, 1: end) = scratch_growing_h ((pad + 1): end- (pad), (pad + 1): end- (pad));
Jan
Jan il 5 Ott 2017
This is no valid Matlab syntax:
while (+ leftFlag rightFlag ~ = 2)
The code from your original post contains too few comments to be explained efficiently and reliably. After we told you this, you post another code (twice), which contains some non-Matlab lines. This does not help to solve the problem.
Again: Either the author can add more comments and explain the purpose of the code. Or an exhaustive explanation of what happens in it might take hours of investigations with the debugger, varied inputs and a comparison of the outputs, of reading papers and books. Whatever you want to do with the code, I'm convinced that it is more efficient to re-write it from scratch.
Sorry for this pessimistic estimation.

Accedi per commentare.

Tag

Non è stata ancora inserito alcun tag.

Community Treasure Hunt

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

Start Hunting!

Translated by