gpt4 book ai didi

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

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

我正在尝试使用 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/

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