How to extract RGB

2 views (last 30 days)
Davidson Bock
Davidson Bock on 24 Jan 2018
Answered: DGM on 11 Dec 2022
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 Comments
Geoff Hayes
Geoff Hayes on 24 Jan 2018
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?
Davidson Bock
Davidson Bock on 24 Jan 2018
Edited: Davidson Bock on 24 Jan 2018
its a simple 2d image. Here are part of the instructions ask
Get 5 user clicks. Use ginput() function.
Make sure to display image before calling ginput() to see the image to click on.
Extract RGB value for each user click
Convert each RGB to HSI. → so you should have 5 hue values
I already did the 1st 2 parts of this piece of the instructions. This is my code so far.
f = imread ('peppers_preview.jpeg.jpg');
imshow(f)
[x,y] = ginput (5);
for i = 1:5
red = f(int16(y(i)), int16(x(i)), 1)
green = f(int16(y(i)), int16(x(i)), 2)
blue = f(int16(y(i)), int16(x(i)), 3)
end

Sign in to comment.

Answers (2)

RobF
RobF on 24 Jan 2018
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:
  1 Comment
Davidson Bock
Davidson Bock on 24 Jan 2018
Oh, I think I already did that. I have the code of what I did in the comments.

Sign in to comment.


DGM
DGM on 11 Dec 2022
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

Categories

Find more on Convert Image Type in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!