Compute the histogram of an image using vImageHistogramCalculation

后端 未结 1 1095
灰色年华
灰色年华 2021-01-19 14:44

I\'m trying to compute the histogram of an image using vImage\'s vImageHistogramCalculation_ARGBFFFF, but I\'m getting a vImage_Error of type kvImageNullP

1条回答
  •  孤街浪徒
    2021-01-19 15:10

    The trouble is that you’re not allocating space to hold the computed histograms. If you are only using the histograms locally, you can put them on the stack like so [note that I’m using eight bins instead of four, to make the example more clear]:

    // 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.
    

    If you need the histograms to stick around outside the scope of your function, you’ll need to allocate space for them on the heap instead:

    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.
    

    Hope this helps.

    0 讨论(0)
提交回复
热议问题