基于Bayer 8×8矩阵的JavaScript 4色有序抖动算法问题求助
求助:基于Bayer 8×8矩阵的有序抖动算法偏差排查
我尝试通过基于Bayer 8×8矩阵的有序抖动算法,将8位灰度图像转换为2位/4色图像,当前结果接近预期但仍存在偏差,反复排查未找到问题根源,恳请各位提供技术帮助。
核心代码
// image_array_2d 是存储0-255灰度值的二维数组 // matrix 是归一化到0.0-1.0的二维数组 // LUT 是8位值数组,此处为 [0, 85, 170, 255] const MAX = 255 const image_height = image_array_2d.length const image_width = image_array_2d[0].length for (let y = 0; y < image_height; y++) { for (let x = 0; x < image_width; x++) { const threshold = (matrix[y % 8][x % 8]) * MAX let pixel_value = image_array_2d[y][x]; //pixel_value = gamma_correct(pixel_value) // 查找LUT中与当前像素值最接近的元素 let value_current, value_prev let closest = lut[0] for (let i = 0; i < lut.length; i++) { const closestDifference = Math.abs(closest - pixel_value); const currentDifference = Math.abs(lut[i] - pixel_value); if (currentDifference < closestDifference) { value_prev = lut[i-1] value_current = lut[i] closest = value_current } } let new_value = 0 new_value = pixel_value > threshold ? value_current : value_prev new_value = pixel_value >= MAX ? MAX : new_value image_array_2d[y][x] = new_value } }
归一化前的Bayer矩阵
[ 0, 32, 8, 40, 2, 34, 10, 42], [48, 16, 56, 24, 50, 18, 58, 26], [12, 44, 4, 36, 14, 46, 6, 38], [60, 28, 52, 20, 62, 30, 54, 22], [ 3, 35, 11, 43, 1, 33, 9, 41], [51, 19, 59, 27, 49, 17, 57, 25], [15, 47, 7, 39, 13, 45, 5, 37], [63, 31, 55, 23, 61, 29, 53, 21]
测试对比(均放大400%)
测试原图:

预期目标效果:

实际运行结果(使用LUT数组[0, 85, 170, 255]):

补充说明
核心逻辑在ordered_dither()函数中。
内容的提问来源于stack exchange,提问作者Phil Strahl
相关产品推荐
相关产品推荐

