我可以从您的问题中收集到的是,您需要以某种方式存储有关单独颜色文本的“信息”。因此,下面的代码可能会解决它,为此我必须实际记下 6 种不同颜色文本中的每一种的颜色。生成的 4D 矩阵存储所有“信息”。请注意,当我们正在寻找完美匹配的颜色时,我们可以通过挤出存储冗余数据的第 3 个维度轻松地将其变为 3D。
代码
%// Read image
img = imread('color_test.bmp');
%%// Create a database of text colors
color1 = [236 3 104]; %%// text1
color2 = [57 228 2]; %%// text2
color3 = [147 190 131]; %%// text3
color4 = [5 107 106]; %%// text4
color5 = [254 223 188]; %%// text5
color6 = [19 98 13]; %%// text6
color = [color1;color2;color3;color4;color5;color6]; %%// all text colors in a Nx3 matrix
%%// Save all the individual colored images in a 4D matrix for later
img_all = false([size(img) size(color,1)]);
for k = 1:size(color,1)
img_all(:,:,:,k) = bsxfun(@eq,img,permute(color(k,:),[1 3 2]));
end
%%// Show the images as black and white
figure,
for k = 1:size(color,1)
subplot(size(color,1),1,k), imshow(uint8(255.*img_all(:,:,:,k)));
end
%%// Show the images in their original colors
figure,
for k = 1:size(color,1)
subplot(size(color,1),1,k), imshow(uint8(bsxfun(@times,img_all(:,:,:,k),permute(color(k,:),[1 3 2]))));
end
输出
让我们知道这是否适合您!