iOS 心率检测算法

标签 ios iphone objective-c algorithm camera

我正在尝试在我正在开发的应用中实现心跳记录功能。

这样做的首选方法是使用 iPhone 的摄像头打开灯,让用户将手指放在镜头上,并检测视频输入中的波动,这与用户的心脏相对应。

我从以下堆栈溢出问题中找到了一个很好的起点 here

该问题提供了有用的代码来绘制心跳时间图。

它展示了如何启动一个 AVCaptureSession 并像这样打开相机的灯:

session = [[AVCaptureSession alloc] init];

AVCaptureDevice* camera = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if([camera isTorchModeSupported:AVCaptureTorchModeOn]) {
    [camera lockForConfiguration:nil];
    camera.torchMode=AVCaptureTorchModeOn;
    //  camera.exposureMode=AVCaptureExposureModeLocked;
    [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];
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];

本例中的self必须是<AVCaptureVideoDataOutputSampleBufferDelegate> 因此必须实现以下方法来获取原始相机数据:

- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection {
static int count=0;
count++;
// only run if we're not already processing an image
// 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);
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];
        //          a+=buf[x+3];
    }
    buf+=bprow;
}
r/=255*(float) (width*height);
g/=255*(float) (width*height);
b/=255*(float) (width*height);

float h,s,v;

RGBtoHSV(r, g, b, &h, &s, &v);

// simple highpass and lowpass filter 

static float lastH=0;
float highPassValue=h-lastH;
lastH=h;
float lastHighPassValue=0;
float lowPassValue=(lastHighPassValue+highPassValue)/2;

lastHighPassValue=highPassValue;

    //low pass value can now be used for basic heart beat detection


}

RGB 被转换为 HSV,并且监控的是 Hue 的波动。

而RGB转HSV的实现如下

void RGBtoHSV( float r, float g, float b, float *h, float *s, float *v ) {
float min, max, delta; 
min = MIN( r, MIN(g, b )); 
max = MAX( r, MAX(g, b )); 
*v = max;
delta = max - min; 
if( max != 0 )
    *s = delta / max;
else {
    // r = g = b = 0 
    *s = 0; 
    *h = -1; 
    return;
}
if( r == max )
    *h = ( g - b ) / delta; 
else if( g == max )
    *h=2+(b-r)/delta;
else 
    *h=4+(r-g)/delta; 
*h *= 60;
if( *h < 0 ) 
    *h += 360;
}

capureOutput: 中计算的低通值最初提供不稳定的数据,但随后稳定到以下:

2013-11-04 16:18:13.619 SampleHeartRateApp[1743:1803] -0.071218
2013-11-04 16:18:13.719 SampleHeartRateApp[1743:1803] -0.050072
2013-11-04 16:18:13.819 SampleHeartRateApp[1743:1803] -0.011375
2013-11-04 16:18:13.918 SampleHeartRateApp[1743:1803] 0.018456
2013-11-04 16:18:14.019 SampleHeartRateApp[1743:1803] 0.059024
2013-11-04 16:18:14.118 SampleHeartRateApp[1743:1803] 0.052198
2013-11-04 16:18:14.219 SampleHeartRateApp[1743:1803] 0.078189
2013-11-04 16:18:14.318 SampleHeartRateApp[1743:1803] 0.046035
2013-11-04 16:18:14.419 SampleHeartRateApp[1743:1803] -0.113153
2013-11-04 16:18:14.519 SampleHeartRateApp[1743:1803] -0.079792
2013-11-04 16:18:14.618 SampleHeartRateApp[1743:1803] -0.027654
2013-11-04 16:18:14.719 SampleHeartRateApp[1743:1803] -0.017288

最初提供的不稳定数据的示例如下:

