Hi Mathwork community,
Using below code, i can get boundary as blue color and gaps filled as yellow color but i want to fill the interior as green color.
Thanks in advance for all help.
s = load('Result_matrix.mat');
Aorg = s.Aorg;
gapmax = 10;
[m,n] = size(Aorg);
A = double(Aorg>0);
P=[1 2 3;
8 0 4;
7 6 5];
Apad = zeros(m+2,n+2);
maxattempt = 10;
for niter = 1:maxattempt
Apad(2:end-1,2:end-1) = A>0;
B = zeros(m,n,8);
for k=1:8
[i,j] = find(P==k);
B(:,:,k) = Apad(i-2+(2:end-1),j-2+(2:end-1));
end
As = A>0 & sum(diff(B(:,:,[1:8 1]),1,3)==1,3)<=1;
[y,x] = find(As);
if isempty(x)
break
end
p = length(x);
xy = [x,y];
xy1 = reshape(xy,1,[],2);
xy2 = reshape(xy,[],1,2);
d = sum(abs(xy1-xy2),3);
d(d==0) = NaN;
i = (1:size(d,1))';
[dmin,j] = min(d,[],2);
keep = dmin <= gapmax;
i = i(keep);
j = j(keep);
for k=1:length(i)
xyi = xy(i(k),:);
xyj = xy(j(k),:);
dxy = xyi-xyj;
if abs(dxy(1)) > abs(dxy(2))
xr = linspace(xyi(1),xyj(1),abs(dxy(1))+1);
yr = round(interp1([xyi(1),xyj(1)],[xyi(2),xyj(2)],xr));
else
yr = linspace(xyi(2),xyj(2),abs(dxy(2))+1);
xr = round(interp1([xyi(2),xyj(2)],[xyi(1),xyj(1)],yr));
end
A(sub2ind(size(A),yr(2:end-1),xr(2:end-1))) = 3;
end
end

댓글 수: 4

KALYAN ACHARJYA
KALYAN ACHARJYA 2019년 9월 17일
Sir, can you share Result_matrix.mat?
Bruno Luong
Bruno Luong 2019년 9월 17일
편집: Bruno Luong 2019년 9월 17일
For those who want to run the code I attach the data apparently from this thread
The result MS Khan want to plot with different colors is in A after runing the code
imagesc(A)
KALYAN ACHARJYA
KALYAN ACHARJYA 2019년 9월 17일
Thanks @Bruno
@M.S. Khan
Are you looking for following one? Bruno provided the answer, please do confirm.
test11.png
M.S. Khan
M.S. Khan 2019년 9월 17일
Yes Mr. Kalyan. i want background as blue, boundary as yellow, filles holes as red and interior as green.
Thanks for cooperation.

댓글을 달려면 로그인하십시오.

 채택된 답변

Bruno Luong
Bruno Luong 2019년 9월 17일
편집: Bruno Luong 2019년 9월 17일

1 개 추천

Fill the hole with imfill
Next you can color your integer image by defining the appropriate colormap
% Generate test image contains values in 0:4
[~,~,I]=histcounts(peaks,5);
I = I-1;
imagesc(I)
colormap([1 0 0; % color of 0 value
0 1 0; % color of 1 value
0 0 1; % color of 2 value
0 0 0; % color of 3 value
1 1 1])% color of 4 value
set(gca,'Clim',[-0.5 max(I(:))+0.5]);
colorbar
color.png

댓글 수: 16

