image-processing - 使用网络摄像头检测心跳?

标签 image-processing processing javacv heartbeat

我正在尝试创建一个可以使用您的计算机网络摄像头检测心跳的应用程序。自 2 周以来我一直在研究代码并开发了这段代码,到目前为止我已经到了

它是如何工作的?如下图...

  • 使用opencv检测人脸
  • 获取额头图像
  • 应用过滤器将其转换为灰度图像[您可以跳过它]
  • 查找每帧绿色像素的平均强度
  • 将平均值保存到数组中
  • 应用 FFT(我使用过最小库)从 FFT 频谱中提取心跳(在这里,我需要一些帮助)

  • 在这里,我需要帮助从 FFT 频谱中提取心跳。谁能帮我。 Here , 是在 python 中开发的类似应用程序,但我无法理解这段代码,所以我在处理中开发相同的应用程序。任何人都可以帮助我理解这个 python 代码中提取心跳的部分。
    //---------import required ilbrary -----------
    import gab.opencv.*;
    import processing.video.*;
    import java.awt.*;
    import java.util.*;
    import ddf.minim.analysis.*;
    import ddf.minim.*;
    //----------create objects---------------------------------
    Capture video; // camera object
    OpenCV opencv; // opencv object
    Minim       minim;
    FFT         fft;
    //IIRFilter filt;
    //--------- Create ArrayList--------------------------------
    ArrayList<Float> poop = new ArrayList(); 
    float[] sample;
    int bufferSize = 128;
    int sampleRate = 512;
    int bandWidth = 20;
    int centerFreq = 80;
    //---------------------------------------------------
    void setup() {
      size(640, 480); // size of the window
      minim = new Minim(this);
      fft = new FFT( bufferSize, sampleRate);
      video = new Capture(this, 640/2, 480/2); // initializing video object
      opencv = new OpenCV(this, 640/2, 480/2); // initializing opencv object
      opencv.loadCascade(OpenCV.CASCADE_FRONTALFACE);  // loading haar cscade file for face detection
      video.start(); // start video
    }
    
    void draw() {
      background(0);
      // image(video, 0, 0 ); // show video in the background
      opencv.loadImage(video);
      Rectangle[] faces = opencv.detect();
      video.loadPixels();
      //------------ Finding faces in the video ----------- 
      float gavg = 0;
      for (int i = 0; i < faces.length; i++) {
        noFill();
        stroke(#FFB700); // yellow rectangle
        rect(faces[i].x, faces[i].y, faces[i].width, faces[i].height); // creating rectangle around the face (YELLOW)
        stroke(#0070FF); //blue rectangle
        rect(faces[i].x, faces[i].y, faces[i].width, faces[i].height-2*faces[i].height/3); // creating a blue rectangle around the forehead
        //-------------------- storing forehead white rectangle part into an image -------------------
        stroke(0, 255, 255);
        rect(faces[i].x+faces[i].width/2-15, faces[i].y+15, 30, 15);
        PImage img = video.get(faces[i].x+faces[i].width/2-15, faces[i].y+15, 30, 15); // storing the forehead aera into a image
        img.loadPixels();
        img.filter(GRAY); // converting capture image rgb to gray
        img.updatePixels();
    
        int numPixels = img.width*img.height;
        for (int px = 0; px < numPixels; px++) { // For each pixel in the video frame...
          final color c = img.pixels[px];
          final color luminG = c>>010 & 0xFF;
          final float luminRangeG = luminG/255.0;
          gavg = gavg + luminRangeG;
        }
    
        //--------------------------------------------------------
        gavg = gavg/numPixels;
        if (poop.size()< bufferSize) {
          poop.add(gavg);
        }
        else poop.remove(0);
      }
      sample = new float[poop.size()];
      for (int i=0;i<poop.size();i++) {
        Float f = (float) poop.get(i);
        sample[i] = f;
      }
    
      if (sample.length>=bufferSize) {
        //fft.window(FFT.NONE); 
        fft.forward(sample, 0);
        //    bpf = new BandPass(centerFreq, bandwidth, sampleRate);
        //    in.addEffect(bpf);
        float bw = fft.getBandWidth(); // returns the width of each frequency band in the spectrum (in Hz).
        println(bw); // returns 21.5332031 Hz for spectrum [0] & [512]
    
        for (int i = 0; i < fft.specSize(); i++)
        {
          // println( " Freq" + max(sample));
          stroke(0, 255, 0);
          float x = map(i, 0, fft.specSize(), 0, width);
          line( x, height, x, height - fft.getBand(i)*100);
         // text("FFT FREQ " + fft.getFreq(i), width/2-100, 10*(i+1));
         // text("FFT BAND " + fft.getBand(i), width/2+100, 10*(i+1));
        }
      }
      else {
        println(sample.length + " " + poop.size());
      }
    }
    
    void captureEvent(Capture c) {
      c.read();
    }
    

    最佳答案

    FFT 应用于具有 128 个样本的窗口。

    int bufferSize = 128;
    

    在绘制方法期间,样本存储在一个数组中,直到填充要应用的 FFT 的缓冲区。然后在此之后缓冲区保持满。要插入新样本,最旧的样本将被移除。 gavg 是平均灰色 channel 颜色。
    gavg = gavg/numPixels;
    if (poop.size()< bufferSize) {
      poop.add(gavg);
    }
    else poop.remove(0);
    

    处理粪便样本
    sample = new float[poop.size()];
    for (int i=0;i < poop.size();i++) {
        Float f = (float) poop.get(i);
        sample[i] = f;
    }
    

    现在可以将 FFT 应用于采样数组
    fft.forward(sample, 0);
    

    代码中仅显示频谱结果。必须计算心跳频率。
    对于 fft 中的每个频段,您必须找到最大值,该位置就是心跳频率。
    for(int i = 0; i < fft.specSize(); i++)
    { // draw the line for frequency band i, scaling it up a bit so we can see it
        heartBeatFrequency = max(heartBeatFrequency,fft.getBand(i));
    }
    

    然后获取带宽以知道频率。
    float bw = fft.getBandWidth();
    

    调整频率。
    heartBeatFrequency = fft.getBandWidth() * heartBeatFrequency ;
    

    关于image-processing - 使用网络摄像头检测心跳?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27310426/

    相关文章:

    image-processing - 在 PHP 中将上传的图像转换为灰度

    algorithm - 围绕其中心旋转一个矩形?

    image - 检测像素变化所需的快速算法

    android - UnsatisfiedLinkError for libopencv_core.so while .apk holds this library

    opencv - 通过对象和轮廓点消除没有棋盘的相机失真

    php - 如何在使用PHP删除图像后显示图像

    python - YUV420p转其他格式,色偏问题

    java - 为 Julia 集生成自定义调色板

    java - 设置处理 vector 的恒定速度

    javacv cvExtractSURF