在 MATLAB 中使用网络摄像头计算风扇的 RPM

问题描述 投票:0回答:1

我通过检测黄色风扇的中心并在其中一个叶片上放置蓝色信号来进行此计算。

然而,当我点击运行时,两个“for”的子图都没有出现在两个单独的图中,第一个“for”的子图出现然后消失,第二个“for”的子图出现。

也在这里:

tetha=tetha+(a2-a1)+2*pi;
我不知道是留下
2*pi
还是删除它。

我有:

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

给我我需要添加的代码,以在不同的图中显示 2 个“for”的子图 如果我保留 +2*pi

,请告诉我
matlab image-processing webcam color-detection circle-detect
1个回答
0
投票

对于绘图部分,您有 2 个选项:

  1. 使用 subplot() 和 subimage() 而不是 imshow(),像这样:

    子图(2,5,i);子图像(img_rect);

但由于兼容性,不建议通过文档推荐。

  1. 使用 imshow() 和 tiledlayout() 而不是 subplot(),像这样:

    tiledlayout('flow') %如果你事先知道维度比流更好 下一步 imshow(img_rect) 次品 ...

有关此事的文档,请参见此处:在此处输入链接描述

对于 2pi 问题,您应该真正详细解释代码的作用以及该行的目标是什么。通过快速阅读它,我会说保留它是有意义的,因为刀片将它偏移了一整圈到起点 (2pi) 而不是半圈 (pi)。但是如果没有适当的解释就很难说

© www.soinside.com 2019 - 2024. All rights reserved.