2

I am currently trying to display a histogram using GPUImage. I currently have following code:

GPUImageOutput<GPUImageInput> *filter = [[GPUImageHistogramFilter alloc] initWithHistogramType:kGPUImageHistogramLuminance];
[self.stillCamera  removeTarget:filter];
GPUImageGammaFilter *gammaFilter = [[GPUImageGammaFilter alloc] init];
[self.stillCamera  addTarget:gammaFilter];
[gammaFilter addTarget:filter];

GPUImageHistogramGenerator *histogramGraph = [[GPUImageHistogramGenerator alloc] init];

[histogramGraph forceProcessingAtSize:CGSizeMake(500.0, 500)];
[filter addTarget:histogramGraph];

GPUImageAlphaBlendFilter *blendFilter = [[GPUImageAlphaBlendFilter alloc] init];
blendFilter.mix = 0.75;
[blendFilter forceProcessingAtSize:CGSizeMake(500, 500)];

[self.stillCamera addTarget:blendFilter];
[histogramGraph addTarget:blendFilter];

[blendFilter addTarget:previewView];

Above shows the histogram overplayed over my previewView. (It does flicker however, another issue, another day)

I want to show this histogram on a smaller view in a particular location on the view. How can I do this?

Sergey Kuryanov
  • 6,114
  • 30
  • 52
Gizmodo
  • 3,151
  • 7
  • 45
  • 92
  • 3
    I think a `GPUImageTransformFilter` with a translation transform would be a good place to start. – Rhythmic Fistman Apr 25 '15 at 22:01
  • 1
    I cannot be the only person wanting to display this graph on a separate view right? I would imagine there's a formal way to pump this out to another view. – Gizmodo Apr 27 '15 at 15:56

1 Answers1

3

To show histogram in separate view you need add another GPUImageView to your main view and point histogram filter to it. Here is source code based on SimpleImageFilter sample.

- (void)loadView
{    
    CGRect mainScreenFrame = [[UIScreen mainScreen] applicationFrame];

    GPUImageView *primaryView = [[GPUImageView alloc] initWithFrame:mainScreenFrame];
    self.view = primaryView;

    UIImage *inputImage = [UIImage imageNamed:@"WID-small.jpg"];
    GPUImagePicture *sourcePicture = [[GPUImagePicture alloc] initWithImage:inputImage smoothlyScaleOutput:YES];

    // Creating another view to show histogram
    GPUImageView *histView = [[GPUImageView alloc] initWithFrame:CGRectMake(mainScreenFrame.size.width - 100, mainScreenFrame.size.height - 100, 100, 100)];
    [primaryView addSubview:histView];

    // Create histogram filter and generator and point it to histogram view
    GPUImageOutput<GPUImageInput> *histFilter = [[GPUImageHistogramFilter alloc] initWithHistogramType:kGPUImageHistogramLuminance];
    GPUImageOutput<GPUImageInput> *histGenerator = [[GPUImageHistogramGenerator alloc] init];
    [histGenerator forceProcessingAtSize:histView.sizeInPixels];
    [sourcePicture addTarget:histFilter];
    [histFilter addTarget:histGenerator];
    // Note target - hist view
    [histGenerator addTarget:histView];

    // Setup sepia filter just to show main picture
    GPUImageOutput<GPUImageInput> *sepiaFilter = [[GPUImageSepiaFilter alloc] init];
    [sepiaFilter forceProcessingAtSize:primaryView.sizeInPixels];
    [sourcePicture addTarget:sepiaFilter];
    // Note target - main view
    [sepiaFilter addTarget:primaryView];

    [sourcePicture processImage];
}

And final result:
enter image description here

Sergey Kuryanov
  • 6,114
  • 30
  • 52
  • Thank you! I successfully got it to show up. Now, with your method, How do I extract just the luminance or RGB only histograms? – Gizmodo May 01 '15 at 21:45
  • 1
    I am seeing the main preview window is dropping frames considerably when I use this method. Any ideas on how to improve this? Separate queue? – Gizmodo Jun 01 '15 at 06:24