
Assign border pixels according to two regions it separates
3 次查看(过去 30 天)
显示 更早的评论
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!

0 个评论
采纳的回答
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
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.
更多回答(0 个)
另请参阅
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!