Sie sind auf Seite 1von 15

Find Area, Perimeter, Centroid, Equivdiameter, Roundness and Bounding Box without Using MATLAB Function regionprops

In MATLAB, the function regionprops is used to measure the image properties. Here are some basic properties computed without using the function. Read an image and find the connected components using bwlabel function. Using the Labeled matrix as an input, the properties can be measured. Example: A=[1 0 0 1 1111 0 0 1 1] To find Area: The total number of ON pixels in the image. The number of ones in the matrix is 8. To find Centroid: Find the row and column having pixel value one. Eg.[row,column]=find(label==1) Row=[ 1 2 2 2 3 1 2 3] Column=[ 1 1 2 3 3 4 4 4] Find the mean of the row and column having pixel value one. Mean of Row=2 and mean of column= 2.75

To find the Bounding Box: We need 4 points, starting position(x,y) , length and breadth. Minimum value of row and column minus 0.5 gives starting position(x,y) respectively Minimum value of row=1-0.5=0.5 Minimum value of column=1-0.5=0.5 Maximum value of column minimum value of column+1 gives breadth of the box Maximum value of column=4 Max value-min value of column=3+1 Maximum value of row- minimum value of row +1gives length of the box

maximum value of row=3 Max value Min value=2+1 Bounding Box value for the given example:0.5000 0.5000 4.0000 3.0000 For more details on how to draw a rectangle check here: http://angeljohnsy.blogspot.in/2011/06/how-to-draw-in-matlab.html

To find the Perimeter Find the boundary of the labeled component Boundary pixels: 1 1 2 2 2 3 1 4 2 4 3 4 3 3 2 2 2 1 1 1 Find the distance between the each adjoining pair of pixels around the border of the region. Use the distance formula:

For instance, calculate the distance between the two points (1,1) and (2,2). distance=sqrt((2-1).^2+(2-1).^2)=1.41 Similarly, the distance is computed for all the pixel positions. The perimeter for the given example is 10.2426 To find the Roundness: Roundness of an object can be determined using the formula: Roundness=(4*Area*pi)/(Perimeter.^2) If the Roundness is greater than 0.90 then, the object is circular in shape. Result= (4*8*3.14)/10.2426.^2=0.9582

To find the Equivdiameter Formula: sqrt(4*Area/pi). Equivdiameter for the given example:3.1915

MATLAB CODE:
%Measure Basic Image Properties without using 'regionprops' function %Measure Area, Perimeter, Centroid , Equvidiameter, Roundness and Bounding Box clc %Read Original Image I=imread('coins.png'); %Convert to Binary B=im2bw(I); %Fill the holes C=imfill(B,'holes'); %Label the image [Label,Total]=bwlabel(C,8); %Object Number num=4; [row, col] = find(Label==num);

%To find 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); %Refer:http://angeljohnsy.blogspot.in/2011/06/how-to-draw-in-matlab.html 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);

%Find Area Obj_area=numel(row); display(Obj_area); %Find Centroid X=mean(col); Y=mean(row); Centroid=[X Y]; display(Centroid); plot(X,Y,'ro','color','r'); hold off;

%Find Perimeter BW=bwboundaries(Label==num); 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,2)-c(i+1,2)).^2); end display(Perimeter);

%Find Equivdiameter EquivD=sqrt(4*(Obj_area)/pi); display(EquivD);

%Find Roundness Roundness=(4*Obj_area*pi)/Perimeter.^2; display(Roundness);

%Calculation with 'regionprops'(For verification Purpose); %Sdata=regionprops(Label,'all'); %Sdata(num).BoundingBox %Sdata(num).Area %Sdata(num).Centroid %Sdata(num).Perimeter %Sdata(num).EquivDiameter

Simple GUI Calculator in MATLAB


Simple GUI calculator

I first created a simple GUI for the calculator to perform +-*/. When the user click the button the function arithmetic is called.

Additional Information: To perform all the complex calculations use Texas Instruments .

function calculator

figure('Position',[200 200 220 200],'Name','Calculator','NumberTitle','off','MenuBar','None','Resize','of f'); txt=uicontrol('Style','Text','Position',[10 165 200 30],'String','0'); global stack value top op tops opstack num1 num2 flag; flag=0; x=10; y=130; top=1; tops=1; name=['7','8','9','/','4','5','6','*','1','2','3','-','0','+','C','='];%k

