2012-02-14 49 views
26

我需要与应用程序相同的功能即时心率使用相机检测心率

的基本过程需要用户:

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

这可以通过开启闪光灯并在血液通过食指移动时观察光线的变化来完成。

如何从视频捕获中获取光照水平数据?我应该在哪里找这个? 我翻遍了类AVCaptureDevice,但没有发现任何有用的东西。

我还发现AVCaptureDeviceSubjectAreaDidChangeNotification,那该有用吗?

+0

但iphone和ipad的非闪存模式呢? – 2012-05-18 11:58:06

+0

@AalokParikh:如果您的环境中有足够的光线,手机闪光灯不是必需的。 – alinoz 2012-05-18 13:01:04

+0

@alinoz手机闪光* *是本相机应用所必需的。用手指靠在镜头上,否则就会看到黑色。 – occulus 2012-09-07 15:48:52

回答

3

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

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

+2

嗨alinoz,你可能会发布一些示例代码?提前致谢! – Brabbeldas 2012-12-18 20:03:26

+0

@AT_AB和Brabbeldas - 我会在接下来的几天尝试做一个小设置。 – alinoz 2013-01-21 13:47:11

+1

@alinoz - 你有机会为此提供一个示例项目/代码吗?请让我们知道,如果你把东西放在一起 – 2013-05-06 03:44:11

25

退房这个..

// 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); 
    } 

示例代码Here

+3

此代码仅给出图表。但是,获得每分钟100次的心率bpm值的逻辑是什么?任何想法? – coder1010 2013-10-14 07:09:14

+2

相同的代码链接已损坏。请修复 – Viper 2015-01-17 23:36:16

3

作为一个侧面说明,你可能感兴趣的this research paper。这种方法甚至不需要手指(或任何东西)直接在镜头上。

+3

该文章是正在申请专利的研究,以备对您有所帮助 – toolbear 2013-04-05 20:20:42

+0

您能提供一份有关它正在申请专利或待批准的参考吗? – AlexK 2017-08-02 01:10:03