Showing posts with label Calculate Centroid in matlab. Show all posts
Showing posts with label Calculate Centroid in matlab. Show all posts

Monday, March 28, 2016

Bit Plane Slicing in MATLAB

       Read fractal.jpg file. Display individual bits as binary image. Display and comment on the result.  (Hint: use bitget built-in function)

CODE:

clc; clear;

A=imread('fractal.jpg');
[row,col]=size(A);
subplot(3,3,1), imshow(A), title('Original Image');
C=zeros(row,col,8);
for k=1:8
    for i=1:row
        for j=1:col
            C(i,j,k)=bitget(A(i,j),k);        %Bit slicing
        end
    end
    subplot(3,3,k+1), imshow(C(:,:,k)), title(['Bit Plane ',num2str(k-1)]);
end


OUTPUT RESULT :

We observe that the three higher order planes, especially the 8th bit plane, contain a significant amount of the visually significant data. The lower order planes contribute to more subtle intensity details in the image. Decomposing an image into its bit planes is useful for analyzing the relative importance of each bit in the image and is useful for image compression.

Saturday, March 19, 2016

How to apply Bounding BOX for selected objects

Connectivity helps in applying bounding box for selected objects.
Calculate  :
i)Area 
ii) Mean 
iii)Centroid 
iv) Perimeter

clear
close all;
I=imread('coins.png');
figure,imshow(I);
B=im2bw(I);
figure,imshow(B);
c=imfill(B,'holes');                            % Fill the holes
figure,imshow(c);
[label,num]=bwlabel(c);
display(num)
object=3;                                       % To apply bounding box for labelled 3
[row,col]=find(label==object); % to find coordinates of the bounding box
sx=min(col)-0.5;
sy=min(row)-0.5;
breadth=max(col)-min(col)+1;
len=max(row)-min(row)+1;
bbox=[sx sy breadth len];
display(bbox);
figure,imshow(I);
hold on;
x=zeros([1 5]);
y=zeros([1 5]);
x(:)=bbox(1);
y(:)=bbox(2);
x(2:3)=bbox(1)+bbox(3);
y(3:4)=bbox(2)+bbox(4);
plot(x,y);
obj_area=numel(row);
display(obj_area);
% Find Centroid
X=mean(col);
Y=mean(row);
Centroid=[X Y];
display(Centroid);
plot(X,Y,'o','color','r');
hold off
%Find perimeter
BW=bwboundaries(label==object);
figure, imshow(I);
hold on
visboundaries(BW);
c=cell2mat(BW(1));
perimeter=0;
for i=1:size(c,1)-1
perimeter=perimeter+sqrt((c(i,1)-c(i+1,1)^2+c(i+1,2)).^2);
end
display(perimeter);

% calculation with regionprops for verification purpose
sdata=regionprops(label,'all'); % click on sdata and check for different parameters

RESULT:

Fig 1

Fig 2