Using bwlabeln in a matrix with first row and column connected to last row and column

Hello all!
This is my first post, and I am pretty desperate. I have a matrix A(nx,ny,nz) where I have applied the function bwlabeln (A=bwlabeln(A)). Now i need to connect those structures because my x and y axis are periodic, that is, the first row in is connected to the last one, and same with the columns: the first column is connected to the last column.
NOTE: there is no connection between the last matrix in z and the first one.
Anybody knows how could I do this?
I thought about creating a matrix B(3nx, 3nz, ny) where I copy the matrix A 9 times, so the central matrix sees that it is periodic, but then I dont know how could I extract the results
Thank you all very much!!

Answers (2)

It can be done but it's a little complicated. Visualize looking down the z axis at your volume/block. What you need to do is find the blobs on the bottom and right face, then stitch them to the left and top face. Then label, then chop off and replace original blobs with the copped off ones. So the steps are (untested)
  1. Set the x=1 plane to 0
  2. Set the x=nx plane to 0
  3. Set the y=1 plane to 0
  4. Set the y=ny plane to 0
  5. Set the z=1 plane to 0
  6. Set the z=nz plane to 0
  7. call imclearborder to remove the blobs on the left and bottom.
  8. subtract from original to get only those edge blobs.
  9. stitch that block to the left and top of the original block
  10. in the upper left block, stitch a block of all zeros so you now have block twice as wide and twice as tall as the original with the original block in the lower left quadrant
  11. call bwlabeln
  12. Now blobs from the other side will be connected and will have the same label.
  13. After labeling extract the 4 quadrant blobs.
  14. Replace the pixels in the original block with non-zero pixels from the left quadrant and top quadrant.
I'm pretty sure that will work, though I don't want to code it up for you. (Too much work to do it for free.)

6 Comments

Eloi's "Answer" moved here since it's a reply to me.
Thank you for your response, but actually my matrix is a numerical matrix, so I didn't understand what you mean by setting the planes to 0. My matrix A is a matrix composed by 0 and 1's, and I want to group together the 1's but with the condition that the first and last row and column of each z are linked together.
Thank you :)
I know your data is binary - you wouldn't have used bwlabeln otherwise. A rectangular block of volumetric data has 6 faces (planes), correct? Just set one layer/face/plane to be zero so that it's not removed when you clear objects touching the edge of the block. For example
binaryImage3D(1,:,:) = false;
Ok I see what you mean. First, let me thank you again for your time, I really appreciate that.
But I am lost here:
7. call imclearborder to remove the blobs on the left and bottom.
imclearborder will remove all blobs (which will have already been removed by setting the planes to 0) in all borders, right?
I also don't understand step 9. You mean I need to get the borders, then switch sides and stich them to the original block?
Thank you again!
Correct. But you don't want it to remove those on the top and left so that's why you erase those faces. Later when you attach the blocks to the left and top, you attach them to the original binary image.
In step 9, you have a 2 by 2 array of blocks twice as wide and tall as your original. You have
(1) (2)
(3) (4)
(4) is your original image. (3) is a block with only the border blobs that were on the right side of (4). (2) is a block with only the border blobs that were on the top side of (4). And (3) is just all zeros and is needed because you can't have an "L"-shaped image.
Thank you again and sorry for my stupidness, but I don't get to see how this can make the faces connect. When you set the 4 different blocks yeah, the faces are connected, but on the right and bottom side of (4) there will be different labels anyway, so they won't be connected to its top and left sides. Right? Maybe it is a matter of me not understanding step 14 :(
You can stitch together like this:
bwz = false(size(bw));
bw2x2 = [bwz, bw2; bw3, bwOriginal];
With what I described, only the top and bottom blobs will be connected and be the same. Same for the right and left. If you want all 4 faces to be connected, then that is even more complicated than this, and you can't use this approach. And if you thought this was complicated,.....
To get all 4 connected, you may have to employ some really complicated region growing routine. I'm not about to try to outline that algorithm for you. Why do you think you need this anyway?

Sign in to comment.

Well, actually I need the four faces to be connected. I will program it with if's and then using find i will rename adjacent structures...
This is a code to obtain coherent structures within a the development of turbulent flow between two plaques. The domain has been chosen so it is statistically independent, and can be considered periodic in the axis x and z, where there is no wall.
Thank you very much for your responses.

Products

Asked:

on 20 Jun 2014

Answered:

on 22 Jun 2014

Community Treasure Hunt

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

Start Hunting!