如何将渐变色图转换为百分比矩阵(将像素颜色转换为基于调色板的百分比)?

How can I transform a graded colored map into a percentage matrix (convert pixel color into a percentage based on a palette)?

我想将 this graded colored map 转换成一个矩阵,其中每个像素都分配了一定百分比的橙色。特别是,使用图像右侧的橙色调色板。

我想将 100% 分配给顶部最亮的橙色,将 0% 分配给底部的黑色。因此,我想根据与调色板相关的单个像素的颜色,为每个像素分配一个百分比。

最后,我的目标是对这些百分比进行平均,以获得橙色的平均百分比。对于后一步,我应该没有任何问题......但是我不知道如何面对前一个问题。

我正在寻找 R、Matlab 或 Mathematica 中的解决方案

这是我的一个想法。假设您有一张任意颜色的图像;让我们通过优化器 运行 解决将颜色从 3 维 space 减少到单一维度的转换。成本函数是从试图通过有损颜色变换恢复原始颜色时的误差导出的。

查看下图我的结果,最亮像素缩放为 100%,最暗像素缩放为 0%。

img = imread('zpiaD.png');
opt = optimset('Display', 'Iter');

itf = fminsearch(@(itf) min_colormap(img, itf) , zeros(1,3), opt);
[~, rimg] = min_colormap(img, itf);

subplot(2,1,1), imshow(rimg), title 'single color dimension'
subplot(2,1,2)
rimg = double(rimg);
imagesc(100 * (rimg - min(min(rimg))) / (max(max(rimg)) - min(min(rimg)))),
colorbar, title 'scaled'

function [res, timg] = min_colormap(img, itf)
  pimg = double(reshape(img, size(img,1)*size(img,2), size(img,3)));
  timg = transpose(itf * pimg');
  res = sum(((timg * itf) - pimg).^2, 'all');
  timg = cast(reshape(timg, size(img,1), size(img,2)), 'uint8');
end