0

I've been trying to create a bwarea function in MATLAB, I need to write it in Swift but first I need to recreate it in MATLAB. I've read up on the algorithm but it's still confusing and I can't get my function to give me similar results to the official bwarea MATLAB function. My professor told me that the code should work for non-binary images as well.

Here is my code, please help me figure out why the code doesn't work as intended. Use your own image to test it because this is supposed to work for any image. If you need one supplied by me please ask.

clc;
clear;

I = imread('ngc064.jpg');


image_size = size(I);
area_counter = 0.00;

BW = im2bw(I);

for y = 1:2:(image_size(1))
for x = 1:2:(image_size(2))

    z = BW(y,x);
    z_1 = BW(y,x+1);
    z_2 = BW(y+1,x);
    z_3 = BW(y+1,x+1);

    %% All off and All on

    if(z == 0 && z_1 == 0 && z_2 == 0 && z_3 == 0)
        area_counter = area_counter + 0;
    end

    if(z == 1 && z_1 == 1 && z_2 == 1 && z_3 == 1)
        area_counter = area_counter + 1;
    end

    %% 1 on pixel

    if(z == 1 && z_1 == 0 && z_2 == 0 && z_3 == 0)
        area_counter = area_counter + (1/4);
    end

    if(z == 0 && z_1 == 1 && z_2 == 0 && z_3 == 0)
        area_counter = area_counter + (1/4);
    end

    if(z == 0 && z_1 == 0 && z_2 == 1 && z_3 == 0)
        area_counter = area_counter + (1/4);
    end

    if(z == 0 && z_1 == 0 && z_2 == 0 && z_3 == 1)
        area_counter = area_counter + (1/4);
    end


    %% 2 adjacent pixels

    if(z == 1 && z_1 == 1 && z_2 == 0 && z_3 == 0)
        area_counter = area_counter + (1/2);
    end

    if(z == 0 && z_1 == 0 && z_2 == 1 && z_3 == 1)
        area_counter = area_counter + (1/2);
    end

    if(z == 1 && z_1 == 0 && z_2 == 1 && z_3 == 0)
        area_counter = area_counter + (1/2);
    end

    if(z == 0 && z_1 == 0 && z_2 == 1 && z_3 == 1)
        area_counter = area_counter + (1/2);
    end


    %% 2 diagonal pixels

    if(z == 1 && z_1 == 0 && z_2 == 0 && z_3 == 1)
        area_counter = area_counter + (3/4);
    end

    if(z == 0 && z_1 == 1 && z_2 == 1 && z_3 == 0)
        area_counter = area_counter + (3/4);
    end

    %% 3 on pixels

    if(z == 1 && z_1 == 1 && z_2 == 1 && z_3 == 0)
        area_counter = area_counter + (7/8);
    end

    if(z == 0 && z_1 == 1 && z_2 == 1 && z_3 == 1)
        area_counter = area_counter + (7/8);
    end

    if(z == 1 && z_1 == 1 && z_2 == 0 && z_3 == 1)
        area_counter = area_counter + (7/8);
    end

    if(z == 1 && z_1 == 0 && z_2 == 1 && z_3 == 1)
        area_counter = area_counter + (7/8);
    end
end
 end
Rody Oldenhuis
  • 37,726
  • 7
  • 50
  • 96
jnaljo1
  • 13
  • 5
  • Please consult the duplicate. The only modification you have to make is have a separate array that stores the total number of pixels per unique region. Each time you find a unique pixel when you pop it off the stack, increment a counter that is indexed by the ID of the unique region by 1 and the result will be an array of the sizes of all objects in an image. – rayryeng Jan 06 '17 at 15:58
  • thank you however I'm still confused on how to implement what you said. I'm not the best with programming. – jnaljo1 Jan 23 '17 at 19:59

0 Answers0