× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

Access RGB channels in an image in MATLAB

Ray Phan
Jan 30, 2015
<p>You are visualizing the red channel as a <strong>grayscale</strong> image. Think about it. The image is essentially a <strong>3D matrix</strong>. By doing <code>image(:,:,1);</code>, you are accessing <strong>the first slice</strong> of that image, which is a 2D matrix and this corresponds to the red components of each pixel. <code>imshow</code> functions such that if the input is a 2D matrix, then the output is automatically visualized as grayscale. If <code>imshow</code> is a 3D matrix, then the output is automatically visualized in colour, where the first, second and third slices of the matrix correspond to the red, green and blue components respectively.</p> <p>Therefore, by doing <code>imshow</code> on this 2D matrix, it would obviously be grayscale. You're just interpreting the results incorrectly. Here, the whiter the pixel <strong>the more red the pixel</strong> is in that location of the image. For example, assuming your image is <code>uint8</code> (unsigned 8-bit integer) if a value has 255 at a particular location, this means that the pixel has a fully red component whereas if you had a value of 0 at a particular location, this means that there is no red component. This would be visualized in black and white.</p> <p>If you want to display how red a pixel is, then put this into a 3D matrix where the second (green) and third (blue) channels are all zero, while you set the red channel to be from the first slice of your original image. In other words, try this:</p> <pre><code>imageRed = uint8(zeros(size(image))); %// Create blank image imageRed(:,:,1) = redPlane; %// Set red channel accordingly imshow(imageRed); %// Show this image </code></pre> <p>However, if you just want to process the red channel, then there's no need to visualize it. Just use it straight out of the matrix itself. You said you wanted to look for specific red channel values in your image. Ignoring the green and blue components, you can do something like this. Let's say we want to create an output Boolean map <code>locationMap</code> such that any location that is true / 1 will mean that this is a location has a red value you're looking for, and false / 0 means that it isn't. As such, do something like:</p> <pre><code>redPlane = image(:,:,1); % // Place values of red you want to check here redValuesToCheck = [15 20 100]; %// Initialize a boolean map where true %// means this is a red value we're looking for and %// false otherwise locationMap = false(size(redPlane)); %// For each red value we want to check... for val = redValuesToCheck %// Find those locations that share this %// value, and set to true on the boolean map locationMap(redPlane == val) = true; end %// Show the map imshow(locationMap); </code></pre> <hr> <p>One small subtlety here that you may or may not notice, but I'll bring it up anyway. <code>locationMap</code> is a Boolean variable, and when you use <code>imshow</code> on this, true gets visualized to white while false gets visualized to black.</p> <hr> <h1>Minor note</h1> <p>Using <code>image</code> as a variable name is a very bad idea. <a href="http://www.mathworks.com/help/matlab/ref/image.html"><code>image</code></a> is a pre-defined function already included in MATLAB that takes in a matrix of numbers and visualizes it in a figure. You should use something else instead, as you may have other functions that rely on this function but you won't be able to run them as the functions are expecting the function <code>image</code>, but you have shadowed it over with a variable instead.</p> <p>This tip was originally posted on <a href="http://stackoverflow.com/questions/25165707/Access%20RGB%20channels%20in%20an%20image%20in%20MATLAB/25165787">Stack Overflow</a>.</p>
comments powered by Disqus