复制 Photoshop 中的“相乘效果”

3
我该如何使用Image Magick库或obj-c代码在iPhone上复制Photoshop的“Multiply effects”?您在哪里可以找到一些示例代码呢?我还看到了这个问题
3个回答

8
如果您想要一种简单的方法来实现这个功能,我的GPUImage框架有一个GPUImageMultiplyBlendFilter,它接收两张图片并对每个像素进行红色、绿色、蓝色和透明度通道的乘法运算。它以GPU加速方式执行,因此比在CPU上执行相同操作快4-6倍。
使用此功能,请设置要混合的两个图像:
UIImage *inputImage1 = [UIImage imageNamed:@"image1.jpg"];    
GPUImagePicture *stillImageSource1 = [[GPUImagePicture alloc] initWithImage:inputImage1];

UIImage *inputImage2 = [UIImage imageNamed:@"image2.jpg"];    
GPUImagePicture *stillImageSource2 = [[GPUImagePicture alloc] initWithImage:inputImage2];

然后创建并配置您的混合滤镜:

GPUImageMultiplyBlendFilter *blendFilter = [[GPUImageMultiplyBlendFilter alloc] init];
[inputImage1 processImage];
[inputImage1 addTarget:blendFilter];
[inputImage2 addTarget:blendFilter];
[inputImage2 processImage];

最后提取混合图像的结果:
UIImage *filteredImage = [blendFilter imageFromCurrentlyProcessedOutput];

当前该实现存在一个缺陷,即早于iPad 2的设备具有有限的纹理尺寸,因此大于2048x2048的图像目前无法在这些老设备上处理。我正在努力解决这个问题。


Brad Larson回答得很好!:)但是你能否也使用库来应用色调? - janusfidel
1
@janusfidel - 改变图像的颜色内容有几种不同的方法,从调整曝光、增益或亮度,到移动其中一个颜色分量的值并应用颜色矩阵。后者在某些情况下用于对图像执行棕褐色调整。除此之外,只需少量类似C代码即可编写新的滤镜,以实现您想要的效果。 - Brad Larson
@BradLarson:我正在做与上面相同的事情,但是我无法找到imageFromCurrentlyProcessedOutput方法来处理我的混合滤镜。有什么问题吗? - Devang
@Devang - 这个方法最近已经被替换了。请阅读此链接的最后一节,了解您现在需要做什么:http://www.sunsetlakesoftware.com/2014/03/17/switching-gpuimage-use-cached-framebuffers - Brad Larson

4
Multiply是一种(Adobe称之为)混合模式。混合模式本质上是使用某些数学公式的像素操作。您可以将两个图像混合在一起,也可以使用一个图像进行“自身混合”。
这可以通过逐像素对图像进行操作来实现,通过获取特定像素的每个通道值并进行处理。
不幸的是,我不熟悉Magick库。但是,这里有一个公式,给定一个通道值(红色、绿色或蓝色,0-255),将返回乘法运算的结果值。 unsigned char result = a * b / 255;
请注意,a和b也必须是无符号字符,否则可能会发生溢出,因为结果将大于一个字节。这是基本的乘法公式,您可以适应变量以支持每通道16位,方法是分配更大的变量大小并适当修改除数。

0

我重用了Brad Larson的代码,它对我很有效。

UIImage *inputImage1 = [UIImage imageNamed:@"image1.jpg"];
GPUImagePicture *stillImageSource1 = [[GPUImagePicture alloc] initWithImage:inputImage1];

UIImage *inputImage2 = [UIImage imageNamed:@"sample.jpg"];
GPUImagePicture *stillImageSource2 = [[GPUImagePicture alloc] initWithImage:inputImage2];

GPUImageMultiplyBlendFilter *blendFilter = [[GPUImageMultiplyBlendFilter alloc] init];
[stillImageSource1 processImage];
[stillImageSource1 addTarget:blendFilter];
[stillImageSource2 addTarget:blendFilter];
[stillImageSource2 processImage];

[blendFilter useNextFrameForImageCapture];

UIImage *filteredImage = [blendFilter imageFromCurrentFramebuffer];

[self.imageView setImage:filteredImage];

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接