Creating a "boundary" between two parts of the matrix
Show older comments
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. Example of the problem
Image 2. Desired results12 Comments
Jan
on 27 Sep 2021
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.
Adam Danz
on 27 Sep 2021
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?
Stanislav Buklovskyi
on 27 Sep 2021
Stanislav Buklovskyi
on 27 Sep 2021
Adam Danz
on 27 Sep 2021
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
Image Analyst
on 27 Sep 2021
I guess you "eat away" one layer from each contiguous region of the same number.
Stanislav Buklovskyi
on 28 Sep 2021
Stanislav Buklovskyi
on 28 Sep 2021
Image Analyst
on 28 Sep 2021
Edited: Image Analyst
on 28 Sep 2021
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.
Stanislav Buklovskyi
on 28 Sep 2021
Image Analyst
on 28 Sep 2021
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?
Stanislav Buklovskyi
on 29 Sep 2021
Accepted Answer
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
Stanislav Buklovskyi
on 28 Sep 2021
Edited: Stanislav Buklovskyi
on 28 Sep 2021
Categories
Find more on Get Started with Computer Vision Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!