使用相机检测心率

ell*_*age 26 camera image-processing objective-c ios avcapturesession

我需要与应用程序即时心率相同的功能.

基本过程要求用户:

  1. 将食指尖轻轻放在相机镜头上.
  2. 施加均匀的压力并覆盖整个镜头.
  3. 保持稳定10秒钟并获得心率.

这可以通过打开闪光灯并在血液通过食指时观察光线变化来实现.

如何从视频捕获中获取亮度数据?我应该在哪里寻找这个?我仔细查看了课程,AVCaptureDevice但没有找到任何有用的东西.

我也发现AVCaptureDeviceSubjectAreaDidChangeNotification,这会有用吗?

小智 26

看看这个..

// switch on the flash in torch mode  
 if([camera isTorchModeSupported:AVCaptureTorchModeOn]) {  
 [camera lockForConfiguration:nil];  
 camera.torchMode=AVCaptureTorchModeOn;  
 [camera unlockForConfiguration];  
 }  

  [session setSessionPreset:AVCaptureSessionPresetLow];

   // Create the AVCapture Session  
   session = [[AVCaptureSession alloc] init];  

  // Get the default camera device  
   AVCaptureDevice* camera = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];  
  if([camera isTorchModeSupported:AVCaptureTorchModeOn]) {  
    [camera lockForConfiguration:nil];  
  camera.torchMode=AVCaptureTorchModeOn;  
    [camera unlockForConfiguration];  
 }  
 // Create a AVCaptureInput with the camera device  
    NSError *error=nil;  
     AVCaptureInput* cameraInput = [[AVCaptureDeviceInput alloc] initWithDevice:camera error:&error];  
   if (cameraInput == nil) {  
    NSLog(@"Error to create camera capture:%@",error);  
  }  

    // Set the output  
    AVCaptureVideoDataOutput* videoOutput = [[AVCaptureVideoDataOutput alloc] init];  

   // create a queue to run the capture on  
  dispatch_queue_t captureQueue=dispatch_queue_create("catpureQueue", NULL);  

   // setup our delegate  
   [videoOutput setSampleBufferDelegate:self queue:captureQueue];  

    // configure the pixel format  
    videoOutput.videoSettings = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber     numberWithUnsignedInt:kCVPixelFormatType_32BGRA], (id)kCVPixelBufferPixelFormatTypeKey,  
     nil];  
   // cap the framerate  
   videoOutput.minFrameDuration=CMTimeMake(1, 10);  
  // and the size of the frames we want  
  [session setSessionPreset:AVCaptureSessionPresetLow];  

   // Add the input and output  
   [session addInput:cameraInput];  
   [session addOutput:videoOutput];  

   // Start the session  

    [session startRunning];  

   - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection {  



   // this is the image buffer  

  CVImageBufferRef cvimgRef = CMSampleBufferGetImageBuffer(sampleBuffer);  


   // Lock the image buffer  

  CVPixelBufferLockBaseAddress(cvimgRef,0);  


  // access the data  

  int width=CVPixelBufferGetWidth(cvimgRef);  
  int height=CVPixelBufferGetHeight(cvimgRef);  


  // get the raw image bytes  
  uint8_t *buf=(uint8_t *) CVPixelBufferGetBaseAddress(cvimgRef);  
  size_t bprow=CVPixelBufferGetBytesPerRow(cvimgRef);  


// get the average red green and blue values from the image  

 float r=0,g=0,b=0;  
 for(int y=0; y<height; y++) {  
 for(int x=0; x<width*4; x+=4) {  
  b+=buf[x];  
  g+=buf[x+1];  
  r+=buf[x+2];  
 }  
 buf+=bprow;  
 }  
  r/=255*(float) (width*height);  
  g/=255*(float) (width*height);  
  b/=255*(float) (width*height);  

  NSLog(@"%f,%f,%f", r, g, b);  
  }  
Run Code Online (Sandbox Code Playgroud)

示例代码 在这里

  • 此代码仅提供图表.但是获得心率的bpm值如100bpm的逻辑是什么呢?任何的想法? (3认同)
  • 相同的代码链接被破坏.请修理 (2认同)

ali*_*noz 3

其实很简单,你必须分析捕获图像的像素值。一种简单的算法是:选择图像中心的区域,转换为灰度,获取每个图像的像素中值,最终得到一个 2D 函数,并在此函数上计算与最小值之间的距离或最大值并解决问题。

如果您查看 5 秒内采集图像的直方图,您会注意到灰度分布的变化。如果您想要更稳健的计算,请分析直方图。

  • 你好,alinoz,你可以发布一些示例代码吗?提前致谢! (2认同)