Creating a "boundary" between two parts of the matrix

I have a 2D array (n by n) in Matlab. (see an example of the matrix on the image 1). I want to separate specific parts of the matrix (elements '2') with additional layer of zeros (change "boudnary" elelements to zero, see an image 2 for a desired result in the example matrix).
Just for a general perspective, the algorithm is needed to create a boudnary between several 3D features, but I wanted to start with a 2D problem and the data has already been imported as described above.
I was wondering if anyone could have some suggestions on the problem and how to do it in Matlab? Any algorithm or maybe a link to similar problems? Any suggestions are welcomed.
Image 1 Image 1. Example of the problem
Image 2. Desired results

12 Comments

What do you expect for:
A = [1 1 1
1 2 1
1 1 1]
B = [1 1 1 1 1
1 2 2 2 1]
C = [1 1 1 1 1
1 1 1 1 1
1 1 2 1 1
1 1 1 1 1
1 1 1 1 1]
You showed one example, but there are different general rule, which produce the output.
In addition to Jan's question, please describe all possible "signal" and "noise" or "frame" values. Will there ever be a "frame" value within the signal values?
Jan,
Thanks for coming up with these cases. One layer boundary would be enough. For your examples,
A =
0 0 0
0 2 0
0 0 0
B =
0 0 0 0 0
0 2 2 2 0
C =
1 1 1 1 1
1 0 0 0 1
1 0 2 0 1
1 0 0 0 1
1 1 1 1 1
Adam,
All values can be positive integers. Regarding the value that is used as a frame, I picked zero for convenience but it can also be any value (but it should be the same used for all isolated "clusters" of values.) "Frame" value (the one used for framing, zero in my case) is never going to be within "signal" values.
In addition to that, the value within is the worst case scenario. Most of the time values are in groups,not penetrating each other, something like:
A =[1 1 1 1 2 2 2 2 2
1 1 1 1 2 2 2 2 2
1 1 1 1 2 2 2 2 2
3 3 3 3 3 3 3 3 3
3 3 3 3 3 3 3 3 3
3 3 3 3 3 3 3 3 3 ]
and the solution to that would be:
B =[1 1 1 0 0 2 2 2 2
1 1 1 0 0 2 2 2 2
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
3 3 3 3 3 3 3 3 3
3 3 3 3 3 3 3 3 3 ]
Ok, it's getting clearer to me but still a bit fuzzy.
Why are there 2 rows and columns of 0s instead of 1?
Is the goal to isolate the cluster of "2"s? If so, why do the two rows of 0s above extend all the way to the left instead of this, below?
1 1 1 0 0 2 2 2 2
1 1 1 0 0 2 2 2 2
1 1 1 0 0 0 0 0 0
3 3 3 0 0 0 0 0 0
3 3 3 3 3 3 3 3 3
3 3 3 3 3 3 3 3 3
What about this situation?
1 1 1 2
1 1 2 2
1 2 2 2
1 1 1 1
or this
1 1 1 2 2
1 1 1 2 2
1 1 1 1 1
2 2 1 1 1
2 2 1 1 1
I guess you "eat away" one layer from each contiguous region of the same number.
Image Analyst,
Exactly! Thank you for putting this idea into simple words
Adam,
Thanks for bringing up this thing with two layers. The idea is to eat away one layer for each same number region. The number of "boundary" layers (that will be one or more than one in some cases) is not a big concern for now.
Did you ever try my solution below???
So, regarding the outside edge of the image, how do you want to handle that? It seems like the outside perimeter stays the same (not eaten away) unless it borders a different number. Is that right? And why? What is the use case? Give the larger context of this so we can tell if it's the right thing to do or if there is a better way.
Image Analyst,
1) Just finished running the code. It seems to be working well and I understand the algorithm. Thank you!
2) Regarding the outside edge of the image: yes, I think it should stay the same. The outside box should be preserved.
3) Broader perspective: There is a cube with 3D "features" inside. Features are numbered. The whole thing has been imported in Matlab as a 3D matrix. Every element in the matrix is in fact a voxel and the value represents the number of the feature (values 1 - voxels of feature #1, etc.). Goal: separate every feature with a boundary layer.
Also, I just started working in image processing, so I am open to any suggestions.
My solution below preserves outer boundary values, as you requested.
You might want to consider watershed() to separate blobs.
However you have not stated WHY you want to separate the labels with a layer of zeros. The image is labeled so why do you think you need a boundary layer. What do you think you can do with that that you cannot do if you don't have a boundary layer of 0s?
Image Analyst,
Thank you for the advice. Looking into watershed algorithm right now.
Regarding your question about "Why" I want to do that: The whole thing is a representation of a material microstructure. Clusters in the matrix represent grains of a material and boundary layer is another material added inside, on grains of the initial material. The application can be found in modelling of materials microstructure.
Please let me know if you think something else may be useful to check out.

Sign in to comment.

 Accepted Answer

Here's a start:
A =[1 1 1 1 2 2 2 2 2
1 1 1 1 2 2 2 2 2
1 1 1 1 2 2 2 2 2
3 3 3 3 3 3 3 3 3
3 3 3 3 3 3 3 3 3
3 3 3 3 3 3 3 3 3 ]
ua = unique(A)
for k = 1 : length(ua)
thisNumber = ua(k);
% Find out where this number occurs in the image.
map = A == thisNumber
% Find the perimeter of each region.
perimeters = bwperim(map)
% Don't include outside edges of the image.
perimeters(1,:) = false;
perimeters(end,:) = false;
perimeters(:, 1) = false;
perimeters(:, end) = false;
% Set to 0.
A(perimeters) = 0;
end
% Show in command window.
A

4 Comments

Thanks for the help. It seems that it will work even for 3D matrices (if I figure out how to play a bit with outside edges conditions).
Last quick thing: I was thinking about allowing the algorithm to eat out values on the boundary but only if neighboors are different. For example:
Input:
A =[1 1 1 1 2 2 2 2 2
1 1 1 1 2 2 2 2 2
1 1 1 1 2 2 2 2 2
3 3 3 3 3 3 3 3 3
3 3 3 3 3 3 3 3 3
3 3 3 3 3 3 3 3 3 ]
Output:
A=[1 1 1 0 0 2 2 2 2
1 1 1 0 0 2 2 2 2
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
3 3 3 3 3 3 3 3 3
3 3 3 3 3 3 3 3 3 ]
Instead of output that is now:
A =[1 1 1 1 2 2 2 2 2
1 1 1 0 0 2 2 2 2
1 0 0 0 0 0 0 0 2
3 0 0 0 0 0 0 0 3
3 3 3 3 3 3 3 3 3
3 3 3 3 3 3 3 3 3 ]
Do you think it would be smart to use the same algorithm but only for outer edges of the matrix now, or just to play more with "outside edges" condition in the code?
You just need to code up some stuff to handle that special case on the edges. I still don't know why it's necessary. Why do you need this pathway of zeros between the labeled regions? It changes their area you know.
That is the only pathway I came up with. I need to introduce new material in the structure (new label?) and it also should be on the surface of features of the initial material.
Regarding the area change, one or two pixels reduction doesn't do much to the area of a feature with hundreds of pixels in diameter. The effect is insignificant.
What would you consider to be a pathway in such a case?
Not sure I understand your comment. I was calling the "pathway" the map of zero-valued pixels in between non-zero-valued regions. I still don't know why you think you need it or want it.
If you want to "introduce" a new region with a new label number, you can just blast over pixels in that region with the new label number. It would not require that the regions be separated by pathways/rivers of zeros to do that.

Sign in to comment.

More Answers (1)

sir,please check the follow code to get some information
clc; clear all; close all;
A =[1 1 1 1 2 2 2 2 2
1 1 1 1 2 2 2 2 2
1 1 1 1 2 2 2 2 2
3 3 3 3 3 3 3 3 3
3 3 3 3 3 3 3 3 3
3 3 3 3 3 3 3 3 3 ];
ad = unique(A(:));
B = A;
for i = 1 : length(ad)
Ai = A;
Ai(A~=ad(i)) = 0;
Ai(A==ad(i)) = 1;
Ai = logical(Ai);
% just thin
Ai2 = bwmorph(Ai, 'thin', 1);
B(Ai) = Ai2(Ai)*ad(i);
end
figure('Color', 'c');
subplot(1,2,1); imshow(A, []); title('before');
subplot(1,2,2); imshow(B, []); title('after');

1 Comment

Thank you for the idea. At this point I think the outside edge of the box should be preserved, but depending on the situation your way can also be used.
Thank you for a visual representation as well. It's helpful in understanding of what is going on.
Edit: I could consider the book if I knew Chinese :)

Sign in to comment.

Products

Release

R2020a

Community Treasure Hunt

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

Start Hunting!