Assign border pixels according to two regions it separates

3 ビュー (過去 30 日間)
ailbeildce
ailbeildce 2018 年 4 月 9 日
コメント済み: ailbeildce 2018 年 4 月 10 日
Hi,
The title may be confusing. So I'll explain with the image below. I want to be able to color the borders (shown in green) depending on the regions in two sides of it. For example, if the border is between two regions shown in red and blue, I want to make the border black. If the demarcating line is between red and white, I want to make the border purple etc.
I am aware that there are some boundary cases where 3 or more regions connect, but I don't care about those. Is there a built-in or an easy way to do this on Matlab? I can think of straightforward solutions like examining the circular/rectangular neighborhood of each border (green) pixel and assigning a color based on that. This seems a bit slow, so I ask for a better way.
Thanks!

採用された回答

Matt J
Matt J 2018 年 4 月 9 日
編集済み: Matt J 2018 年 4 月 9 日
I can think of straightforward solutions like examining the circular/rectangular neighborhood of each border (green) pixel and assigning a color based on that. This seems a bit slow, so I ask for a better way.
It's not the wrong approach. You just have to recognize how dilations and erosions can be used to implement it efficiently. The code below generates a label map for each of the border types. Once you have that, you can use it to assign colors to the original image.
A=double(rgb2gray(imread('Colors.png')));
border=A<255&A>81;
tic;
A(border)=-inf;
Maximums=imdilate(A, strel('disk',20)).*border;
A(border)=+inf;
Minimums=imerode(A, strel('disk',20)).*border;
toc
map=zeros(size(A));
map(Maximums==255 & Minimums==81)=1; %labels
map(Maximums==255 & Minimums==29)=2;
map(Maximums==81 & Minimums==29)=3;
map(Maximums==Minimums &Minimums==255)=4;
map(Maximums==Minimums &Minimums==81)=5;
map(Maximums==Minimums &Minimums==29)=6;
  3 件のコメント
Matt J
Matt J 2018 年 4 月 10 日
編集済み: Matt J 2018 年 4 月 10 日
When you ran the code, did you not get the same image as I posted above??? As you can see there, all boundaries were found.
In any case, the approach is pretty much what you mentioned in your post. After converting to grayscale, we look for the maximum and minimum values in a 20 pixel disk-shaped neighborhood around each boundary pixel. This segment of the code generates images giving the per pixel max and min:
A(border)=-inf;
Maximums=imdilate(A, strel('disk',20)).*border;
A(border)=+inf;
Minimums=imerode(A, strel('disk',20)).*border;
The rest of it is just testing each pixel for different combinations of maximum and minimum values. Each combination of max/min corresponds to a color combination.
ailbeildce
ailbeildce 2018 年 4 月 10 日
Ah, sorry this screenshot does not have red = 255 0 0, some compression quirk I assume. Your code is perfect and I should do more due diligence. Thank you!

サインインしてコメントする。

その他の回答 (0 件)

製品

Community Treasure Hunt

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

Start Hunting!

Translated by