如何使用iOS GPUImage生成直方图?

时间:2021-02-01 14:56:58

Working on https://github.com/luisespinoza/LEColorPicker project, I am trying to generate an histogram UIImage from an arbitrary input UIImage using the project GPUImage (https://github.com/BradLarson/GPUImage).

在https://github.com/luisespinoza/LEColorPicker项目上工作,我试图使用项目GPUImage(https://github.com/BradLarson/GPUImage)从任意输入UIImage生成直方图UIImage。

The current code that I'm using is the following:

我正在使用的当前代码如下:

- (NSDictionary*)dictionaryWithColorsPickedFromImage:(UIImage *)image
{
    GPUImageFilter *filter = [[GPUImageHistogramFilter alloc] initWithHistogramType:kGPUImageHistogramRGB];

    UIImage *filteredImage = [filter imageByFilteringImage:image];

    [UIImagePNGRepresentation(filteredImage) writeToFile:@"/Users/Luis/histogram.png" atomically:YES];

    return nil;
}

The problem is that histogram.png is resulting in just a black line for every input image.

问题是histogram.png导致每个输入图像只有一条黑线。

So, how will be the correct code to generate an histogram UIImage using iOS GPUImage?

那么,如何使用iOS GPUImage生成直方图UIImage的正确代码呢?

1 个解决方案

#1


6  

You can look at the FilterShowcase example to see how this is supposed to work in practice.

您可以查看FilterShowcase示例,了解它在实践中应该如何工作。

The GPUImageHistogramFilter takes in an image and outputs a 256x3 image that encodes the histogram (it's 3 pixels tall because a 1 pixel height isn't allowed in framebuffer construction). The R, G, and B values are stored in their respective color channels within a central 1-pixel-tall stripe at the center of that image.

GPUImageHistogramFilter接收图像并输出256x3图像,该图像对直方图进行编码(它的高度为3像素,因为帧缓冲结构中不允许1像素高度)。 R,G和B值存储在它们各自的颜色通道中,位于该图像中心的中心1像素高条纹内。

To visualize this, you'll need to use a GPUImageHistogramGenerator, and feed the GPUImageHistogramFilter's output into that. The GPUImageHistogramGenerator creates a visual representation of the histogram input as an image. You do need to use -forceProcessingAtSize: to set the size for the GPUImageHistogramGenerator's output image, because it doesn't have a set size by default.

要想象这一点,您需要使用GPUImageHistogramGenerator,并将GPUImageHistogramFilter的输出提供给它。 GPUImageHistogramGenerator创建直方图输入的可视化表示作为图像。您需要使用-forceProcessingAtSize:来设置GPUImageHistogramGenerator输出图像的大小,因为默认情况下它没有设置大小。

One other caution is that you'll need to have a dummy filter of some kind between your input image and the GPUImageHistogramFilter. GPUImageHistogramFilter currently relies on glReadPixels() and that only works for rendered content, not directly uploaded images or video frames.

另一个警告是,您需要在输入图像和GPUImageHistogramFilter之间使用某种虚拟过滤器。 GPUImageHistogramFilter当前依赖于glReadPixels(),它仅适用于渲染内容,而不适用于直接上传的图像或视频帧。

The code used in the FilterShowcase for this is as follows:

FilterShowcase中使用的代码如下:

        filter = [[GPUImageHistogramFilter alloc] initWithHistogramType:kGPUImageHistogramRGB];

        GPUImageGammaFilter *gammaFilter = [[GPUImageGammaFilter alloc] init];
        [videoCamera addTarget:gammaFilter];
        [gammaFilter addTarget:filter];

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

        [histogramGraph forceProcessingAtSize:CGSizeMake(256.0, 330.0)];
        [filter addTarget:histogramGraph];

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

        [videoCamera addTarget:blendFilter];
        [histogramGraph addTarget:blendFilter];

        [blendFilter addTarget:filterView];

This overlays the generated histogram visualization on top of the incoming camera video.

这将生成的直方图可视化叠加在传入的摄像机视频之上。

#1


6  

You can look at the FilterShowcase example to see how this is supposed to work in practice.

您可以查看FilterShowcase示例,了解它在实践中应该如何工作。

The GPUImageHistogramFilter takes in an image and outputs a 256x3 image that encodes the histogram (it's 3 pixels tall because a 1 pixel height isn't allowed in framebuffer construction). The R, G, and B values are stored in their respective color channels within a central 1-pixel-tall stripe at the center of that image.

GPUImageHistogramFilter接收图像并输出256x3图像,该图像对直方图进行编码(它的高度为3像素,因为帧缓冲结构中不允许1像素高度)。 R,G和B值存储在它们各自的颜色通道中,位于该图像中心的中心1像素高条纹内。

To visualize this, you'll need to use a GPUImageHistogramGenerator, and feed the GPUImageHistogramFilter's output into that. The GPUImageHistogramGenerator creates a visual representation of the histogram input as an image. You do need to use -forceProcessingAtSize: to set the size for the GPUImageHistogramGenerator's output image, because it doesn't have a set size by default.

要想象这一点,您需要使用GPUImageHistogramGenerator,并将GPUImageHistogramFilter的输出提供给它。 GPUImageHistogramGenerator创建直方图输入的可视化表示作为图像。您需要使用-forceProcessingAtSize:来设置GPUImageHistogramGenerator输出图像的大小,因为默认情况下它没有设置大小。

One other caution is that you'll need to have a dummy filter of some kind between your input image and the GPUImageHistogramFilter. GPUImageHistogramFilter currently relies on glReadPixels() and that only works for rendered content, not directly uploaded images or video frames.

另一个警告是,您需要在输入图像和GPUImageHistogramFilter之间使用某种虚拟过滤器。 GPUImageHistogramFilter当前依赖于glReadPixels(),它仅适用于渲染内容,而不适用于直接上传的图像或视频帧。

The code used in the FilterShowcase for this is as follows:

FilterShowcase中使用的代码如下:

        filter = [[GPUImageHistogramFilter alloc] initWithHistogramType:kGPUImageHistogramRGB];

        GPUImageGammaFilter *gammaFilter = [[GPUImageGammaFilter alloc] init];
        [videoCamera addTarget:gammaFilter];
        [gammaFilter addTarget:filter];

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

        [histogramGraph forceProcessingAtSize:CGSizeMake(256.0, 330.0)];
        [filter addTarget:histogramGraph];

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

        [videoCamera addTarget:blendFilter];
        [histogramGraph addTarget:blendFilter];

        [blendFilter addTarget:filterView];

This overlays the generated histogram visualization on top of the incoming camera video.

这将生成的直方图可视化叠加在传入的摄像机视频之上。