Objective c 从网络摄像头获取像素颜色

Objective c 从网络摄像头获取像素颜色,objective-c,macos,Objective C,Macos,我试图从网络摄像头显示的图像中获取像素颜色。我想看看像素颜色是如何随时间变化的 我目前的解决方案消耗了大量CPU,它工作正常并给出了正确的答案,但我不能100%确定我是否做得正确,或者我可以减少一些步骤 - (IBAction)addFrame:(id)sender { // Get the most recent frame // This must be done in a @synchronized block because the delegate method tha

我试图从网络摄像头显示的图像中获取像素颜色。我想看看像素颜色是如何随时间变化的

我目前的解决方案消耗了大量CPU,它工作正常并给出了正确的答案,但我不能100%确定我是否做得正确,或者我可以减少一些步骤

- (IBAction)addFrame:(id)sender
{
    // Get the most recent frame
    // This must be done in a @synchronized block because the delegate method that sets the most recent frame is not called on the main thread
    CVImageBufferRef imageBuffer;

    @synchronized (self) {
        imageBuffer = CVBufferRetain(mCurrentImageBuffer);
    }

    if (imageBuffer) {
        // Create an NSImage and add it to the movie
        // I think I can remove some steps here, but not sure where.
        NSCIImageRep *imageRep = [NSCIImageRep imageRepWithCIImage:[CIImage imageWithCVImageBuffer:imageBuffer]];
        NSSize n = {320,160 };
        //NSImage *image = [[[NSImage alloc] initWithSize:[imageRep size]] autorelease];
        NSImage *image = [[[NSImage alloc] initWithSize:n] autorelease];
        [image addRepresentation:imageRep];
        CVBufferRelease(imageBuffer);


        NSBitmapImageRep* raw_img = [NSBitmapImageRep imageRepWithData:[image TIFFRepresentation]];
        NSLog(@"image width is %f", [image size].width);
        NSColor* color = [raw_img colorAtX:1279 y:120];

        float colourValue = [color greenComponent]+ [color redComponent]+ [color blueComponent];
        [graphView setXY:10 andY:200*colourValue/3];

    NSLog(@"%0.3f", colourValue);
非常感谢您的帮助,我很乐意尝试其他想法。
谢谢大家。

有几种方法可以提高效率。请看中的
imageFromSampleBuffer:
方法,它提供了一种从
CVImageBufferRef
获取图像的更干净方法(示例使用
UIImage
,但对于
NSImage
,它实际上是相同的)


您还可以直接从
CVImageBufferRef
中提取像素值,无需任何转换。有了缓冲区的基址后,就可以计算任何像素的偏移量,然后从中读取值。

有几种方法可以提高效率。请看中的
imageFromSampleBuffer:
方法,它提供了一种从
CVImageBufferRef
获取图像的更干净方法(示例使用
UIImage
,但对于
NSImage
,它实际上是相同的)


您还可以直接从
CVImageBufferRef
中提取像素值,无需任何转换。一旦你有了缓冲区的基址,你就可以计算任何像素的偏移量,然后从中读取值。

谢谢phil,我用过这个指南,并努力从缓冲区中取出数据。谢谢phil,我用过这个指南,并努力从缓冲区中取出数据。