使用AVFoundation处理视频像素的亮度值(Y')

时间:2012-02-09 19:49:30

标签: iphone objective-c ios macos avfoundation

我无法访问视频像素中的亮度值。我正在使用的视频都是黑色的,所以我希望亮度值接近0.这对于视频上半部分的像素来说是正确的,但对于下半部分则没有那么多 - 这就是为什么我认为我读错了路。

上面设置的视频设置:

[videoSettings setObject:[NSNumber numberWithInt:kCVPixelFormatType_420YpCbCr8Planar] forKey:(NSString*)kCVPixelBufferPixelFormatTypeKey];

这是我的代码:

    CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer); 

    // Lock the image buffer
    CVPixelBufferLockBaseAddress(imageBuffer,0); 

    // Get information of the image
    uint8_t *baseAddress = (uint8_t *)CVPixelBufferGetBaseAddress(imageBuffer);
    size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);
    size_t width = CVPixelBufferGetWidth(imageBuffer);
    size_t height = CVPixelBufferGetHeight(imageBuffer); 

    NSLog(@"dimensions: %lu x %lu", width, height);

    for (uint y = 0; y < height; y += pixelSkipHeight)
    {
        uint8_t *p = baseAddress + y * bytesPerRow; // this gets Y' in YUV

        for (uint x = 0; x < width; x += pixelSkipWidth)
        {

           if (x == 33 * 20)
                NSLog(@"color at %dx%d: %u", x, y, p[0]);

        }


    }

    // Unlock the image buffer
    CVPixelBufferUnlockBaseAddress(imageBuffer,0);


    CMSampleBufferInvalidate(sampleBuffer);
    CFRelease(sampleBuffer);
    sampleBuffer = nil; // NULL?

这是我的输出:

2012-02-08 10:29:12.142 MyApp[5193:15103] dimensions: 1920 x 1080
2012-02-08 10:29:12.143 MyApp[5193:15103] color at 660x0: 0
2012-02-08 10:29:12.144 MyApp[5193:15103] color at 660x33: 5
2012-02-08 10:29:12.145 MyApp[5193:15103] color at 660x66: 3
2012-02-08 10:29:12.146 MyApp[5193:15103] color at 660x99: 4
2012-02-08 10:29:12.146 MyApp[5193:15103] color at 660x132: 4
2012-02-08 10:29:12.147 MyApp[5193:15103] color at 660x165: 4
2012-02-08 10:29:12.148 MyApp[5193:15103] color at 660x198: 4
2012-02-08 10:29:12.149 MyApp[5193:15103] color at 660x231: 5
2012-02-08 10:29:12.150 MyApp[5193:15103] color at 660x264: 5
2012-02-08 10:29:12.151 MyApp[5193:15103] color at 660x297: 3
2012-02-08 10:29:12.152 MyApp[5193:15103] color at 660x330: 5
2012-02-08 10:29:12.154 MyApp[5193:15103] color at 660x363: 4
2012-02-08 10:29:12.156 MyApp[5193:15103] color at 660x396: 6
2012-02-08 10:29:12.157 MyApp[5193:15103] color at 660x429: 6
2012-02-08 10:29:12.158 MyApp[5193:15103] color at 660x462: 7
2012-02-08 10:29:12.159 MyApp[5193:15103] color at 660x495: 5
2012-02-08 10:29:12.160 MyApp[5193:15103] color at 660x528: 3
2012-02-08 10:29:12.161 MyApp[5193:15103] color at 660x561: 6
2012-02-08 10:29:12.162 MyApp[5193:15103] color at 660x594: 2
2012-02-08 10:29:12.163 MyApp[5193:15103] color at 660x627: 4
2012-02-08 10:29:12.164 MyApp[5193:15103] color at 660x660: 4
2012-02-08 10:29:12.165 MyApp[5193:15103] color at 660x693: 6
2012-02-08 10:29:12.166 MyApp[5193:15103] color at 660x726: 128
2012-02-08 10:29:12.168 MyApp[5193:15103] color at 660x759: 129
2012-02-08 10:29:12.169 MyApp[5193:15103] color at 660x792: 128
2012-02-08 10:29:12.171 MyApp[5193:15103] color at 660x825: 129
2012-02-08 10:29:12.172 MyApp[5193:15103] color at 660x858: 128
2012-02-08 10:29:12.175 MyApp[5193:15103] color at 660x891: 128
2012-02-08 10:29:12.177 MyApp[5193:15103] color at 660x924: 128
2012-02-08 10:29:12.177 MyApp[5193:15103] color at 660x957: 128
2012-02-08 10:29:12.198 MyApp[5193:15103] color at 660x990: 128
2012-02-08 10:29:12.199 MyApp[5193:15103] color at 660x1023: 128
2012-02-08 10:29:12.200 MyApp[5193:15103] color at 660x1056: 127

有什么想法吗?提前谢谢!

1 个答案:

答案 0 :(得分:0)

我不知道这种像素格式的细节,但如果它是平面的,那么图像就不会像:

Plane 1 [Y0 Y1 ... YN]
Plane 2 [U0 U1 ... UN/4]
Plane 3 [V0 V1 ... VN/4]

在你的输出中,前三分之二是亮度样本,最后三分之一是四分之一分辨率的两个色度部分,正如我所期望的那样。您似乎期待交错的样本。

http://en.wikipedia.org/wiki/YUV#Y.27UV420p_.28and_Y.27V12_or_YV12.29我认为那里的彩色表格解释了我的意思。

相关问题