gpt4 book ai didi

iOS Accelerate Framework vImage - 性能改进?

转载 作者:可可西里 更新时间:2023-11-01 05:01:55 30 4
gpt4 key购买 nike

我一直在使用 OpenCV 和 Apple 的 Accelerate 框架,发现 Accelerate 的性能很慢,Apple 的文档也很有限。让我们举个例子:

void equalizeHistogram(const cv::Mat &planar8Image, cv::Mat &equalizedImage)
{
cv::Size size = planar8Image.size();
vImage_Buffer planarImageBuffer = {
.width = static_cast<vImagePixelCount>(size.width),
.height = static_cast<vImagePixelCount>(size.height),
.rowBytes = planar8Image.step,
.data = planar8Image.data
};

vImage_Buffer equalizedImageBuffer = {
.width = static_cast<vImagePixelCount>(size.width),
.height = static_cast<vImagePixelCount>(size.height),
.rowBytes = equalizedImage.step,
.data = equalizedImage.data
};

TIME_START(VIMAGE_EQUALIZE_HISTOGRAM);
vImage_Error error = vImageEqualization_Planar8(&planarImageBuffer, &equalizedImageBuffer, kvImageNoFlags);
TIME_END(VIMAGE_EQUALIZE_HISTOGRAM);
if (error != kvImageNoError) {
NSLog(@"%s, vImage error %zd", __PRETTY_FUNCTION__, error);
}
}

此调用大约需要 20 毫秒。这具有在我的应用程序中无法使用的实际意义。也许直方图的均衡本来就很慢,但我也测试了 BGRA->Grayscale,发现 OpenCV 可以在 ~5ms 内完成,而 vImage 需要 ~20ms。

在测试其他功能时,我发现了一个 project that made a simple slider appblur function (gist)我清理了测试。大约也是 20 毫秒。

是否有一些技巧可以使这些功能更快?

最佳答案

要使用 equalizeHistogram 函数每秒获得 30 帧,您必须对图像进行去交错处理(从 ARGBxxxx 转换为 PlanarX)并仅均衡 R(ed)G(reen)B(lue);如果均衡 A(lpha),帧率将下降到至少 24。

这是完全按照您的要求执行的代码:

- (CVPixelBufferRef)copyRenderedPixelBuffer:(CVPixelBufferRef)pixelBuffer {

CVPixelBufferLockBaseAddress( pixelBuffer, 0 );

unsigned char *base = (unsigned char *)CVPixelBufferGetBaseAddress( pixelBuffer );
size_t width = CVPixelBufferGetWidth( pixelBuffer );
size_t height = CVPixelBufferGetHeight( pixelBuffer );
size_t stride = CVPixelBufferGetBytesPerRow( pixelBuffer );

vImage_Buffer _img = {
.data = base,
.height = height,
.width = width,
.rowBytes = stride
};

vImage_Error err;
vImage_Buffer _dstA, _dstR, _dstG, _dstB;

err = vImageBuffer_Init( &_dstA, height, width, 8 * sizeof( uint8_t ), kvImageNoFlags);
if (err != kvImageNoError)
NSLog(@"vImageBuffer_Init (alpha) error: %ld", err);

err = vImageBuffer_Init( &_dstR, height, width, 8 * sizeof( uint8_t ), kvImageNoFlags);
if (err != kvImageNoError)
NSLog(@"vImageBuffer_Init (red) error: %ld", err);

err = vImageBuffer_Init( &_dstG, height, width, 8 * sizeof( uint8_t ), kvImageNoFlags);
if (err != kvImageNoError)
NSLog(@"vImageBuffer_Init (green) error: %ld", err);

err = vImageBuffer_Init( &_dstB, height, width, 8 * sizeof( uint8_t ), kvImageNoFlags);
if (err != kvImageNoError)
NSLog(@"vImageBuffer_Init (blue) error: %ld", err);

err = vImageConvert_ARGB8888toPlanar8(&_img, &_dstA, &_dstR, &_dstG, &_dstB, kvImageNoFlags);
if (err != kvImageNoError)
NSLog(@"vImageConvert_ARGB8888toPlanar8 error: %ld", err);

err = vImageEqualization_Planar8(&_dstR, &_dstR, kvImageNoFlags);
if (err != kvImageNoError)
NSLog(@"vImageEqualization_Planar8 (red) error: %ld", err);

err = vImageEqualization_Planar8(&_dstG, &_dstG, kvImageNoFlags);
if (err != kvImageNoError)
NSLog(@"vImageEqualization_Planar8 (green) error: %ld", err);

err = vImageEqualization_Planar8(&_dstB, &_dstB, kvImageNoFlags);
if (err != kvImageNoError)
NSLog(@"vImageEqualization_Planar8 (blue) error: %ld", err);

err = vImageConvert_Planar8toARGB8888(&_dstA, &_dstR, &_dstG, &_dstB, &_img, kvImageNoFlags);
if (err != kvImageNoError)
NSLog(@"vImageConvert_Planar8toARGB8888 error: %ld", err);

err = vImageContrastStretch_ARGB8888( &_img, &_img, kvImageNoError );
if (err != kvImageNoError)
NSLog(@"vImageContrastStretch_ARGB8888 error: %ld", err);

free(_dstA.data);
free(_dstR.data);
free(_dstG.data);
free(_dstB.data);

CVPixelBufferUnlockBaseAddress( pixelBuffer, 0 );

return (CVPixelBufferRef)CFRetain( pixelBuffer );

请注意,我分配了 alpha channel ,尽管我没有对其执行任何操作;这仅仅是因为在 ARGB8888 和 Planar8 之间来回转换需要 alpha channel 缓冲区分配和引用。无论如何,性能和质量都得到了提升。

另请注意,我在将 Planar8 缓冲区转换为单个 ARGB8888 缓冲区后执行对比度拉伸(stretch);那是因为它比逐个 channel 应用函数更快,就像我对直方图均衡函数所做的那样,并且得到与单独执行相同的结果(对比度拉伸(stretch)函数不会导致与直方图均衡相同的 alpha channel 失真) .

关于iOS Accelerate Framework vImage - 性能改进?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28739404/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com