How to adjust brightness and contrast of an image by GPUImage?

╄→гoц情女王★ 提交于 2019-12-24 02:01:44

问题


I wrote a method to filter an image with brightness factor and contrast factor as following:

- (UIImage*)image:(UIImage*)image withBrightness:(float)brightness contrast:(float)contrast{
    GPUImagePicture *imagePicture = [[GPUImagePicture alloc] initWithImage:image];
    GPUImageBrightnessFilter *brightnessFilter = [[GPUImageBrightnessFilter alloc] init];
    GPUImageContrastFilter *contrastFilter = [[GPUImageContrastFilter alloc] init];

    [imagePicture addTarget:brightnessFilter];
    [brightnessFilter addTarget:contrastFilter];

    brightnessFilter.brightness = brightness; // set brightness
    contrastFilter.contrast = contrast;       // set contrast

    [imagePicture processImage];
    UIImage *filteredImage = [contrastFilter imageFromCurrentlyProcessedOutput];
    return filteredImage;
}

Invoke this implementation would raise an error: <Error>: CGContextDrawImage: invalid context 0x0.

How to correctly implement brightness and contrast adjustment using GPUImage?


回答1:


You Probably should create a group filter,Check this sample code

    GPUImageFilterGroup *groupFilter = [[GPUImageFilterGroup alloc]init];
    [groupFilter addTarget:edit.currentFilter];

    //create a brightness filter
    GPUImageBrightnessFilter *brightnessFilter = [[GPUImageBrightnessFilter alloc]init];
    [brightnessFilter setBrightness:brightnessValue];

    //create a contrast filter
    GPUImageContrastFilter *contrastFilter = [[GPUImageContrastFilter alloc]init];
    [contrastFilter setContrast:contrastValue];

    //create a exposure filter
    GPUImageExposureFilter *exposureFilter =[[GPUImageExposureFilter alloc]init];
    [exposureFilter setExposure:exposureValue];

    //add brightness,contrast,exposure filters to group filter
    [groupFilter addFilter:brightnessFilter];
    [groupFilter addFilter:contrastFilter];
    [groupFilter addFilter:exposureFilter];


    [edit.currentFilter addTarget:brightnessFilter];
    [brightnessFilter addTarget: contrastFilter];
    [contrastFilter addTarget: exposureFilter];
    //setting initial and terminal filters
    [(GPUImageFilterGroup *) groupFilter setInitialFilters:[NSArray arrayWithObject:edit.currentFilter]];
    [(GPUImageFilterGroup *) groupFilter setTerminalFilter:exposureFilter];


    //apply it to the GPUImagePicture


来源:https://stackoverflow.com/questions/19134112/how-to-adjust-brightness-and-contrast-of-an-image-by-gpuimage

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!