Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
See LICENSE folder for this sample’s licensing information.
Abstract:
Class for performing histogram equalization efficiently
*/
#import <Foundation/Foundation.h>
#import "HistogramCalculator.h"
@implementation HistogramCalculator
+(void) calcHistogramForPixelBuffer:(CVPixelBufferRef)pixelBuffer
toBuffer:(float*)histogram
withSize:(int)size
forColors:(int)colors
minDepth:(float)minDepth
maxDepth:(float)maxDepth
binningFactor:(int)factor {
memset(histogram, 0, size * sizeof(histogram[0]));
size_t width = CVPixelBufferGetWidth(pixelBuffer);
size_t height = CVPixelBufferGetHeight(pixelBuffer);
size_t stride = CVPixelBufferGetBytesPerRow(pixelBuffer);
CVPixelBufferLockBaseAddress(pixelBuffer, kCVPixelBufferLock_ReadOnly);
const uint8_t* baseAddress = (const uint8_t*)CVPixelBufferGetBaseAddress(pixelBuffer);
size_t numPoints = 0;
for (size_t y = 0; y < height; ++y) {
const __fp16* data = (const __fp16*)(baseAddress + y * stride);
for (size_t x = 0; x < width; ++x, ++data) {
__fp16 depth = *data;
if (!isnan(depth) && depth > minDepth && depth < maxDepth) {
ushort binIndex = depth * factor;
++histogram[binIndex];
++numPoints;
}
}
}
CVPixelBufferUnlockBaseAddress(pixelBuffer, kCVPixelBufferLock_ReadOnly);
for (int i = 1; i < size; ++i)
histogram[i] += histogram[i-1];
for (int i = 1; i < size; ++i)
histogram[i] = colors * histogram[i] / numPoints;
for (int i = 1; i < size; ++i)
histogram[i] = colors - histogram[i];
}
@end