Using bwlabeln in a matrix with first row and column connected to last row and column
Show older comments
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)
Image Analyst
on 20 Jun 2014
0 votes
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)
- Set the x=1 plane to 0
- Set the x=nx plane to 0
- Set the y=1 plane to 0
- Set the y=ny plane to 0
- Set the z=1 plane to 0
- Set the z=nz plane to 0
- call imclearborder to remove the blobs on the left and bottom.
- subtract from original to get only those edge blobs.
- stitch that block to the left and top of the original block
- 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
- call bwlabeln
- Now blobs from the other side will be connected and will have the same label.
- After labeling extract the 4 quadrant blobs.
- 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
Image Analyst
on 20 Jun 2014
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 :)
Image Analyst
on 20 Jun 2014
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;
Eloi
on 20 Jun 2014
Image Analyst
on 20 Jun 2014
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.
Eloi
on 20 Jun 2014
Image Analyst
on 21 Jun 2014
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?
Eloi
on 22 Jun 2014
0 votes
Categories
Find more on Axis Labels in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!