RGB Values

I am currently detecting RGB values using the impixel() command. Is there a function in MatLab which states the name of the color given the RGB value? For instance, is there something that will say [r g b] = [100 100 100] = 'Dark Gray' or similar?

回答(1 个)

Sven
Sven 2011-10-28
Your best bet would be to create a lookup from a source such as: http://en.wikipedia.org/wiki/List_of_colors.
Here's my take on it:
% Read from a great source and split it into table rows
str = urlread('http://en.wikipedia.org/wiki/List_of_colors');
trCells = regexp(str,'<tr>\n','split');
% Find rows matching pattern of "Xanadu | #738678 | 45% | 53% | 47%"
expressn = ['<th.*?>(?<colName>.*?)</th>\s*'...
'<td.*?>#\w{6}</td>\s*'...
'<td.*?>(?<pcntR>\d+)%</td>\s*'...
'<td.*?>(?<pcntG>\d+)%</td>\s*'...
'<td.*?>(?<pcntB>\d+)%</td>\s*'...
];
matchedCells = regexp(trCells, expressn,'tokens','once');
% Collate into names and rgb value pairs
allMatches = cat(1,matchedCells{:});
colNames = allMatches(:,1);
colVals = cellfun(@str2double, allMatches(:,2:4))/100;
% QUERY!
testRGB = [.4 .2 .7];
colDiffs = bsxfun(@minus, colVals, testRGB);
colDists = sqrt(sum(colDiffs.^2,2));
[minDist, minIdx] = min(colDists);
% REPORT!
fprintf('[%g %g %g] is closest to [%g %g %g] (%s)\n', testRGB, colVals(minIdx,:), colNames{minIdx})
[0.4 0.2 0.7] is closest to [0.41 0.21 0.61] (Purple Heart)

1 个评论

Sven
Sven 2011-10-28
Oh, and it looks like there's already a file exchange entry that does a similar thing:
http://www.mathworks.com/matlabcentral/fileexchange/24497-rgb-triple-of-color-name-version-2

请先登录,再进行评论。

类别

在 帮助中心 和 File Exchange 中查找有关 Image Processing and Computer Vision 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by