I have written a code for finding centroid of connected components of an image and it gives an error.I tried to debug it but couldn't get a clue.How to solve that?

1 view (last 30 days)
So, the given is my code below.I need to find out the centroid of the connected components and it doesn't work.Also,I am supposed to save these centroids and I couldn't save it.The error that the gives is also given below- %%Computing Background Markers
bw = im2bw(Iobrcbr);
%compute connected components
CC=bwconncomp(bw);
lab=labelmatrix(CC);
%% FINDING CENTROID OF CONNECTED COMPONENTS(need to work on lab)
s=regionprops(lab,'centroid');
centroids=cat(1, s.Centroid);
figure
imagesc(lab)
hold on
plot(centroids(:,1),centroids(:,2), 'b*');
hold off
ERROR-Index exceeds matrix dimensions.
Error in actual (line 92) plot(centroids(:,1),centroids(:,2), 'b*');
  4 Comments
Prachi Sharma
Prachi Sharma on 25 Oct 2016
Edited: Prachi Sharma on 25 Oct 2016
It worked fine for one video,for other videos it is just not working fine.Here is my complete code- %% VIDEO FRAMES
close all clear all
vidobj=VideoReader('clouds.mp4');
%frames=vidobj.Numberofframes;
Frame_No=vidobj.NumberOfFrames;
%for i=1:Frame_No
for i=1:5
imwrite(read(vidobj,i),strcat('C:\Users\Narendra Singh\Documents\MATLAB\Cloudframes\',num2str(i),'.png'))
end
%% WATERSHED SEGMENTATION
%%CONVERTING COLOR IMAGE TO GRAY IMAGE
for i=1:5
rgb=imread(strcat('C:\Users\Narendra Singh\Documents\MATLAB\Cloudframes\',num2str(i),'.png'));
a=imresize(rgb,[512 512]);
I=rgb2gray(a);
%%FINDING THE GRADIENT OF THE IMAGE
hy=fspecial('sobel');
hx=hy';
Iy=imfilter(double(I), hy, 'replicate');
Ix=imfilter(double(I), hx, 'replicate');
gradmag=sqrt(Ix.^2 + Iy.^2);
%% %%Marking the Foreground Objects
se=strel('disk',10);
Io=imopen(I, se);
%%
%%Computing the opening-by-reconstruction using imerode and imreconstruct.
Ie = imerode(I, se);
Iobr = imreconstruct(Ie, I);
%%
%%Following the opening with a closing to remove the dark spots and stem mark
Ioc = imclose(Io, se);
%%
%%Using imdilate followed by imreconstruct
Iobrd = imdilate(Iobr, se);
Iobrcbr = imreconstruct(imcomplement(Iobrd), imcomplement(Iobr));
Iobrcbr = imcomplement(Iobrcbr);
%% Calculate the regional maxima of Iobrcbr to obtain good foreground markers
fgm = imregionalmax(Iobrcbr);
%%
%%Superimpose the foreground marker image on the original image.
I2 = I;
I2(fgm) = 200;
%%
%%cleaning the edges of the marker blobs and then shrinking them a bit.
se2 = strel(ones(2,2));
fgm2 = imclose(fgm, se2);
fgm3 = imerode(fgm2, se2);
fgm4 = bwareaopen(fgm3,200);
I3 = I;
I3(fgm4) = 255;
%%
%%Computing Background Markers
bw = im2bw(Iobrcbr);
%% CONNECTED COMPONENTS %compute connected components CC=bwconncomp(bw);
lab=labelmatrix(CC);
%% FINDING CENTROID OF CONNECTED COMPONENTS(need to work on lab)
s=regionprops(lab,'centroid');
centroids=cat(1, s.Centroid);
figure
imagesc(lab)
hold on
plot(centroids(:,1),centroids(:,2), 'b*');
hold off
end

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!