0

I am doing this calculation by detecting the center of a yellow fan and putting a blue signal on one of its blades.

However when I hit run, the subplots of both "for" do not appear in 2 separate figures, the subplots of the first "for" appear and then disappear and the subplots of the second "for" appear.

also here: tetha=tetha+(a2-a1)+2*pi; I don't know whether to leave the 2*pi or to remove it.

i have:

clear; clc; close all;
cam = webcam(); % Accede a la cámara web
preview(cam); % Mue
stra la vista previa de la cámara web
centros_azul = zeros(10,2);
centros_circulos = zeros(10,2);
centro_x_prom=0;
centro_y_prom=0;
num_valores=0;
tetha=0;

% Tomar 10 fotografías y guardarlas
disp('Comenzamos')
for i=1:10
    % Capturar imagen
    img = snapshot(cam);
    % Guardar imagen en archivo
    filename = sprintf('imagen_%d.jpg', i); % Define el nombre del archivo
    imwrite(img, filename); % Guarda la imagen en un archivo
    fprintf('Imagen %d guardada.\n', i); % Muestra un mensaje en la consola
    pause(1); % Espera 1 segundo antes de tomar la siguiente imagen
end

  %Acceder a las 10 fotografías

for i=1:10
    figure(1)
    % Leer la imagen del archivo
    filename = sprintf('imagen_%d.jpg', i); % Define el nombre del archivo
    img = imread(filename); % Lee la imagen del archivo
    % Convertir la imagen a formato HSV
    img_hsv = rgb2hsv(img);
    % Definir los rangos de los valores de H, S y V para el color amarillo
    h_min = 0.11; h_max = 0.18;
    s_min = 0.3; s_max = 1.0;
    v_min = 0.7; v_max = 1.0;
    % Detectar el color amarillo en la imagen utilizando máscaras
    h_mask = (img_hsv(:,:,1) >= h_min) & (img_hsv(:,:,1) <= h_max);
    s_mask = (img_hsv(:,:,2) >= s_min) & (img_hsv(:,:,2) <= s_max);
    v_mask = (img_hsv(:,:,3) >= v_min) & (img_hsv(:,:,3) <= v_max);
    yellow_mask = (h_mask & s_mask & v_mask);
% Detección de círculos
rangoRadio = [80 200]; % Rango de radios de los círculos a buscar
sensibilidad = 0.95; % Sensibilidad de la detección de círculos
[centros,radios] = imfindcircles(yellow_mask,rangoRadio,'ObjectPolarity','bright','Sensitivity',sensibilidad); % Detectar círculos
% Mostrar la imagen con el círculo amarillo encerrado en un rectángulo
if ~isempty(centros)
    centro = round(centros(1,:));
    centros_circulos(i,:) = centro;
    radio = round(radios(1));
    img_rect = insertShape(img,'Rectangle',[centro(1)-radio centro(2)-radio radio*2 radio*2],'LineWidth',2,'Color','yellow');
    
    subplot(2,5,i); imshow(img_rect);
    title('Círculo amarillo detectado');
else
    
    subplot(2,5,i); imshow(img);
    title('Círculo amarillo no detectado');
end
disp(centros_circulos);%muestra centros almacenados

end
%promedio de centro de circulo amarillo
for i=1:10
centro_x=centros_circulos(i,1);
centro_y=centros_circulos(i,2);
if centro_x ~= 0 && centro_y ~= 0
centro_x_prom=centro_x+centro_x_prom;
centro_y_prom=centro_y+centro_y_prom;
num_valores=num_valores+1;
end
end

if num_valores > 0
    centro_x_prom = centro_x_prom / num_valores;
    centro_y_prom = centro_y_prom / num_valores;
end

%detección del azul
for i=1:10
    figure(2)
    % Leer la imagen del archivo
    filename = sprintf('imagen_%d.jpg', i); % Define el nombre del archivo
    img = imread(filename); % Lee la imagen del archivo
    
    % Convertir la imagen a formato HSV
    img_hsv = rgb2hsv(img);
    % Definir los rangos de los valores de H, S y V para el color azul
    h_min = 0.50; h_max = 0.61;
    s_min = 0.34; s_max = 1.0;
    v_min = 0.47; v_max = 1.0;
    % Detectar el color azul en la imagen utilizando máscaras
    h_mask = (img_hsv(:,:,1) >= h_min) & (img_hsv(:,:,1) <= h_max);
    s_mask = (img_hsv(:,:,2) >= s_min) & (img_hsv(:,:,2) <= s_max);
    v_mask = (img_hsv(:,:,3) >= v_min) & (img_hsv(:,:,3) <= v_max);
    blue_mask = (h_mask & s_mask & v_mask);
   
    subplot(2,5,i); imshow(blue_mask);
    title(sprintf('Imagen %d', i));
    [height, width]=size(blue_mask);
    contador=0;
    xprom=0;
    yprom=0;
    for y=1:height
        for x=1:width
            if(blue_mask(y,x))
                contador=contador+1;    
                xprom=xprom+x;
                yprom=yprom+y;
            end
        end
    end   
   % [row, col] = find(blue_mask);
    xprom=xprom/contador;
    yprom=yprom/contador;
    centros_azul(i,1)=xprom;
    centros_azul(i,2)=yprom;
    fprintf('Coordenadas del color azul en la imagen %d: (%.2f, %.2f)\n', i, xprom, yprom);
