如何在MATLAB中将图像转换为Web安全的颜色映射?(How can I convert an image to a web

编程入门 行业动态 更新时间:2024-10-26 05:16:55
如何在MATLAB中将图像转换为Web安全的颜色映射?(How can I convert an image to a web-safe color map in MATLAB?)

我想使用MATLAB将图像转换为Web安全颜色。 它有预定义的功能吗? 如果没有,我的第一步应该是什么?

I want to convert images to web-safe colors using MATLAB. Is there any predefined function for it? If not, what should be my first step to start off?

最满意答案

Ashish有正确的方法,但您可能会发现将所有这些值从网页中删除并进入可以使用的地图是令人生畏的。 你有几个选择来创建地图......

一种选择是使用函数URLREAD实际获取页面的源代码,并使用函数REGEXP解析所需的数字(“他只是建议用正则表达式解析HTML吗?!”是的,我做了。我能说什么?我是一个孤独的人,Dottie。反叛者。):

mapURL = 'http://en.wikipedia.org/wiki/Web_colors#Web-safe_colors'; urlText = urlread(mapURL); matchExpr = ['<td style="background: #\w{3};">' ... '(?:<u>\*)?(\w{3})(?:\*</u>)?</td>']; colorID = regexp(urlText,matchExpr,'tokens'); colorID = char([colorID{:}]); [~,webSafeMap] = ismember(colorID,'0369CF'); webSafeMap = (webSafeMap-1)./5;

但是,在我完成上述操作之后,我意识到结果Web安全颜色映射值有一个很好的常规结构。 这意味着您实际上可以忽略所有上述混乱并使用函数REPMAT和KRON自己生成映射:

colorValues = (0:0.2:1).'; %' webSafeMap = [repmat(colorValues,36,1) ... kron(colorValues,ones(36,1)) ... repmat(kron(colorValues,ones(6,1)),6,1)];

然后,您可以使用RGB2IND和IND2RGB功能轻松地重新着色RGB图像。 例如:

imageRGB = imread('peppers.png'); %# Load a built-in image imageRGB = ind2rgb(rgb2ind(imageRGB,webSafeMap),webSafeMap); imshow(imageRGB);

pepper.png的网页安全版本

Ashish has the right approach, but you may be finding it daunting to get all those values off of the web page and into a map that you can use. You have a couple of options for creating the map...

One option is to actually get the source for the page using the function URLREAD and parse out the numbers you need using the function REGEXP ("Did he just suggest parsing HTML with a regex?!" Yes, I did. What can I say? I'm a loner, Dottie. A rebel.):

mapURL = 'http://en.wikipedia.org/wiki/Web_colors#Web-safe_colors'; urlText = urlread(mapURL); matchExpr = ['<td style="background: #\w{3};">' ... '(?:<u>\*)?(\w{3})(?:\*</u>)?</td>']; colorID = regexp(urlText,matchExpr,'tokens'); colorID = char([colorID{:}]); [~,webSafeMap] = ismember(colorID,'0369CF'); webSafeMap = (webSafeMap-1)./5;

However, after I did the above I realized that there is a nice regular structure to the resulting web-safe color map values. This means you could actually ignore all the above mess and generate the map yourself using the functions REPMAT and KRON:

colorValues = (0:0.2:1).'; %' webSafeMap = [repmat(colorValues,36,1) ... kron(colorValues,ones(36,1)) ... repmat(kron(colorValues,ones(6,1)),6,1)];

And then you can easily recolor, say, an RGB image using the functions RGB2IND and IND2RGB. For example:

imageRGB = imread('peppers.png'); %# Load a built-in image imageRGB = ind2rgb(rgb2ind(imageRGB,webSafeMap),webSafeMap); imshow(imageRGB);

A web-safe version of peppers.png

更多推荐

本文发布于:2023-07-14 23:43:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1108360.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   中将   图像   颜色   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!