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
For the plotting section you have 2 options:
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.
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