CIFilter 的输出对同一图像的不同尺寸有不同的影响
Output of CIFilter has different effect for different sizes of same image
func getImageWithFilter(by filterType: MainViewModel.FilterType,
image: CIImage) -> CIImage {
guard filterType.filterEnabled,
let filter = CIFilter(name: filterType.rawValue)
else {
return image
}
filter.setValue(image, forKey: kCIInputImageKey)
return filter.outputImage ?? image
}
我有两个输出:
- 用于预览的 AVCaptureVideoDataOutput
- AVCapturePhotoOutput 用于以高分辨率保存照片。
应用过滤器后我得到不同的结果
请帮我解决这个问题。
发生这种情况是因为内置 Core Image 滤镜的大多数参数都以像素为基础运行。在您的情况下,它是 CICrystallize
过滤器的 inputRadius
参数。来自文档:
The radius determines how many pixels are used to create the effect. The larger the radius, the larger the resulting crystals.
这意味着您需要根据输入大小将参数设置为不同的值。
我通常会计算一些与我的基本参数值相乘的因子。例如:
let inputSizeFactor = min(inputImage.size.width, inputImage.size.height) / 1000
let scaledRadius = radius * inputSizeFactor
filter.setValue(scaledRadius, forKey: "inputRadius")
func getImageWithFilter(by filterType: MainViewModel.FilterType,
image: CIImage) -> CIImage {
guard filterType.filterEnabled,
let filter = CIFilter(name: filterType.rawValue)
else {
return image
}
filter.setValue(image, forKey: kCIInputImageKey)
return filter.outputImage ?? image
}
我有两个输出:
- 用于预览的 AVCaptureVideoDataOutput
- AVCapturePhotoOutput 用于以高分辨率保存照片。
应用过滤器后我得到不同的结果
请帮我解决这个问题。
发生这种情况是因为内置 Core Image 滤镜的大多数参数都以像素为基础运行。在您的情况下,它是 CICrystallize
过滤器的 inputRadius
参数。来自文档:
The radius determines how many pixels are used to create the effect. The larger the radius, the larger the resulting crystals.
这意味着您需要根据输入大小将参数设置为不同的值。
我通常会计算一些与我的基本参数值相乘的因子。例如:
let inputSizeFactor = min(inputImage.size.width, inputImage.size.height) / 1000
let scaledRadius = radius * inputSizeFactor
filter.setValue(scaledRadius, forKey: "inputRadius")