MATLAB Recognition of geometric figures

Asked

Viewed 148 times

0

The program in Matlab has to have as input an image (black and white) and as output a string with the format name of this image (circle, star, rectangle, square, ellipse). the code I made this recognizing some of these figures, but I would like some suggestions and tips to improve it.

clear all
close all
im=imread('circulo_fr.jpg');
img=rgb2gray(im);
BW=im2bw(img);
stats=regionprops(BW,'Perimeter','Area','Centroid','BoundingBox');
figure,imshow(BW);
hold on
for k=1:length(stats)
    thisboundingbox=stats(k).BoundingBox;
    if stats(k).Area>10000
        retangulo('Position',[thisboundingbox(1), thisboundingbox(2), thisboundingbox(3), thisboundingbox(4)], 'EdgeColor','r','LineWidth',2);
    else
        retangulo('Position',[thisboundingbox(1), thisboundingbox(2), thisboundingbox(3), thisboundingbox(4)], 'EdgeColor','b','LineWidth',2);
    end
    if stats(k).Perimeter^2/stats(k).Area > 18
        text(stats(k).Centroid(1),stats(k).Centroid(2),'Triangulo','Color','r');
    elseif stats(k).Perimeter^2/stats(k).Area < 14.3
        text(stats(k).Centroid(1),stats(k).Centroid(2),'Circulo','Color','g');
    else
        text(stats(k).Centroid(1),stats(k).Centroid(2),'Quadrado','Color','b');
end

1 answer

-1


%This function is able to recognize and name specific figures as %rectangles, stars, ellipses, circles and triangles. % depending on the figures and need to change the value of the circularity

Function classifier = Classifier(image)

%This line of code receives an image

im=imread(imagem);

%Imbinarize creates a binary image, chooses the limit value to minimize %intraclass variation of black and white pixels with limit

img=imbinarize(im,0.3);

%Shows the image.

imshow(img);


[img numberOfObjcts] = bwlabel(img);

%Regionprops maps and returns measures for the specified property set.

blobMeasurements = regionprops(img,'Perimeter','Area', 'Centroid'); 

%Circularity refers to the ratio of the area of overlap of the object to the equivalent circle and %the total area of the object, through a mathematical formula % identifying measures of the object.

circularities = [blobMeasurements.Perimeter].^2 ./ (4 * pi * [blobMeasurements.Area])
hold on;

%This loop of repetition identifies and names the geometric figure accordingly % with its measures and form.

for blobNumber = 1 : numberOfObjcts    
  if circularities(blobNumber) < 2.1016
    message = sprintf('O objeto é uma estrela',...
    blobNumber, circularities(blobNumber));
    theLabel = 'Estrela';
  end
  if circularities(blobNumber) < 1.6243
      message = sprintf('O objeto é um Triangulo',...
      blobNumber, circularities(blobNumber));
    theLabel = 'Triangulo';      
  end
  if circularities(blobNumber) <1.4095
     message = sprintf('O objeto é um Retangulo',...
     blobNumber, circularities(blobNumber));
     theLabel = 'Retangulo';
  end 
  if circularities(blobNumber) <1.2264
    message = sprintf('O objeto é uma Elipse',...
      blobNumber, circularities(blobNumber));
    theLabel = 'Elipse';  
  end 
  if circularities(blobNumber) <0.9875
     message = sprintf('O objeto é um Circulo',...
     blobNumber, circularities(blobNumber));
     theLabel = 'Circulo';
  end
end  

  text(blobMeasurements(blobNumber).Centroid(1), blobMeasurements(blobNumber).Centroid(2),...
    theLabel, 'Color', 'r');
  uiwait(msgbox(message));
end

Browser other questions tagged

You are not signed in. Login or sign up in order to post.