Steve on Image Processing and MATLAB

Concepts, algorithms & MATLAB

Using reshape to rearrange image color components 4

Posted by Steve Eddins,

I recently saw some code that transformed the RGB pixel values of an image into a Px3 matrix, such that each row contained the red, green, and blue color components of a single pixel. Today I want to show that code fragment, explain it, and then demonstrate what I think is the fastest possible to perform that transformation in MATLAB.

I tend to use the peppers sample image a lot, so I'll switch things up and use NGC6543 instead:

RGB = imread('ngc6543a.jpg');

Historical note: This was the first "truecolor" sample image to ship with MATLAB.

RGB is a three-dimensional array:

ans =

   650   600     3

The third dimension, which has length three, contains the red, green, and blue component images. Compare, for example, the red and green component images:


Here is the code that I saw for transforming this array into a Px3 matrix, where each row contains the three color component values of one pixel:

red = RGB(:,:,1);
green = RGB(:,:,2);
blue = RGB(:,:,3);

A = [red(:) green(:) blue(:)];

The first five pixels are along the uppermost left edge of the image, and they are all black:

ans =

  5×3 uint8 matrix

   0   0   0
   0   0   0
   0   0   0
   0   0   0
   0   0   0

The first three lines of code did not give me pause:

red = RGB(:,:,1);
green = RGB(:,:,2);
blue = RGB(:,:,3);

I have written code just like that approximately 2.7183 bazillion times. It was the next line that made me stop and scratch my head for a moment:

A = [red(:) green(:) blue(:)];

Hmm. The array is being split apart into three components, which are then immediately put back together into a matrix. That suggests ... reshape, maybe?

Yes, as it turns out, this can all be done with a call to reshape. And, there is a big advantage to doing so, as I'll explain in a moment. First, the new code:

B = reshape(RGB,[],3);

That line of code says, "MATLAB, reshape the array RGB so that it is Px3, and will you please just go ahead and figure out what P should be to make it work?"

You see in a call to reshape, the number of elements must not change. Here are the number of elements of the array RGB:

ans =


To reshape that successfully into a matrix with 3 columns, we can compute how many rows there must be:

P = numel(RGB) / 3
P =


The empty matrix as the second argument to reshape signals to MATLAB to compute that size automatically. That works out just fine:

ans =

      390000           3

So, each column of B has 390,000 elements. The first column of B contains the first 390,000 elements (in memory order) of RGB. The second column of B contains the second 390,000 elements of RGB, and similarly for the third column. Because of the way MATLAB arrangements elements in memory, these columns are exactly the elements from the red component, the green component, and the blue component of RGB.

The big advantage of doing it this way is that MATLAB makes no memory copy of data when calling reshape. Because of this optimization, calls to reshape are almost instantaneous.

Let reshape be your friend.

Get the MATLAB code

Published with MATLAB® R2019a

4 CommentsOldest to Newest

Bjorn Gustavsson replied on : 1 of 4
The irony that this false-colour-image was the first "truecolor" RGB-image used by matlab should not be missed! Already when produced htere must have been a few digital rgb-images around... In this the red channel was from H-alpha at 656 nm, the green channel N+ at 658 nm and the blue channel from O at 630 nm - all definitely in the red part of the spectrum.
Steve Eddins replied on : 2 of 4
Bjorn—Not many, and most that I could find had unclear copyright status or other permissions issues.
Bjorn Gustavsson replied on : 3 of 4
Steve, I don't know if this should make me feel too old or too young - I do feel less than intelligent. We used science-grade ccds around that time and I "just assumed" that there were a reasonable number of photographer-level cameras around...
Bjorn—It would have been around 1995 when I put this image into the dev build for MATLAB 5. I don't remember for sure, but I believe it was several years later before the Image Processing Toolbox team got a digital camera to experiment with, and it wasn't that good. We didn't have lab-grade hardware lying around MathWorks at that time. :-) I did my graduate work in the late 1980s at Georgia Tech. Our digital signal processing lab had a film scanner, which I could never get to work. Mostly, we used the same couple of dozen 256x256 and 512x512 images that everybody else was using at the time.