2013-11-04 16:17:28.747 SampleHeartRateApp[1743:3707] 17.271435
2013-11-04 16:17:28.822 SampleHeartRateApp[1743:1803] -0.049067
2013-11-04 16:17:28.922 SampleHeartRateApp[1743:1803] -6.524201
2013-11-04 16:17:29.022 SampleHeartRateApp[1743:1803] -0.766260
2013-11-04 16:17:29.137 SampleHeartRateApp[1743:3707] 9.956407
2013-11-04 16:17:29.221 SampleHeartRateApp[1743:1803] 0.076244
2013-11-04 16:17:29.321 SampleHeartRateApp[1743:1803] -1.049292
2013-11-04 16:17:29.422 SampleHeartRateApp[1743:1803] 0.088634
2013-11-04 16:17:29.522 SampleHeartRateApp[1743:1803] -1.035559
2013-11-04 16:17:29.621 SampleHeartRateApp[1743:1803] 0.019196
2013-11-04 16:17:29.719 SampleHeartRateApp[1743:1803] -1.027754
2013-11-04 16:17:29.821 SampleHeartRateApp[1743:1803] 0.045803
2013-11-04 16:17:29.922 SampleHeartRateApp[1743:1803] -0.857693
2013-11-04 16:17:30.021 SampleHeartRateApp[1743:1803] 0.061945
2013-11-04 16:17:30.143 SampleHeartRateApp[1743:1803] -0.701269

只要有心跳,低通值就会变为正值。所以我尝试了一个非常简单的活体检测算法,它基本上是看当前值,如果是正值,它也会查看之前的值,如果是负值,它会检测到负值变为正值并发出哔声。

问题在于数据并不总是像上面那样完美,有时在负读数中会出现异常的正读数,反之亦然。

低通值的时间图如下所示: enter image description here

有趣的是,上述异常很常见,如果我记录一段时间的图表,我会多次看到形状非常相似的异常。

在我非常简单的节拍检测算法中,如果出现如上所示的异常情况,则检测期间(10 秒)内的节拍计数可以增加 4 或 5 次。这使得计算的 BPM 非常不准确。但尽管它很简单,但它确实在大约 70% 的时间里工作。

为了解决这个问题,我尝试了以下方法。

1.开始在数组中记录最后3个低通值

2.然后查看中间值前后是否有两个较小的值。 (基本峰值检测)

3.将此场景计为一个节拍,并将其添加到给定时间内的运行节拍总数中。

然而,这种方法和其他任何方法一样容易受到异常的影响。实际上似乎是一种更糟糕的方法。 (在检测后播放实时哔哔声时,它们似乎比正负算法更不稳定)

我的问题是你能不能帮我想出一个算法,该算法能够以合理的准确度可靠地检测心跳的发生时间。

我意识到我必须解决的另一个问题是检测用户的手指是否在镜头上。

我想过检测不稳定的低通值,但问题是低通滤波器会解释不稳定的值并随着时间的推移将它们平滑掉。因此,我们也将不胜感激。

感谢您的宝贵时间。

最佳答案

这个问题的答案有点复杂,因为您需要做几件事来处理信号,并且没有单一的“正确”方法可以做到这一点。但是,对于您的过滤器,您想使用 band-pass filter .这种类型的滤波器允许您指定在高端和低端都接受的频率范围。对于人类的心跳,我们知道这些界限应该是多少(不低于 40 bpm 且不高于 250 bpm),因此我们可以创建一个过滤器来去除该范围之外的频率。该过滤器还将数据移动到以零为中心,因此峰值检测变得更加容易。即使您的用户增加/减少他们的手指压力(在一定程度上),此过滤器也会为您提供更平滑的信号。之后,还需要进行额外的平滑和异常值移除。

我使用的一种特定类型的带通滤波器是巴特沃斯滤波器。这有点涉及手动创建,因为过滤器会根据您收集数据的频率而变化。幸运的是,有一个网站可以提供帮助 here .如果您以 30 fps 的速度收集数据,那么频率将为 30 hz。

我创建了一个项目,将所有这些都封装在一起,并且可以很好地检测用户的心率,以便将其包含在我在 iOS 应用商店上的应用中。我已在 github 上提供了心率检测代码.

关于iOS 心率检测算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19773631/

相关文章:

ios - UIPickerView 委托(delegate)方法返回 nil

ios - 如何预览在 .xib 文件中以编程方式创建的项目?

iphone - 具有 "Circular"滚动的 UIScrollView

ios - 多次将完全相同的 NSLayoutConstraints 附加到同一个 UIView 会发生什么?

ios - 在 'CamScanner' 应用程序中进行图像裁剪控制的最佳方法是什么?

iOS 音频操作 - 向后播放本地 .caf 文件

objective-c - 何时重写 objective c getters

ios - 将 UIimageView 置于最前面

ios - 你将如何重新实现 UIControl?它如何适应响应者链?

objective-c - 求多维数组教程资料