How to regroup or reconstruct connected elements in a binary image into squares

Hello, please I need help with this.
I have a binary image(50x50).
The matrix was randomly generated therefore making the image noisy. My aim is to regroup or reconstruct connected elements of same value together into different square shapes(not necessarily a perfect square), the edges of the square doesn't have to be smooth since the matrix is randomly generated. I did the following but I want to reconstruct all same elements into squares.
I = imread('Fig1.jpg');
BW = imbinarize(I); % Convert image to binary matrix.
L = bwlabeln(BW, 26) % Label connected elements.

4 Comments

Are there any specific conditions for the total number of pixels in a square/area of square?
May be 9 large portions of square/area of square, the background may still have some tiny squares of least connected elements. I've attached a typical randomly generated binary image of (50x50).
After seeing the image, I request you to rethink the question? What is your actual objective?
Hi @KALYAN ACHARJYA, Sorry I didn't respond on time. I was thinking of a better way to put the question. The objective here is to remove the scattered pixels making the image noisy without distorting so much of the main squares/rectangle hidden in the noisy image. I initially thought regrouping them would help but it will be fine to remove them rather.

Sign in to comment.

Answers (1)

Why are you using bwlabeln()? For one thing, that's for 3-D images, not 2-D like you have. And what do you mean "of the same value"? Originally, you only have two values 0 and 1. Do you simply want a square that is all white with as many white dots there are in your original image?
numDots = nnz(BW);
rows = ceil(sqrt(numDots));
whiteSquare = ones(rows, rows);
numBlack = rows * rows - numDots;
if numBlack >= 1
whiteSquare(1:numBlack) = 0;
end
Of course if you label them each connected component will have it's own ID number. Not sure if you want 4 connected or 8 connected but I believe if you use 26 connected on a 2-D image you'll get 8 connected (if it doesn't throw an error).

12 Comments

The background should be white and the foreground should have the black square pixels. If you do 'imread' on the uploaded image, you will notice that some of the ones(at random positions) are very close to the main rectangular/square block of ones. My main aim is to regroup as all the '1's connected to the main blocks together.
The tiny dots should be the '1's not connected to the main blocks of '1's.
L = bwlabeln(BW, 26) worked for the 2-D image without error, although "26" is actually for 3-D image. It might not be necessary but i initially thought labelling them will help me to regroup them.
I have no idea what you want, or why. Mock something up in Photoshop and upload it so we can see.
@Image Analyst Something similar to any of these images. I think another way around it could be to remove the scattered '1's making the image noisy and regroup all the nearest neighbour closest to the main blocks of '1's or possibly remove all the scattered '1's and leave only the main blocks of ones.
Try bwmorph(binaryImage, 'clean') followed by imdilate()
binaryImage = bwmorph(binaryImage, 'clean'); % Remove single pixels.
binaryImage = imdilate(binaryImage, true(3)); % Expand remaining blobs by one later of pixels.
Thank you @Image Analyst, it did removed most of the unwanted pixels. Is it posible to remove it to the barest minimum without distorting so much of the main square/rectangle-blocks. See the attached image of what I got as output.
@Akakan-Abasi Okon I'm not sure what you want so I don't know what to tell you to do. I'd suggest you try various morphological operations like bwareaopen(), imclose(), imopen(), imerode(), imdilate(), and bwmorph(). Also try different structuring element window sizes until you get what you want.
@Image Analyst Once again thank you, this really helped.
I'm trying to change all '-1's to '0' in a large matrix like 100x80. I tried this for smaller dimension and it worked well but it did not work for 100x80. Please how can it be done in larger matrix dimension.
>>h = [1 0 1 0; 1 -1 0 0; -1 -1 0 1; -1 0 -1 1]
h =
1 0 1 0
1 -1 0 0
-1 -1 0 1
-1 0 -1 1
>> h(h==-1) = 0
h =
1 0 1 0
1 0 0 0
0 0 0 1
0 0 0 1
h = randi([-1 1], 100, 80)
h = 100×80
1 0 -1 0 0 -1 0 -1 -1 1 1 -1 0 1 -1 -1 1 1 -1 -1 1 1 1 0 0 1 0 -1 1 0 1 1 0 0 0 1 1 0 1 -1 -1 1 1 0 0 -1 -1 0 1 0 -1 1 1 1 0 0 1 0 -1 1 1 1 -1 0 -1 0 0 1 -1 1 0 -1 -1 0 0 1 0 1 1 -1 -1 0 0 0 0 -1 0 -1 0 1 -1 0 1 0 0 1 -1 -1 1 1 -1 -1 0 -1 1 1 -1 1 -1 1 1 0 1 -1 0 -1 0 1 1 -1 1 -1 1 1 1 0 0 -1 1 -1 -1 -1 -1 -1 0 -1 -1 -1 -1 -1 1 0 0 1 1 -1 -1 1 1 0 1 -1 1 1 -1 0 0 0 -1 -1 0 0 0 -1 -1 -1 0 -1 0 0 0 1 1 -1 -1 0 -1 0 -1 0 0 -1 1 1 1 1 -1 0 -1 1 0 -1 0 0 -1 -1 -1 -1 -1 -1 1 -1 -1 1 0 1 -1 1 0 -1 1 0 -1 -1 -1 1 -1 -1 0 1 0 1 0 1 1 -1 0 1 1 -1 -1 0 1 1 1 -1 -1 -1 -1 0 -1 -1 1 0 -1 -1 1 1 0 -1 1 1 0 1 0 -1 -1 -1 -1 -1 -1 1 1 0 -1 1 0 0 -1 0 -1 1 0 0 -1 1 1 0 0 0 0 -1 -1 1 -1 1 0 1 0 0 0 1 0 0 1 -1 0 0 -1 0
h(h==-1) = 0
h = 100×80
1 0 0 0 0 0 0 0 0 1 1 0 0 1 0 0 1 1 0 0 1 1 1 0 0 1 0 0 1 0 1 1 0 0 0 1 1 0 1 0 0 1 1 0 0 0 0 0 1 0 0 1 1 1 0 0 1 0 0 1 1 1 0 0 0 0 0 1 0 1 0 0 0 0 0 1 0 1 1 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 1 0 0 1 1 0 0 0 0 1 1 0 1 0 1 1 0 1 0 0 0 0 1 1 0 1 0 1 1 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 1 0 0 1 1 0 1 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 1 0 1 0 0 1 0 0 0 0 1 0 0 0 1 0 1 0 1 1 0 0 1 1 0 0 0 1 1 1 0 0 0 0 0 0 0 1 0 0 0 1 1 0 0 1 1 0 1 0 0 0 0 0 0 0 1 1 0 0 1 0 0 0 0 0 1 0 0 0 1 1 0 0 0 0 0 0 1 0 1 0 1 0 0 0 1 0 0 1 0 0 0 0 0
Looks okay to me?
Don't use the while. It's not needed and confusing since h<0 is a matrix -- a lot of values -- not a single value. What Walter gave you is sufficient and correct.
h(h == -1) = 0;
Again, delete the while and end lines.
@Image Analyst I think the problem was with my matlab, I restarted it and this 'h(h == -1) = 0;'is working fine now.

Sign in to comment.

Products

Release

R2021a

Asked:

on 29 Aug 2021

Community Treasure Hunt

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

Start Hunting!