How to simplify [ 1 0 0] to red?

조회 수: 3(최근 30일)
Conner Carriere
Conner Carriere 2021년 2월 10일
댓글: Walter Roberson 2021년 2월 10일
This is my code that checks which color I have and converts it to matlab syntax
%{
Red = [0 0 0; 255 20 20]; should be complete
Orange = [240 50 0; 255 146 100]; should be complete
Blue = [0 0 0; 20 20 255]; should be complete
Green = [0 200 60; 46 255 150]; should be complete
White = [190 200 200; 255 255 255]; should be complete
Yellow = [215 230 0; 255 255 34]; should be complete
%}
function ColorIdent = ColorID(h)
for w = 1: size(h,1)
if [(h(w,1) <= 255),(h(w,2) <= 20),(h(w,3) <= 20)] %Red limits
ColorIdent(w,:) = [1 0 0]; %Red
elseif [(h(w,1) >= 240),((h(w,2) >= 50) && (h(w,2) <= 146)),(h(w,3) <= 100)] %Orange limits
ColorIdent(w,:) = [1 0 1]; %Orange but had to use magenta
elseif [(h(w,1) <= 20),(h(w,2) <= 20),(h(w,3) <= 255)&&(h(w,3) >= 0)] %Blue limits
ColorIdent(w,:) = [0 0 1]; %Blue
elseif [(h(w,1) <= 46),(h(w,2) >= 200),(h(w,3) <= 150)&&(h(w,3) >= 60)] %Green limits
ColorIdent(w,:) = [0 1 0]; %Green
elseif [(h(w,1) >= 190),(h(w,2) >= 200),(h(w,3) >= 200)] %White limits
ColorIdent(w,:) = [1 1 1]; %White
elseif [(h(w,1) >= 215),(h(w,2) >= 230),(h(w,3) <= 140)] %Yellow limits
ColorIdent(w,:) = [1 1 0]; %Yellow
end
end
end
What I want to do is change the rgb triplet to its respective color
so if [1 0 0] then r
if [1 0 1] then m
if [0 0 1] then b
so on so forth
  댓글 수: 4
Walter Roberson
Walter Roberson 2021년 2월 10일
ColorCodes = 'kbgyrmcw';
k = [0 0 0]; b = [0 0 1]; g = [0 1 0]; y = [0 1 1]; r = [1 0 0]; m = [1 0 1]; c = [1 1 0]; w = [1 1 1];
idx = rgb2ind(reshape(h, [], 1, 3), [k;b;g;y;r;m;c;w]);
ColorIdent = ColorCodes(idx);

댓글을 달려면 로그인하십시오.

답변(0개)

Community Treasure Hunt

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

Start Hunting!

Translated by