How to regroup or reconstruct connected elements in a binary image into squares
Afficher commentaires plus anciens
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 commentaires
KALYAN ACHARJYA
le 30 Août 2021
Modifié(e) : KALYAN ACHARJYA
le 30 Août 2021
Are there any specific conditions for the total number of pixels in a square/area of square?
Akakan-Abasi Okon
le 30 Août 2021
Modifié(e) : Akakan-Abasi Okon
le 30 Août 2021
KALYAN ACHARJYA
le 30 Août 2021
After seeing the image, I request you to rethink the question? What is your actual objective?
Akakan-Abasi Okon
le 31 Août 2021
Réponses (1)
Image Analyst
le 30 Août 2021
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 commentaires
Akakan-Abasi Okon
le 30 Août 2021
Modifié(e) : Akakan-Abasi Okon
le 30 Août 2021
Image Analyst
le 30 Août 2021
I have no idea what you want, or why. Mock something up in Photoshop and upload it so we can see.
Akakan-Abasi Okon
le 30 Août 2021
Image Analyst
le 31 Août 2021
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.
Akakan-Abasi Okon
le 31 Août 2021
Image Analyst
le 31 Août 2021
@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.
Akakan-Abasi Okon
le 31 Août 2021
Akakan-Abasi Okon
le 1 Sep 2021
h = randi([-1 1], 100, 80)
h(h==-1) = 0
Looks okay to me?
Akakan-Abasi Okon
le 1 Sep 2021
Modifié(e) : Akakan-Abasi Okon
le 1 Sep 2021
Image Analyst
le 1 Sep 2021
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.
Akakan-Abasi Okon
le 1 Sep 2021
Catégories
En savoir plus sur Morphological Operations dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!