for k=1:size(name,2)

uicontrol('Style','Pushbutton','Position',[x y 50 30],'String',name(k),'Callback',@arithmetic) ; x=x+50; if(mod(k,4)==0)

x=10;y=y-35;

end end

The String value of the pressed button is obtained using theget(object,String) function.

If the value is a number then it is placed in a stack. If the value is a operator then the operator will be placed in the operator stack (opstack). If the value is C then the stacks top is set to 1.

function arithmetic(object,~) num=str2double(get(object,'String')); if((num>=0)&&(num<=9)) value=num; evaluate(); else op=get(object,'String'); if(op=='C') top=1; tops=1; set(txt,'String','0'); else operator(); end end end

The function evaluate is called to add the number to the stack. If the stack (1) =0 and the stack (2) =9, then the stack (1) =9. This is because, when the user enters zero and then 1, the stack value will be 01. To obtain the correct value, it is better to remove the zero and display as 1.

If the user enters numbers continuously without entering an operator then the values should be concatenated at each time. For example, if the user enter 2, 4, 5 and then +, the values 245 should be displayed as a single number.

function evaluate() stack(top)=value; if((stack(1)==0)&&(top==2)) stack(1)=stack(top); top=top-1; end str=num2str(stack(1)); for i=2:top str=strcat(str,num2str(stack(i))); end top=top+1; set(txt,'String',' '); set(txt,'String',str); flag=0; end

If the user enter an operator then the operator will be added to the operator stack and the first number to perform the arithmetic operation is stored in the variable num1. If the user enters an operator again then the operator will be added to the stack and the second number will be stored in the variable num2. After getting the two numbers, the corresponding arithmetic operation is done based on the operator in the opstack(1). The result will be stored in the variable num1.

function operator()

if((top~=1)||(flag==1))

opstack(tops)=op; tops=tops+1;

if((tops==2)&&(flag~=1))

str=num2str(stack(1)); for i=2:top-1 str=strcat(str,num2str(stack(i))); end num1=str2double(str);

flag=1; top=1; elseif(tops>=3)

if(flag==0) str=num2str(stack(1)); for i=2:top-1 str=strcat(str,num2str(stack(i))); end num2=str2double(str);

top=1;

if(opstack(tops-1)=='=')

calculate(); set(txt,'String',' ');

set(txt,'String',num2str(num1));

flag=1; tops=1;

else

calculate(); set(txt,'String',' ');

set(txt,'String',num2str(num1));

flag=1; tmp=opstack(tops-1); opstack(1)=tmp; tops=tops-1; end else tmp=opstack(tops-1); opstack(1)=tmp; tops=tops-1;

end end

end end

function calculate() switch (opstack(1)) case '+'

num1=num1+num2; case '-' num1=num1-num2; case '/' num1=num1/num2; case '*' num1=num1*num2;

end end

end

Did u find the material useful? The Essential Guide to User Interface Design: An Introduction to GUI Design Principles and Techniques (CourseSmart) User Interface Design for Programmers

How to draw in MATLAB


I used the function getline to get the points from the user. Using plot function the lines are drawn. MATLAB CODE: scz=get(0,'ScreenSize'); fig=figure('Position',[round(scz(1,3)/4) round(scz(1,4)/8) 700 500],'MenuBar','None','NumberTitle','off','Name','Draw Lines','Resize','off');

[x, y] = getline(fig); plot(x,y); axis([0 1 0 1]);

Draw lines To draw a rectangle: x=zeros([1 5]); y=zeros([1 5]); r=getrect(fig); x(:)=r(1); y(:)=r(2); x(2:3)=r(1)+r(3); y(3:4)=r(2)+r(4);

plot(x,y); axis([0 1 0 1]);

Draw Rectangle The function getrect returns the [xmin ymin width height]. We need 4 points to draw a rectangle. The first point is (x,y) , here it is (xmin,ymin). The second point is(x+width,y) Third point is (x+width, y+height) Fourth point is (x,y+height) Since I am using the plot function I need to connect the 1st and the fourth point. So I need a fifth point i.e (x,y) which completes the rectangle.