MATLAB Answers

Creating RGB images with MATLAB

71 views (last 30 days)
Rinita
Rinita on 25 Jan 2020 at 6:50
Edited: Rinita on 26 Jan 2020 at 22:50
Hello, I have the following problem to solve and I am quite new to working with image colors on MATLAB. I cannot figure out how to code the color scalings defined.
Create an RGB image that consists of four horizontal stripes. The first stripe will be green at the left image border to black at the right border. the second will be blue at the left image border to white at the right border. The third will be cyan at the left image border to red at the right borderr. The fourth will be yellow at the left image border to magenta at the right border.

  0 Comments

Sign in to comment.

Accepted Answer

Stephen Cobeldick
Stephen Cobeldick on 25 Jan 2020 at 7:46
Edited: Stephen Cobeldick on 25 Jan 2020 at 7:48
% 1. green at the left image border to black at the right border
% 2. blue at the left image border to white at the right border
% 3. cyan at the left image border to red at the right border.
% 4. yellow at the left image border to magenta at the right border.
A = cat(3,[0,1,0;0,0,0],[0,0,1;1,1,1],[0,1,1;1,0,0],[1,1,0;1,0,1]);
C = 64; % columns in image.
B = interp1([0,1],A,linspace(0,1,C));
image(permute(B,[3,1,2]))
Giving:
temp2.png

  3 Comments

Rinita
Rinita on 25 Jan 2020 at 19:05
Thank you so much! Could you please explain how you put the inputs for interp1 and permute functions? I am a little confused.
Stephen Cobeldick
Stephen Cobeldick on 25 Jan 2020 at 23:43
Different steps in this code arrange the image dimensions in two different ways, and knowing why is critical to understanding this code.
When defining the array A the RGB values are arranged so that each color is simply defined by its standard RGB triplet, e.g. [0,1,0] for green, so this determines that the 2nd dimension must correspond to the RGB. Then we read the interp1 documentation, which states that we can interpolate multiple data sets at once when "Each column of array v contains a different set of 1-D sample values", which determines that the 1st dimension must therefore correspond to the start- and end-colors (1st and 2nd rows respectively). This leaves the 3rd dimension (or any higher dimension of your choice) to correspond to the four stripes themselves, and that is exactly what cat does for us: it joins four matrices (one for each stripe) along the 3rd dimension, where each matrix has two rows (the start and end colors).
So the output of cat is an array with this size (I also marked what each dimension corresponds to):
A(2 <start & end colors>, 3 <RGB>, 4 <stripes>)
Then we simply tell interp1 to resample those colors (using the default linear interpolation, which exactly matches your requirement) using as many points as you want (I used 64). Remember that each column of the array contains a different set of values, so the output is an array like this:
B(64 <interpolated colors>, 3 <RGB>, 4 <stripes>)
Now we have our final colors, but the dimensions are incorrect for displaying an RGB image:
Following the definition of an RGB image, we know that the dimensions should be arranged like this to display those colors as four horizontal stripes of 64 colors:
(4 <stripes>, 64 <interpolated colors>, 3 <RGB>)
and that is exactly that permute does: its second input [3,1,2] tells MATLAB to rearrange the array like that.
Rinita
Rinita on 26 Jan 2020 at 17:15
Thank you very much for this explanation. The problem is cleared up now :)

Sign in to comment.

More Answers (0)

Sign in to answer this question.


Translated by