Detecting n number of boundary pixels in an image

1 visualizzazione (ultimi 30 giorni)
Hello, I am trying to determine n boundary pixels from any image. The image can be of an arbitrary shape. I have made a very C type code which is using two for loops with the combination of find to find out the indices. The code is working and I attach the code here. I wanted to know if you guys know of a smarter way to do this. One way I can think of is to convert my image in to a binary image and use bwboundaries n times (the next time after eliminating the first boundary pixels) to determine the pixels but this will require to use the bwboundaries n times for n pixels which I think will take more time as compared to the present code. The image to be checked for is loaded as im2_tobe_rep. A boundary is defined as the start of a nonzero value. Checking is done from all four directions.
#Code
[row col bands]=size(im2_tobe_rep);
im2gray=rgb2gray(im2_tobe_rep);
im2grayfl=fliplr(im2gray);
r=0;
c=0;
%finds the col value of non zero value for each row
for i=1:row
indrn=find(im2gray(i,:),2);
indrl=(find(im2grayfl(i,:),2));
if(isempty(indrn)~=1)
[rowr colr]=ind2sub([1 col],indrn);
rowr=i*ones(1,length(rowr));
r=[r rowr];
c=[c colr];
end
if(isempty(indrl)~=1)
[rowl coll]=ind2sub([1 col],indrl);
coll=col-coll+1;
rowl=i*ones(1,length(rowl));
r=[r rowl];
c=[c coll];
end
end
%finds the row value of non zero value for each col
im2grayip=flipud(im2gray); %Convert to gray
for i=1:col
indcn=find(im2gray(:,i),2);
indcl=(find(im2grayip(:,i),2));
if(isempty(indcn)~=2)
[rowcr colcr]=ind2sub([row 1],indcn);
colcr=i*ones(1,length(colcr));
r=[r rowcr.'];
c=[c colcr];
end
if(isempty(indcl)~=1)
[rowcl colcl]=ind2sub([row 1],indcl);
rowcl=row-rowcl+1;
colcl=i*ones(1,length(colcl));
r=[r rowcl.'];
c=[c colcl];
end
end
r(1)=[];
c(1)=[];
testim=zeros(row,col);
for i=1:length(r)
testim(r(i),c(i))=255;
end

Risposte (0)

Categorie

Scopri di più su Image Processing Toolbox in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by