我想使用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);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);更多推荐
发布评论