How to convert image to web safe color map in MATLAB?

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

+3
source share
2 answers

Ashish has the right approach, but it may be difficult for you to get all of these values ​​from a web page and on a map that you can use. You have several options for creating a map ...

One option is to actually get the source for the page using the URLREAD function and parse the numbers you need using the REGEXP function (“ Did he just parse the HTML with a regular expression ?!” 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 this, I realized that there is a good regular structure for the safe color map values ​​received on the network. This means that you can really ignore all the problems described above and generate the map yourself using the REPMAT and KRON functions :

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

, , RGB, RGB2IND IND2RGB. :

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

A web-safe version of peppers.png

+3

All Articles