ios - 使用 vImageHistogramCalculation 计算图像的直方图

标签 ios iphone macos accelerate-framework vimage

我正在尝试使用 vImage 的 vImageHistogramCalculation_ARGBFFFF 计算图像的直方图,但我得到了 kvImageNullPointerArgument 类型的 vImage_Error(错误代码 -21772)。

这是我的代码:

- (void)histogramForImage:(UIImage *)image {

    //setup inBuffer
    vImage_Buffer inBuffer;

    //Get CGImage from UIImage
    CGImageRef img = image.CGImage;

    //create vImage_Buffer with data from CGImageRef
    CGDataProviderRef inProvider = CGImageGetDataProvider(img);
    CFDataRef inBitmapData = CGDataProviderCopyData(inProvider);

    //The next three lines set up the inBuffer object
    inBuffer.width = CGImageGetWidth(img);
    inBuffer.height = CGImageGetHeight(img);
    inBuffer.rowBytes = CGImageGetBytesPerRow(img);

    //This sets the pointer to the data for the inBuffer object
    inBuffer.data = (void*)CFDataGetBytePtr(inBitmapData);

    //Prepare the parameters to pass to vImageHistogramCalculation_ARGBFFFF
    vImagePixelCount *histogram[4] = {0};
    unsigned int histogram_entries = 4;
    Pixel_F minVal = 0;
    Pixel_F maxVal = 255;
    vImage_Flags flags = kvImageNoFlags;

    vImage_Error error = vImageHistogramCalculation_ARGBFFFF(&inBuffer,
                                                             histogram,
                                                             histogram_entries,
                                                             minVal,
                                                             maxVal,
                                                             flags);
    if (error) {
        NSLog(@"error %ld", error);
    }

    //clean up
    CGDataProviderRelease(inProvider);
}

我怀疑它与我的 histogram 参数有关,根据文档,该参数应该是“指向四个直方图数组的指针”。我声明正确吗?

谢谢。

最佳答案

问题在于您没有分配空间来保存计算出的直方图。如果您只在本地使用直方图,您可以像这样将它们放在堆栈上 [请注意,我使用八个 bin 而不是四个,以使示例更清楚]:

// create an array of four histograms with eight entries each.
vImagePixelCount histogram[4][8] = {{0}};
// vImageHistogramCalculation requires an array of pointers to the histograms.
vImagePixelCount *histogramPointers[4] = { &histogram[0][0], &histogram[1][0], &histogram[2][0], &histogram[3][0] };
vImage_Error error = vImageHistogramCalculation_ARGBFFFF(&inBuffer, histogramPointers, 8, 0, 255, kvImageNoFlags);
// You can now access bin j of the histogram for channel i as histogram[i][j].
// The storage for the histogram will be cleaned up when execution leaves the
// current lexical block.

如果您需要直方图停留在函数范围之外,则需要在堆上为它们分配空间:

vImagePixelCount *histogram[4];
unsigned int histogramEntries = 8;
histogram[0] = malloc(4*histogramEntries*sizeof histogram[0][0]);
if (!histogram[0]) { // handle error however is appropriate }
for (int i=1; i<4; ++i) { histogram[i] = &histogram[0][i*histogramEntries]; }
vImage_Error error = vImageHistogramCalculation_ARGBFFFF(&inBuffer, histogram, 8, 0, 255, kvImageNoFlags);
// You can now access bin j of the histogram for channel i as histogram[i][j].
// Eventually you will need to free(histogram[0]) to release the storage.

希望这对您有所帮助。

关于ios - 使用 vImageHistogramCalculation 计算图像的直方图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25633053/

相关文章:

ios - 在 Swift 中强制定向后恢复自动旋转

iOS:保存双选择器的设置(数据持久性)

iphone - 使用 UIImage imageNamed 方法呈现图 block 的性能问题

ios - 如果重复设置为每天,则不会触发本地通知

iphone - 如何使用 UIsearchBar 委托(delegate)方法关闭键盘?我尝试了所有方法,但没有用。

objective-c - 将按钮添加到 OSX 锁定屏幕

macos - 如何在 macOSX 终端命令行上将 makeinfo/texinfo 从 4.8 版升级/更新到 4.13 版?

ios - UiView setFrame 对象

iphone - 奇怪的 NSMutable 数组错误

c++ - 如何在 OSX 上使用 C++ 更改罗技 c920 网络摄像头设置?