How to extract RGB
5 次查看(过去 30 天)
显示 更早的评论
I'm supposed to "extract rgb" values from 5 points from an image and I am very confused on how to do it. I am then supposed to get each of those RGB and convert it to HSI so that I am supposed to have 5 HSI. It sounds like I have to create 5 rgb variables, right? If so, what is the function I need to use?
2 个评论
Geoff Hayes
2018-1-24
Davidson - is your image three dimensional (and so has the red, green, and blue components)? Are you unsure how to extract the red component for one of these five points?
回答(2 个)
RobF
2018-1-24
Extraction of RGB values:
C = imread('ngc6543a.jpg'); % Sample image file
% Coordinates of point/pixel P1: x1, y1
x1 = 354; % Sample coordinates
y1 = 284; % Sample coordinates
% Extract values for red (R), green (G) and blue (B):
R1 = C(x1, y1, 1) % Red is first layer
G1 = C(x1, y1, 2) % Green is second layer
B1 = C(x1, y1, 1) % Blue is third layer
Information on RGB to HSI conversion:
DGM
2022-12-11
I'm just going to throw this down here. MATLAB has no native HSI conversion tools. It has HSV tools, but if you actually want HSI, you'll have to find third-party conversion tools. @RobF linked to the tools from MIMT. I'll continue on the assumption that MIMT is available.
This will collect up to 5 points (discarding any invalid points). Results will be returned as color tables in RGB and HSI as instructed. For sake of comparison, I'm also converting a copy to LAB as well.
% load an image
inpict = imread('peppers.png');
% input must be RGB
% MIMT imsize() will return fixed-length vector output
% so we can know that sz(3) will always exist, and that it will be
% correct even if there are trailing non-singleton dimensions
sz = imsize(inpict,3); % size of first three dims
if sz(3)~=3
error('input must be RGB')
end
% display it and collect 5 points
imshow(inpict);
[x y] = ginput(5);
% round to nearest pixel
x = round(x);
y = round(y);
% discard points which lie outside image area
badpts = x<1 | x>sz(2) | y<1 | y>sz(1);
% generate index list
x = repmat(x(~badpts),[1 sz(3)]);
y = repmat(y(~badpts),[1 sz(3)]);
ch = repmat(1:sz(3),[size(x,1) 1]);
idx = sub2ind(sz,y,x,ch);
% selected points as a Px3 color table of unit-scale RGB tuples
CTrgb = im2double(inpict(idx))
CTrgb =
0.2627 0.1451 0.2706
0.2706 0.1608 0.2902
0.2784 0.1569 0.2902
0.3020 0.1686 0.2902
0.2745 0.1608 0.2863
% selected points as a Px3 color table of LAB tuples
% native conversion tools accept color tuples/tables directly
CTlab = rgb2lab(CTrgb)
CTlab =
19.8274 20.2164 -14.2700
21.3782 19.5417 -15.2412
21.4010 20.9113 -15.1841
22.9996 21.2279 -12.6460
21.4656 19.6757 -14.4189
% selected points as a Px3 color table of HSI tuples
% MIMT conversion tools generally don't accept color tables due to the ambiguity
% so permute the CT so that it can be processed explicitly as a Px1x3 image
% MIMT ctflop(mat) is the same as permute(mat,[1 3 2])
% this is an involution, so the second call returns the array to Px3
CThsi = ctflop(rgb2hsi(ctflop(CTrgb)))
CThsi =
296.8021 0.3584 0.2261
291.9196 0.3315 0.2405
295.4294 0.3514 0.2418
304.5706 0.3351 0.2536
295.1311 0.3315 0.2405
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Convert Image Type 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!