快速图像处理过滤器
Posted
技术标签:
【中文标题】快速图像处理过滤器【英文标题】:Swift image processing filters 【发布时间】:2015-12-20 19:26:08 【问题描述】:我的 swift 代码中的一个过滤器有问题:luminosityModifier,其他用于红色、绿色、蓝色和 alpha 的过滤器可以正常工作。如果我使用 0 作为 luminosityModifier 值,则代码运行时不会出错,但如果我尝试任何其他整数,我会看到错误: 执行被中断,原因:EXC_BAD_INSTRUCTION(代码 = EXC_I386_INVOP,子代码 = 0x0)。这是这段代码的最后一行。
import UIKit
let image = UIImage(named: "sample.png")!
// Process the image!
// The Filter class contains the definition of a simple RGBA variable
class Filter
var rgba = [UInt8](count:5, repeatedValue: 0)
// Here are the 5 filters that can later be selected. By modifying the RGBA values, I intened to serve the requirement to create modifiers to individual filters.
let redFilter: Filter = Filter()
redFilter.rgba[0] = 255
let greenFilter: Filter = Filter()
greenFilter.rgba[1] = 45
let blueFilter: Filter = Filter()
blueFilter.rgba[2] = 255
let alphaFilter: Filter = Filter()
alphaFilter.rgba[3] = 50
// Set luminosity Modifier in desired percentage (%), should be <100 to avoid explosion due to internal rounding
let luminosityModifier = Filter()
luminosityModifier.rgba[4] = 20
class ImageProcessor
var filterSequenceList: [String] = []
//This Dictionary is later used to accept strings to set the filter the processor will apply.
var filtersAvailable: [String: Filter] = [
"redFilter": redFilter,
"greenFilter": greenFilter,
"blueFilter": blueFilter,
"alphaFilter": alphaFilter,
"luminosityModifier": luminosityModifier
]
func addFilterToSequence(filterName: String)
filterSequenceList.append(filterName)
func applyFilters(image: UIImage) -> UIImage
var filters: [Filter] = []
// A list if filters gets populated according to the array of strings entered.
for name in filterSequenceList
filters.append(filtersAvailable[name]!)
let rgbaImage = RGBAImage(image: image)!
// Loop through each pixel
for y in 0..<rgbaImage.height
for x in 0..<rgbaImage.width
let index = y * rgbaImage.width + x
var pixel = rgbaImage.pixels[index]
// Loop through each filter
for filter in filters
for value in 0...4
// RGBA values with value 0 get ignored. This means that 1 needs to be used if you wand that specific value to be very low.
if(filter.rgba[value] != 0 )
switch value
case 0:
pixel.red = filter.rgba[value]
rgbaImage.pixels[index] = pixel
case 1:
pixel.green = filter.rgba[value]
rgbaImage.pixels[index] = pixel
case 2:
pixel.blue = filter.rgba[value]
rgbaImage.pixels[index] = pixel
case 3:
pixel.alpha = filter.rgba[value]
rgbaImage.pixels[index] = pixel
case 4:
let red = pixel.red
let green = pixel.green
let blue = pixel.blue
let luminosityModifier = Double(filter.rgba[value])
let relativeluminosity = Double(red) * 0.2126 + Double(green) * 0.7152 + Double(blue) * 0.0722
let transformerRed = (relativeluminosity - Double(green) * 0.7152 - Double(blue) * 0.0722) / 0.2126
let transformerGreen = (relativeluminosity - Double(red) * 0.2126 - Double(blue) * 0.0722 ) / 0.7152
let transformerBlue = (relativeluminosity - Double(red) * 0.2126 - Double(green) * 0.7152 ) / 0.0722
pixel.red = UInt8(transformerRed * luminosityModifier / 100 )
pixel.green = UInt8(transformerGreen * luminosityModifier / 100)
pixel.blue = UInt8(transformerBlue * luminosityModifier / 100)
rgbaImage.pixels[index] = pixel
default:
print("No image changes")
let newImage = rgbaImage.toUIImage()!
return newImage
//--------------------------------------- Start using the class here--------------------------------
var processor: ImageProcessor = ImageProcessor()
// Use the addFilterToSequence function and pass in one of the strings mentioned below. Using a non-existing filter name will cause a runtime error
// "redFilter"
// "greenFilter"
// "blueFilter"
// "alphaFilter"
// "luminosityModifier"
//processor.addFilterToSequence("redFilter")
//processor.addFilterToSequence("blueFilter")
processor.addFilterToSequence("luminosityModifier")
//processor.addFilterToSequence("greenFilter")
//processor.addFilterToSequence("alphaFilter")
processor.filterSequenceList
processor.applyFilters(image)
【问题讨论】:
【参考方案1】:rgba[4]
的索引 (4) 超出范围。只有四个元素(从零开始),第四个元素 (alpha) 位于索引 3。
至于过滤器本身,我认为您的意思是使用 CILuminosityBlendMode() (并放弃尝试完全设置其 rgba 的崩溃线)。它没有其他参数;它只是链末端的一个简单的图像输入图像输出补丁。除非您打算通过其 alpha 通道弱应用它(不确定这是否可行;尝试看看),否则只需将索引 4 更改为 3,以便您在 rgba
数组中设置 alpha 元素。
【讨论】:
谢谢约书亚!这正是我所需要的。我对你投了赞成票,但我没有足够的声望点来注册。另外,很抱歉我的回复延迟。我一直在放假。以上是关于快速图像处理过滤器的主要内容,如果未能解决你的问题,请参考以下文章
(34)java Spring Cloud+Spring boot+mybatis企业快速开发架构之SpringCloud-Zuul过滤器介绍及使用(传递数据拦截请求和异常处理)