How to measure the aspect ratio of a certain part of an image

5 visualizaciones (últimos 30 días)
I am working with the mnist dataset and I am trying to calculate the aspect ratio of every number. The aspect ratio is defined as the minimum ractangular area that the number covers any idea how to do that?
  2 comentarios
Rik
Rik el 28 de Mzo. de 2020
What have you tried so far? You must have had an idea.
Adam Danz
Adam Danz el 28 de Mzo. de 2020
If the rectangle is always parallel with the axes, you could use the boundingbox property of regionprops() to get the size of the rectangle.

Iniciar sesión para comentar.

Respuesta aceptada

Ameer Hamza
Ameer Hamza el 28 de Mzo. de 2020
Editada: Ameer Hamza el 28 de Mzo. de 2020
Try this:
% im is the MNIST image
sum_row = sum(im, 2);
sum_col = sum(im, 1);
min_row = find(sum_row, 1);
max_row = find(sum_row, 1, 'last');
min_col = find(sum_col, 1);
max_col = find(sum_col, 1, 'last');
width = max_col-min_col+1;
height = max_row-min_row+1;
aspect_ratio = height/width;
This solution is also much faster then regionprops() and both give the same result as shown by following example
tic
sum_row = sum(im, 2);
sum_col = sum(im, 1);
min_row = find(sum_row, 1);
max_row = find(sum_row, 1, 'last');
min_col = find(sum_col, 1);
max_col = find(sum_col, 1, 'last');
width = max_col-min_col+1;
height = max_row-min_row+1;
aspect_ratio1 = height/width;
toc
tic
props = regionprops(imbinarize(im));
aspect_ratio2 = props.BoundingBox(4)/props.BoundingBox(3);
toc
Eq = isequal(aspect_ratio1, aspect_ratio2);
Result:
t1 =
2.6254e-04
>> t2
t2 =
0.0145
>> Eq
Eq =
logical
1
  5 comentarios
Adam Danz
Adam Danz el 29 de Mzo. de 2020
Note that if the numbers in the image aren't already segmented, regionprops may come in handy.
Ameer Hamza
Ameer Hamza el 29 de Mzo. de 2020
Yes, this solution is just specific for the MNIST dataset. For a more general case, I will fail.

Iniciar sesión para comentar.

Más respuestas (0)

Community Treasure Hunt

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

Start Hunting!

Translated by