改进图像主颜色占比检测算法:解决复杂图像黑白误判问题
优化颜色占比检测中的黑白误判问题
我需要检测8种颜色在图像中的占比,包括3种RGB基础色(红、绿、蓝)、4种CMYK基础色(青、粉、黄、黑)以及白色。
当前实现方案
目标颜色列表
const colors = [ {r: 255, g: 0, b: 0, name: 'red'}, {r: 0, g: 255, b: 0, name: 'green'}, {r: 0, g: 0, b: 255, name: 'blue'}, {r: 0, g: 255, b: 255, name: 'cyan'}, {r: 255, g: 0, b: 255, name: 'pink'}, {r: 255, g: 255, b: 0, name: 'yellow'}, {r: 0, g: 0, b: 0, name: 'black'}, {r: 255, g: 255, b: 255, name: 'white'} ]
颜色匹配逻辑
通过计算像素颜色与目标颜色的曼哈顿距离判定最近匹配色:
const colorNumber = (color, pixel_rgba) => { const number = Math.abs(color.r - pixel_rgba.r) + Math.abs(color.g - pixel_rgba.g) + Math.abs(color.b - pixel_rgba.b) return number }
存在的问题
该方案在文档、色彩区分度高的简单图像上表现正常,但在复杂图像中会误判出过多的黑色与白色。
尝试过的优化(效果不理想)
曾尝试给黑白的差异值乘以2,以此降低它们的匹配优先级,但这种方法不够理想:
const colorNumber = (color, pixel_rgba) => { const number = Math.abs(color.r - pixel_rgba.r) + Math.abs(color.g - pixel_rgba.g) + Math.abs(color.b - pixel_rgba.b) return color.name === "black" || color.name === "white" ? number * 2 : number }
寻求建议
希望优化最近色判定逻辑,使其更符合人眼感知,减少黑白的误判情况。
内容的提问来源于stack exchange,提问作者khramov
相关产品推荐
相关产品推荐

