I have the following source image with random squares in black color:

[![source image](https://i.stack.imgur.com/866PX.jpg)](https://i.stack.imgur.com/866PX.jpg)

**Questions:**

1. How can I detect how many squares are there in it? And how to get information about width, height, and start position (x,y) for each square?
2. How can I separate from combine square to a single square (can separately become 2 squares / 3 squares / more / automatically) as shown below:

[![](https://i.stack.imgur.com/txKp5.png)](https://i.stack.imgur.com/txKp5.png)

i am already make some simple simulation according to this problem, here is my code in matlab, now this code completely work to detect square, but when i change the image source to sample file with square, it has some error

`%% First Initialisation
 tic; % Start timer.
 clc; % Clear command window.
 clear;
 close all; % Close all figure windows except those created by imtool.
 imtool close all;
 clearvars; % Get rid of variables from prior run of this m-file.
 workspace; % Make sure the workspace panel is showing.`

```
%Source
%RGB = imread('Result.png');
RGB = [
     0   0   0   0 255 255 255  0   0;
     0   0   0   0 255 255 255  0   0;
     0   0   0   0 255 255 255 255 255;
    255 255 255 255 0   0   0   0   0;
    255 255 255 255 0   0   0   0   0;
     0   0   0   0 255 255 255  0   0;
     0   0   0   0 255 255 255  0   0;
    255 255 255 255 0   0   0   0   0;
    255 255 255 255 0   0   0   0   0;
    ];
figure;
imshow(RGB);
caption = sprintf('Source Image');
title(caption, 'FontSize', 13);

%%Make It White
white=Make_Image_White(RGB);
figure;
imshow(white);
caption = sprintf('White Image');
title(caption, 'FontSize', 13);
RGB=white;

%save x,y location
[yy xx] = find( RGB == 0 );

%%Prepare Struct Variabel to Save vertical Black Line
RectLine=struct('Line',[],'PosX',[],'PosY',[],'Width',[],'Height',[]);

%start counting vertical black line
startNew=0;
line=0;
limit=0;

for repeat = 1 : size(find(RGB==0),1)
    if startNew==0
        line=line+1;
        fprintf('Start The-%d Line....\n',line);
        startNew=1;
        Height=0;
        if limit==1
            PosY=yy(repeat-1);
            PosX=xx(repeat-1);
            fprintf(' =-=-=-=-=-=-=-=-=-=-=-=-Continue \n'); % Message sent to command window.                
            fprintf(' New Box Detection ==> %d \n', line); % Message sent to command window.                
            fprintf('   Position (x,y) => (%d,%d) \n', PosX, PosY); % Message sent to command window.                
            fprintf('   Counting X-%d : (x,y) -> [ %d , %d ] \n', repeat-1, xx(repeat-1), yy(repeat-1)); % Message sent to command window.                
%            fprintf('xx(repeat)==tempX && tempY+1==yy(repeat)\n\r');
%            fprintf('%d==%d && %d==%d\n\r',xx(repeat-1),xx(repeat-1),tempY-1,yy(repeat-1));
            fprintf(' 1.* add height+1 ==> Counting X-%d : (x,y) -> [ %d , %d ] \n', repeat-1, xx(repeat-1), yy(repeat-1)); % Message sent to command window.                
            tempX=xx(repeat);
            Height=Height+1;    
            tempY=yy(repeat-1);
            startNew=startNew+1;
        else
            PosY=yy(repeat);
            PosX=xx(repeat);
            tempX=0;
            tempY=0;
        end
        Width=1;
        startY=PosY;
        startX=PosX;        
        if limit==0
            fprintf(' =-=-=-=-=-=-=-=-=-=-=-=- Real\n'); % Message sent to command window.                
            fprintf(' New Line Detection ==> %d \n', line); % Message sent to command window.                
            fprintf('   Position (x,y) => [ %d , %d ] \n', PosX, PosY); % Message sent to command window.                
            fprintf('   Counting X-%d : (x,y) -> [ %d , %d ] \n', repeat, xx(repeat), yy(repeat)); % Message sent to command window.                
        end

    end

    if (RGB(yy(repeat),xx(repeat),1)==0)
%            fprintf(' =-=Tes Titik [%d,%d] \n',xx(repeat),yy(repeat)); % Message sent to command window.                
        if tempX==0
%            fprintf('%d==%d && %d==%d\n',xx(repeat),tempX,tempY+1,yy(repeat));
            fprintf(' %d.a add height+1 ==> Counting X-%d : (x,y) -> [ %d , %d ] \n', startNew, repeat,xx(repeat), yy(repeat)); % Message sent to command window.                
            tempX=xx(repeat);
            Height=Height+1;    
            tempY=tempY+1;
            startNew=startNew+1;
        elseif xx(repeat)==tempX && tempY+1==yy(repeat)
 %           fprintf('xx(repeat)==tempX && tempY+1==yy(repeat)\n\r');
 %           fprintf('%d==%d && %d==%d\n\r',xx(repeat),tempX,tempY+1,yy(repeat));
            fprintf(' %d.b add height+1 ==> Counting X-%d : (x,y) -> [ %d , %d ] \n', startNew, repeat,xx(repeat), yy(repeat)); % Message sent to command window.                
            Height=Height+1;     
            tempX=xx(repeat);
            tempY=tempY+1;
            startNew=startNew+1;
            if repeat == size(find(RGB==0),1)
                RectLine(line).Line=line;
                RectLine(line).PosX=PosX;
                RectLine(line).PosY=PosY;
                RectLine(line).Width=Width;
                RectLine(line).Height=Height;                
            end
        else
%            fprintf('xx(repeat)==tempX && tempY+1==yy(repeat)\n');
%            fprintf('%d==%d && %d==%d\n',xx(repeat),tempX,tempY+1,yy(repeat));
            limit=1;            
            startNew=0;
            RectLine(line).Line=line;
            RectLine(line).PosX=PosX;
            RectLine(line).PosY=PosY;
            RectLine(line).Width=Width;
            RectLine(line).Height=Height;                
            tempX=xx(repeat);    
        end
    end;
end

fprintf('\n\nStart Combine Line become Box\n');
RectBox=struct('Box',[],'PosX',[],'PosY',[],'Width',[],'Height',[]);
startBox=1;
for line = size(RectLine,1) : size(RectLine,2)
    fprintf(' ************************** Line %d\n',line);
    Width=0;
    fprintf('  Start Point (%d,%d) Height = %d ** Width=%d\n',RectLine(line).PosX,RectLine(line).PosY,RectLine(line).Height,Width);
    if startBox>1
        exist=0;
        for cek = size(RectBox,1) : size(RectBox,2)
            if (RectLine(line).PosX >= RectBox(cek).PosX) && (RectLine(line).PosX <= RectBox(cek).PosX+RectBox(cek).Width-1) ...
                && (RectLine(line).PosY >= RectBox(cek).PosY) && (RectLine(line).PosY <= RectBox(cek).PosY+RectBox(cek).Height-1)
                if (RectLine(line).Height == RectBox(cek).Height)
                    exist=1;  
                elseif (RectLine(line).Height > RectBox(cek).Height)
                    RectLine(size(RectBox,2)+1).Line=size(RectBox,2)+1;
                    RectLine(size(RectBox,2)+1).PosX=RectLine(line).PosX;
                    RectLine(size(RectBox,2)+1).PosY=RectLine(line).PosY;
                    RectLine(size(RectBox,2)+1).Width=1;
                    RectLine(size(RectBox,2)+1).Height=RectLine(line).Height - RectBox(cek).Height;
                end
            end

            if exist==1
                fprintf('  ** Line Start From (%d,%d) -> Already Exist On Box %d\n',RectLine(line).PosX,RectLine(line).PosY,cek);
                break;
            end
        end
        if exist==0
            for x=RectLine(line).PosX : size(RGB,2)
                fprintf('      Cek Find Start Position looping at %d -> (%d,%d)\n',x,RectLine(line).PosX,RectLine(line).PosY);
                fprintf('      find(RGB(%d:%d,%d:%d))\n',RectLine(line).PosY,RectLine(line).PosY+RectLine(line).Height-1,RectLine(line).PosX,x);
                if find(RGB(RectLine(line).PosY:RectLine(line).PosY+RectLine(line).Height-1,RectLine(line).PosX:x))
                    fprintf('  --> Save %d - Box\n',startBox);
                    fprintf('      Start Position (%d,%d)\n',RectLine(line).PosX,RectLine(line).PosY);
                    fprintf('      Width=%d ** Height=%d\n',Width,RectLine(line).Height);
                    RectBox(startBox).Box=startBox;
                    RectBox(startBox).PosX=RectLine(line).PosX;
                    RectBox(startBox).PosY=RectLine(line).PosY;
                    RectBox(startBox).Width=Width;
                    RectBox(startBox).Height=RectLine(line).Height;    
                    startBox=startBox+1;
                    break;
                elseif x==size(RGB,2)
                    Width=Width+1;
                    fprintf(' b--> Save %d - Box\n',startBox);
                    fprintf('      Start Position (%d,%d)\n',RectLine(line).PosX,RectLine(line).PosY);
                    fprintf('      Width=%d ** Height=%d\n',Width,RectLine(line).Height);
                    RectBox(startBox).Box=startBox;
                    RectBox(startBox).PosX=RectLine(line).PosX;
                    RectBox(startBox).PosY=RectLine(line).PosY;
                    RectBox(startBox).Width=Width;
                    RectBox(startBox).Height=RectLine(line).Height;    
                    startBox=startBox+1;
                    break;
                else
                    up0=0;
                    down0=0;
%                    fprintf('Y RGB=%d,%d \n',RectLine(line).PosY-1,x);
                    if RectLine(line).PosY>1
                        if (RGB(RectLine(line).PosY-1,x)>0)
                            up0=1;
                        else
                        end     

                    else
                        up0=1;
                    end

 %                   fprintf('X RGB=%d,%d \n',RectLine(line).PosY+RectLine(line).Height,x);
                    if (RectLine(line).PosY+RectLine(line).Height) <= size(RGB,1)
                        if (RGB(RectLine(line).PosY+RectLine(line).Height,x)>0)
                            down0=1;
                        end   
                    else
                        down0=1;
                    end
%                    fprintf('posy=%d,x=%d,height=%d-->tot=%d\n',RectLine(line).PosY, x, RectLine(line).Height, RectLine(line).PosY + RectLine(line).Height);
%                    fprintf('Status Up=%d ---- Down=%d\n',up0,down0);
                    if (up0==1) && (down0==1)
                        Width=Width+1;
                        fprintf('    Position (%d,%d) -> Width+1 => %d\n',x,RectLine(line).PosY,Width);                        
                    else
                        fprintf('c  --> Save %d - Box\n',startBox);
                        fprintf('      Start Position (%d,%d)\n',RectLine(line).PosX,RectLine(line).PosY);
                        fprintf('      Width=%d ** Height=%d\n',Width,RectLine(line).Height);
                        RectBox(startBox).Box=startBox;
                        RectBox(startBox).PosX=RectLine(line).PosX;
                        RectBox(startBox).PosY=RectLine(line).PosY;
                        RectBox(startBox).Width=Width;
                        RectBox(startBox).Height=RectLine(line).Height;    
                        startBox=startBox+1;
                        break;
                    end
                end
            end
        end
    else
         for x=RectLine(line).PosX : size(RGB,2)
             fprintf('      Cek Find Start Position looping at %d -> (%d,%d)\n',x,RectLine(line).PosX,RectLine(line).PosY);
             fprintf('      find(RGB(%d:%d,%d:%d))\n',RectLine(line).PosY,RectLine(line).PosY+RectLine(line).Height-1,RectLine(line).PosX,x);

            if find(RGB(RectLine(line).PosY:RectLine(line).PosY+RectLine(line).Height-1,RectLine(line).PosX:x))
                fprintf('  --> Save %d - Box\n',startBox);
                fprintf('      Start Position (%d,%d)\n',RectLine(line).PosX,RectLine(line).PosY);
                fprintf('      Width=%d ** Height=%d\n',Width,RectLine(line).Height);
                RectBox(startBox).Box=startBox;
                RectBox(startBox).PosX=RectLine(line).PosX;
                RectBox(startBox).PosY=RectLine(line).PosY;
                RectBox(startBox).Width=Width;
                RectBox(startBox).Height=RectLine(line).Height;    
                startBox=startBox+1;
                break;
            elseif x==size(RGB,2)
                Width=Width+1;
                fprintf(' b--> Save %d - Box\n',startBox);
                fprintf('      Start Position (%d,%d)\n',RectLine(line).PosX,RectLine(line).PosY);
                fprintf('      Width=%d ** Height=%d\n',Width,RectLine(line).Height);
                RectBox(startBox).Box=startBox;
                RectBox(startBox).PosX=RectLine(line).PosX;
                RectBox(startBox).PosY=RectLine(line).PosY;
                RectBox(startBox).Width=Width;
                RectBox(startBox).Height=RectLine(line).Height;    
                startBox=startBox+1;
                break;
            else
                Width=Width+1;
                fprintf('    Position (%d,%d) -> Width+1 => %d\n',x,RectLine(line).PosY,Width);
            end
        end
    end
end

RectBox 

```

expectation result like this

<http://imgur.com/a/0ypc3>