Main Content

La traducción de esta página aún no se ha actualizado a la versión más reciente. Haga clic aquí para ver la última versión en inglés.

Identificar objetos redondos

Este ejemplo muestra cómo clasificar objetos en función de su redondez empleando bwboundaries, una rutina de rastreo de límites.

Paso 1: Leer una imagen

Lea pills_etc.png.

RGB = imread("pillsetc.png");
imshow(RGB)

Figure contains an axes object. The axes object contains an object of type image.

Paso 2: Determinar el umbral de la imagen

Convierta la imagen a blanco y negro para preparar al rastreo de límites empleando bwboundaries.

I = im2gray(RGB);
bw = imbinarize(I);
imshow(bw)

Figure contains an axes object. The axes object contains an object of type image.

Paso 3: Preprocesar la imagen

Usando funciones morfológicas, elimine los píxeles que no pertenezcan a los objetos de interés.

Elimine todos los objetos que contengan menos de 30 píxeles.

minSize = 30;
bw = bwareaopen(bw,minSize);
imshow(bw)

Figure contains an axes object. The axes object contains an object of type image.

Rellene el hueco de la tapa del bolígrafo.

se = strel("disk",2);
bw = imclose(bw,se);
imshow(bw)

Figure contains an axes object. The axes object contains an object of type image.

Rellene los huecos, de forma que regionprops se pueda utilizar para calcular el área delimitada por cada uno de los límites

bw = imfill(bw,"holes");
imshow(bw)

Figure contains an axes object. The axes object contains an object of type image.

Paso 4: Encontrar los límites

Concéntrese únicamente en los límites exteriores. Especificar la opción "noholes" acelerará el procesamiento impidiendo que bwboundaries busque contornos internos.

[B,L] = bwboundaries(bw,"noholes");

Muestre la matriz de etiquetas y dibuje cada límite.

imshow(label2rgb(L,@jet,[.5 .5 .5]))
hold on
for k = 1:length(B)
  boundary = B{k};
  plot(boundary(:,2),boundary(:,1),"w",LineWidth=2)
end
title("Objects with Boundaries in White")

Figure contains an axes object. The axes object with title Objects with Boundaries in White contains 7 objects of type image, line.

Paso 5: Determinar qué objetos son redondos

Estime la circularidad y el centroide de todos los objetos utilizando la función regionprops. La métrica de circularidad es igual a 1 para un círculo ideal y menor que 1 para cualquier otra forma.

stats = regionprops(L,"Circularity","Centroid");

El proceso de clasificación puede controlarse estableciendo un umbral adecuado. En este ejemplo, utilice un umbral de 0.94 para que solo las píldoras se clasifiquen como redondas.

threshold = 0.94;

Realice un bucle sobre los límites detectados. Para cada objeto:

  • Obtenga las coordenadas (x, y) del límite y la medida de circularidad.

  • Compare la medida de circularidad con el umbral. Si la circularidad supera el umbral, calcule la posición del centroide y muéstrelo como un círculo negro.

  • Muestre la medida de circularidad en texto amarillo sobre el objeto.

for k = 1:length(B)

  % Obtain (X,Y) boundary coordinates corresponding to label "k"
  boundary = B{k};
  
  % Obtain the circularity corresponding to label "k"
  circ_value = stats(k).Circularity;
  
  % Display the results
  circ_string = sprintf("%2.2f",circ_value);

  % Mark objects above the threshold with a black circle
  if circ_value > threshold
    centroid = stats(k).Centroid;
    plot(centroid(1),centroid(2),"ko");
  end
  
  text(boundary(1,2)-35,boundary(1,1)+13,circ_string,Color="y",...
       FontSize=14,FontWeight="bold")
  
end
title("Centroids of Circular Objects and Circularity Values")

Figure contains an axes object. The axes object with title Centroids of Circular Objects and Circularity Values contains 17 objects of type image, line, text. One or more of the lines displays its values using only markers

Consulte también

| | | | | | |

Temas relacionados