Creating a "boundary" between two parts of the matrix
15 views (last 30 days)
Show older comments
Stanislav Buklovskyi
on 27 Sep 2021
Commented: Image Analyst
on 1 Oct 2021
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 results
12 Comments
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?
Accepted Answer
Image Analyst
on 27 Sep 2021
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
Image Analyst
on 1 Oct 2021
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.
More Answers (1)
yanqi liu
on 28 Sep 2021
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
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!