M.S. Khan
M.S. Khan 2019년 9월 17일
Dear Bruno, i want the Background as blue as it is, boundary as yellow as it is, now i want to color the filled holes as red color while the interior i want to fill as green.Thanks for your all guidance. Holes filled on the boundary we had already labelled as 3s. if the interior is all filled as 2s. can we make all the interior filled inside the yellow boundary as green color.
Screen Shot 2019-09-17 at 8.12.05 PM.png
Bruno Luong
Bruno Luong 2019년 9월 17일
편집: Bruno Luong 2019년 9월 17일
I already gave you a clear guidance about coloring. You must use IMFILL and then apply my code after filling.
I have done with my own function ROSENFELD that does more or less IMFILL
s = load('Result_matrix.mat');
% Code to fill the gap, just recopy here but IRRELEVANT to coloring
Aorg = s.Aorg;
gapmax = 10;
[m,n] = size(Aorg);
A = double(Aorg>0);
P=[1 2 3;
8 0 4;
7 6 5];
Apad = zeros(m+2,n+2);
maxattempt = 10;
for niter = 1:maxattempt
Apad(2:end-1,2:end-1) = A>0;
B = zeros(m,n,8);
for k=1:8
[i,j] = find(P==k);
B(:,:,k) = Apad(i-2+(2:end-1),j-2+(2:end-1));
end
As = A>0 & sum(diff(B(:,:,[1:8 1]),1,3)==1,3)<=1;
[y,x] = find(As);
if isempty(x)
break
end
p = length(x);
xy = [x,y];
xy1 = reshape(xy,1,[],2);
xy2 = reshape(xy,[],1,2);
d = sum(abs(xy1-xy2),3);
d(d==0) = NaN;
i = (1:size(d,1))';
[dmin,j] = min(d,[],2);
keep = dmin <= gapmax;
i = i(keep);
j = j(keep);
for k=1:length(i)
xyi = xy(i(k),:);
xyj = xy(j(k),:);
dxy = xyi-xyj;
if abs(dxy(1)) > abs(dxy(2))
xr = linspace(xyi(1),xyj(1),abs(dxy(1))+1);
yr = round(interp1([xyi(1),xyj(1)],[xyi(2),xyj(2)],xr));
else
yr = linspace(xyi(2),xyj(2),abs(dxy(2))+1);
xr = round(interp1([xyi(2),xyj(2)],[xyi(1),xyj(1)],yr));
end
A(sub2ind(size(A),yr(2:end-1),xr(2:end-1))) = 3;
end
end
I = rosenfeld(~(A>0)); % replace this with IMFILL if you own IMP toolbox, I don't
% Coloring part
imagesc(I)
colormap([0 1 0; % yellow, color of 1 value
0 0 1; % blue, color of 2 value
1 0 0])% red, color of 3 value
set(gca,'Clim',[min(I(:))-0.5 max(I(:))+0.5]);
h = colorbar;
set(h, 'Ytick', 1:3, 'YtickLabel', {'Boundary','Exterior','Interior'});
M.S. Khan
M.S. Khan 2019년 9월 17일
Thanks Bruno, Let’s me work on it. God bless u. Warm regards
Bruno Luong
Bruno Luong 2019년 9월 17일
Apply on your X_slice.xlsx data
apple.png
Look like from a computer 40 year ago.
M.S. Khan
M.S. Khan 2019년 9월 18일
Love you Dear Bruno Luong. Its looking so cool.
Hi Bruno, the same file i using the same method but i could not succeed. can you guide me where i am making mistake. i used fill function but not working. The last part of the coding is making problem. Thanks
gapmax = 10;
[m,n] = size(Aorg);
A = double(Aorg>0);
P=[1 2 3;
8 0 4;
7 6 5];
Apad = zeros(m+2,n+2);
maxattempt = 10;
for niter = 1:maxattempt
Apad(2:end-1,2:end-1) = A>0;
B = zeros(m,n,8);
for k=1:8
[i,j] = find(P==k);
B(:,:,k) = Apad(i-2+(2:end-1),j-2+(2:end-1));
end
As = A>0 & sum(diff(B(:,:,[1:8 1]),1,3)==1,3)<=1;
[y,x] = find(As);
if isempty(x)
break
end
p = length(x);
xy = [x,y];
xy1 = reshape(xy,1,[],2);
xy2 = reshape(xy,[],1,2);
d = sum(abs(xy1-xy2),3);
d(d==0) = NaN;
i = (1:size(d,1))';
[dmin,j] = min(d,[],2);
keep = dmin <= gapmax;
i = i(keep);
j = j(keep);
for k=1:length(i)
xyi = xy(i(k),:);
xyj = xy(j(k),:);
dxy = xyi-xyj;
if abs(dxy(1)) > abs(dxy(2))
xr = linspace(xyi(1),xyj(1),abs(dxy(1))+1);
yr = round(interp1([xyi(1),xyj(1)],[xyi(2),xyj(2)],xr));
else
yr = linspace(xyi(2),xyj(2),abs(dxy(2))+1);
xr = round(interp1([xyi(2),xyj(2)],[xyi(1),xyj(1)],yr));
end
A(sub2ind(size(A),yr(2:end-1),xr(2:end-1))) = 3;
end
end
if isempty(x)
fprintf('Success\n')
else
fprintf('Fail\n')
end
figure(1);
clf;
ax=subplot(1,3,1);
imagesc([-20 280],[-140 140],Aorg>0);
%----
%----
set(ax,'xtick',-20:10:280,'ytick',-140:10:140,'clim',[0 3]); % change here for Q1
axis('on','image')
axis equal
% title('Apple 10,000 point cloud @ width 10');
ax=subplot(1,3,2);
imagesc([-20 280],[-140 140],A); % change here for Q1
set(ax,'xtick',-20:10:280,'ytick',-140:10:140,'clim',[0 3]); % change here for Q1
axis('on','image')
axis equal
title('Apple 10,000 point cloud after filling gaps');
dlmwrite('Boundary_closed_1s_3s.txt',A)
% new attempt to pun 3rd figure
%% interior filling
A = load('Boundary_closed_1s_3s.txt')
Perimeter=bwperim(A)
% dlmwrite('Closed Boundary filled with 1s.txt',Perimeter)
interior_filled = imfill(A,'holes')
A = 3*double(A)
interior_filled = 2*double(interior_filled)
% interior_filled(Perimeter)=1
interior_filled(Perimeter)=A(Perimeter)
interior_filled(interior_filled==3)=1
interior_filled(interior_filled==9)=3
% imshow(interior_filled,[])
dlmwrite('Boundary_closed_1s_3s_interior_2s.txt',interior_filled)
%% to count number of filled voxels on bounary and interior
m = load('Boundary_closed_1s_3s_interior_2s.txt')
%----- filling colors
ax=subplot(1,3,3); % i want to fill
I = imfill(~(m>0),'holes'); % replace this with IMFILL if you own IMP toolbox, I don't
% Coloring part
imagesc(I)
colormap([0 1 0; % yellow, color of 1 value
0 0 1; % blue, color of 2 value
1 0 0])% red, color of 3 value
set(gca,'Clim',[min(I(:))-0.5 max(I(:))+0.5]);
h = colorbar;
set(h, 'Ytick', 1:3, 'YtickLabel', {'Boundary','Exterior','Interior'});
M.S. Khan
M.S. Khan 2019년 9월 18일
How you got the 3rd image. i am tired working on it
Bruno Luong
Bruno Luong 2019년 9월 18일
편집: Bruno Luong 2019년 9월 18일
Please replace the last Coloring part by
%----- filling colors
ax=subplot(1,3,3); % i want to fill
% Coloring part
imagesc(interior_filled); % <--- *YOUR* IMAGE RESULT HERE !!!!!!
colormap([0 0 1; % blue, color of 0 value % <--- YOUR DESIRED COLORED HERE
0 1 1; % yellow, color of 1 value % <--- AND HERE
1 0 0; % red, color of 2 value % <--- AND HERE
0 1 0]) % green, color of 3 value % <--- AND HERE !!!!!!
set(gca,'Clim',[min(I(:))-0.5 max(I(:))+0.5]);
h = colorbar;
set(h, 'Ytick', 1:3, 'YtickLabel', {'Exterior','Boundary','Interior','Gap'}); %EDIT
This piece of code is a minor modification of my answer above using PEAKS image example.
What is often missing with you is trying to understand the code we gave you and adapt to YOUR specific need
Thanks Bruno, really you are very kind and helpful.
set(h, 'Ytick', 1:4, 'YtickLabel', {'Exterior','Boundary','Exterior','Gap'});
Its only given exterior, boundary and exterior but not showing gaps on color bar
i am trying to resolve this problem.
Thanks and regards
Bruno Luong
Bruno Luong 2019년 9월 18일
편집: Bruno Luong 2019년 9월 18일
Please upload yor image interior_filled in MATFILE (obtained after the calculation step, which is another subjects.)
I suggest you to concentrate step by step, analyze what is the input what is the output, post the question you might have each step as a series of indepent questions. You'll get more assistant, since not many people here would look at the code if the code has more than say 10 lines and the question is not consistent with filled with details that is not relevant.
PS: I fix the typo {'Exterior','Boundary','Interior','Gap'}
Hi Mathwork community brothers,
i am running this coding to but i color bar is ignoring to show 'gaps'.
i want to match the color and labels
Thanks in advance for cooperation
ax=subplot(1,3,3);
% Coloring part
imagesc(m); % <--- *YOUR* IMAGE RESULT HERE !!!!!!
colormap([0 1 1; % cyan, color of 0 value %
0 1 0; % blue, color of 1 value %
0 0 1; % green, color of 2 value
1 0 0]) % red, color of 3 value %
set(gca,'clim',[min(m(:))-0.5 max(m(:))+0.5]);
h = colorbar;
set(h, 'Ytick', 1:4, 'YtickLabel', {'Exterior','Boundary','Interior','Gaps'});
Bruno Luong
Bruno Luong 2019년 9월 18일
편집: Bruno Luong 2019년 9월 18일
untitled.png
clear
load('m.mat'); % load your data
close all
ax = axes();
% Coloring part
imagesc(interior_filled); % <--- *YOUR* IMAGE RESULT HERE !!!!!!
colormap([0 0 1; % blue, color of 0 value % <--- YOUR DESIRED COLORED HERE
1 1 0; % yellow, color of 1 value % <--- AND HERE
1 0 0; % red, color of 2 value % <--- AND HERE
0 1 0]) % green, color of 3 value % <--- AND HERE !!!!!!
set(ax,'Clim',[min(interior_filled(:))-0.5 max(interior_filled(:))+0.5]);
h = colorbar(ax);
set(h, 'Ytick', 0:3, 'YtickLabel', {'Exterior','Boundary','Interior','Gap'}); %EDIT
axis(ax,'equal');
axis(ax,'tight');
M.S. Khan
M.S. Khan 2019년 9월 18일
Thanks Bruno Luong. You are genius. God protect you from devil eyes!!!!
M.S. Khan
M.S. Khan 2019년 9월 18일
Dear Burno, is there any fastest method to save the image like above.?
plz guide me, how can we save that image
Bruno Luong
Bruno Luong 2019년 9월 18일
I fail to see the relationship between saving and coloring. What is your motivation of you asking unrelated question to this thread?
"Please post the question you might have each step as a series of indepent questions."
M.S. Khan
M.S. Khan 2021년 2월 28일
Hi Dear Bruno Luong,
i am using your above code to fill the gaps on boundary surface of cone but could not succeed to fill the gaps.
i have tried to change the ie. P=[1 2 3; 8 0 4;7 6 5] but its giving me errors.
Please guide me.
The file of cone with gaps on the boundary is attached.
Thanks for all your kind cooperations and guidance.
Regards

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Color and Styling에 대해 자세히 알아보기

질문:

2019년 9월 17일

댓글:

2021년 2월 28일

Community Treasure Hunt

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

Start Hunting!

Translated by