dimanche 12 juillet 2020

Why absolute value difference test does not give correct results in MATLAB

I have a gray-scale image read in MATLAB and I want to calculate the ratio of how many pixels have the same value or different from their left neighbors by absolute threshold differences [0,1,3]. Initially I was using the following code:

[row,col] = size(processed_map);
flagged_map = zeros(row,col);

for thres = [0,1,3]

    cnt = 0;
    for i=1:row
        if (mod(i, 10) == 0)

            %fprintf('Line %i is processed .\n', i); 
        end
        
        for j =2:col
           
            left_n = processed_map(i,j-1);
            current = processed_map(i,j);    
                            
            if(abs(left_n - current) <= thres)
                fprintf('Current Pixel Value = %i , its neighbour value = %i \n',current,left_n);
                cnt = cnt + 1;
                flagged_map(i,j) = 1;


            end
 
        end

    end
      format long g  
      Ratio = round(100 * cnt/(row * (col-1)),2);

     
     fprintf('DELTA = %i : %.2f \n',thres,Ratio);
      
end

For a threshold value of '0', meaning I am looking for same neighbor values. When it enters the 'if condition of same value' I am getting some printed statements outputs as

Current Pixel Value = 118 , its neighbour value = 53 
Current Pixel Value = 119 , its neighbour value = 118 

Which is not correct of course! And I could not figure why! However when I use the following if

statement for threshold = 0

if(left_n == current)

All entered current & left_n pixels are as expected. Same errors occur for other threshold absolute conditions when written as the given code.

Aucun commentaire:

Enregistrer un commentaire