end
%calculo del angulo promedio
for i=1:9
x1=centro_x_prom-centros_azul(i,1);
y1=centro_y_prom-centros_azul(i,2);
x2=centro_x_prom-centros_azul(i+1,1);
y2=centro_y_prom-centros_azul(i+1,2);
a1=atan(y1/x1);
a2=atan(y2/x2);
tetha=tetha+(a2-a1)+2*pi;

end
 tetha_promedio=tetha/9;
 tiempo_total=8.96;
 tiempo_entre_foto=0.9956;
 velocidad_angular_rad=tetha_promedio/tiempo_entre_foto;
 RPM=(velocidad_angular_rad*30)/pi;
 fprintf('RPM = %.2f ', RPM); 
clear cam

give me the code I need to add to display the subplots of the 2 "for" in different Figures say me if i keep the +2*pi

Filburt
  • 17,626
  • 12
  • 64
  • 115

2 Answers2

0

For the plotting section you have 2 options:

  1. use subplot() and subimage() instead of imshow(), like so:

    subplot(2,5,i); subimage(img_rect);

but it not recommended via documentation because of compatibility.

  1. use imshow() with tiledlayout() instead of subplot(), like so:

    tiledlayout('flow') %if you know the dimension beforehand is better than flow nexttile imshow(img_rect) nextile ...

see here for documentation on the matter: enter link description here

For the 2pi matter, you should really explain what the code does in detail and what the goal of that line is. By just quickly reading it i would say it makes sense to keep it since the blade offset it a whole round to the starting point (2pi) instead of a half one (pi). But it's hard to tell without a proper explanation

Ferro Luca
  • 459
  • 11
0

1.- What is the rpm range

Common table fan blades spin between 500 and 2000 rpm. Source here.

2.- What camera do you use?

Most of the available cameras that one could think of, at least on first instance, to visually catch such rotating velocity, would be embedded in a smartphone, after all the consumer handheld cameras market crashed some years ago when smart phone cameras quality surpassed hand-held cheap cameras.

Let's say an Apple; a high end smart phone like iPhone14 (from here) has the following image acquisition specs:

4K video recording at 24 fps, 25 fps, 30 fps or 60 fps 1080p HD video recording at 25 fps, 30 fps or 60 fps 720p HD video recording at 30 fps Cinematic mode up to 4K HDR at 30 fps Action mode up to 2.8K at 60 fps HDR video recording with Dolby Vision up to 4K at 60 fps Slo‑mo video support for 1080p at 120 fps or 240 fps

60fps means the acquired time resolution can only go up to 30Hz before incurring alias.

Meaning, with the fastest mode Slo-mo, at 240fps this high-end device can only (potentially speaking) catch rotating blades at 120rpm.

3.- Use laser

You have better chances to correctly measure such rotating velocity with an Arduino or Raspberry Pi board controlling a laser, recording or real time feeding MATLAB with the laser beam interruptions.

Do you have a Raspberry Pi?

4.- High fps cameras

Another solution would be using a high TIME resolution camera, aka high fps camera.

Just check some prices here to realise that the cheapest 'action cameras' may give you many pixels but again have a rather low fps because to catch down-hill skiing or for the matter, to catch any kind of human action you may think of there is no need for 4000fps, when the average customer is only going to splash up to $200 on such devices.

And cheap action cameras may end up rolling down or squashed on the tarmac: high-risk product specs instantaneously pull up design price, and product price: If the parachute is too cheap to be true, just wall away.

5.- Careful with aiming lasers outdoors

Just in case, one thing is to have curiosity to know if the table fan next to your computer is really spinning at the expect revolutions, and the other one is to try to measure the rpm of a turbojet or propfan attached to an aircraft in the airport, or taking off, or landing.

As obvious as it may seem, I have impression that it's important to mention that aiming lasers at a flying aircraft is illegal.

It's like in the US showing a gun for not reason, even with concealed permit, or in Europe for common civilians buying a taser.

And actually on the runway, because the turbines are already on, if caught attempting measurement, it may be as illegal as trying to track flying aircrafts with a laser beam that can potentially temporarily blind pilots hence putting in danger all-on-board, the bird, and whoever/whatever caught on the way down.

So make sure you have all the permits on you, validated, if you are doing these measurements for work.

And if you just want to know how fast an aircraft turbine is spinning on the runway tarmac, be aware that some aircrafts are equipped with basic EW receivers: they know when laser energy is directed at them.

Also, aiming lasers open-door may cause from unconscious nuisance to partial/temporary/permanent blinding of by standers: pedestrians. cyclists, or drivers alike.

Hope it helps.

John BG
  • 690
  • 4
  • 12