You are on page 1of 10

I = imread('exemple.

jpg');

%Red component
R = I(:,:,1);
image(R), colormap([[0:1/255:1]',
zeros(256,1), zeros(256,1)]), colorbar;

%Green Component
G = I(:,:,2);
figure;
image(G),
colormap([zeros(256,1),[0:1/255:1]',
zeros(256,1)]), colorbar;

%Blue component
B = I(:,:,3);
figure;
image(B), colormap([zeros(256,1),
zeros(256,1), [0:1/255:1]']), colorbar;
share|improve this answer edited Nov 29 '11 at 19:44



Chadwick
4,71741447
answered Nov 29 '11 at 19:43



Landrover
211
up vote1down vote
You mean you want to extract red color only? using
im(:,:,1) only seperate the red channel from the 3D
image and convert it to a 2D image. Try this simple
code:
im=imread('example.jpg');
im_red=im(:,:,1);
im_gray=rgb2gray(im);
im_diff=imsubtract(im_red,im_gray);
imshow(im_diff);
share|improve this answer edited Mar 20 '11 at 8:54



snakile
6,873114295
answered Mar 20 '11 at 5:23



Aatish Ali Khan
111
up
vote1down
vote
Try this:
% display one channel only
clear all;

im=imread('images/DSC1228L_512.jpg');
im_red = im;
im_green = im;
im_blue = im;

% Red channel only
im_red(:,:,2) = 0;
im_red(:,:,3) = 0;
figure, imshow(im_red);

% Green channel only
im_green(:,:,1) = 0;
im_green(:,:,3) = 0;
figure, imshow(im_green);

% Blue channel only
im_blue(:,:,1) = 0;
im_blue(:,:,2) = 0;
figure, imshow(im_blue);





New

1
4
So I am trying to create an effect similar to Sin City or other movies where they remove all colors except one
from an image.
I have an RGB image which I want to convert to grayscale but I want to keep one color.
This is my picture:

I want to keep the red color. The rest should be grayscale.
This is what my code outputs so far (you can see that the areas are correct, I don't know why they are white
instead of red though):

Here is my code so far:
filename = 'roses.jpg';

[cdata,map] = imread( filename );
% convert to RGB if it is indexed image
if ~isempty( map )
cdata = idx2rgb( cdata, map );
end

%imtool('roses.jpg');

imWidth = 685;
imHeight = 428;

% RGB ranges of a color we want to keep
redRange = [140 255];
greenRange = [0 40];
blueRange = [0 40];

% RGB values we don't want to convert to grayscale
redToKeep = zeros(imHeight, imWidth);
greenToKeep = zeros(imHeight, imWidth);
blueToKeep = zeros(imHeight, imWidth);

for x=1:imWidth

for y=1:imHeight

red = cdata( y, x, 1 );
green = cdata( y, x, 2 );
blue = cdata( y, x, 3 );

if (red >= redRange(1) && red <= redRange(2) && green >= greenRange(1)
&& green <= greenRange(2) && blue >= blueRange(1) && blue <= blueRange(2))
redToKeep( y, x ) = red;
greenToKeep( y, x ) = green;
blueToKeep( y, x ) = blue;
else
redToKeep( y, x ) = 999;
greenToKeep( y, x ) = 999;
blueToKeep( y, x ) = 999;
end

end

end

im = rgb2gray(cdata);
[X, map] = gray2ind(im);
im = ind2rgb(X, map);

for x=1:imWidth

for y=1:imHeight

if (redToKeep( y, x ) < 999)
im( y, x, 1 ) = 240;
end
if (greenToKeep( y, x ) < 999)
im( y, x, 2 ) = greenToKeep( y, x );
end
if (blueToKeep( y, x ) < 999)
im( y, x, 3 ) = blueToKeep( y, x );
end

end

end

imshow(im);
matlab image-processing colors rgb grayscale
share|improve this question edited Nov 4 '10 at 17:21



gnovice
57.7k477135
asked Oct 31 '10 at 16:33



Richard Knop
9,08520103245


It seems Matlab is providing a solution but it would be interesting to see a code golf of this... gary Nov 1 '10 at 15:13

4 Answers
activeoldest votes
up
vote9d
own
voteaccepted
figure
pic = imread('EcyOd.jpg');

for mm = 1:size(pic,1)
for nn = 1:size(pic,2)
if pic(mm,nn,1) < 80 || pic(mm,nn,2) > 80 || pic(mm,nn,3) > 100
gsc = 0.3*pic(mm,nn,1) + 0.59*pic(mm,nn,2) + 0.11*pic(mm,nn,3);
pic(mm,nn,:) = [gsc gsc gsc];
end
end
end
imshow(pic)

Conversion

0dow
n
votefavorite
I am trying to write a function in Matlab that takes an RGB image of class unit8 and double and converts
it to a YCBCR image. The transformation formula is below.

I would be really thankful for any help of any kind.
matlab image-processing image-conversion
share|improve this queston asked Jun 10 '11 at 19:57



biz
264417



Have a look at this page, here you will fnd all informaton needed regarding yuv, rgb and the conversion between them;
including sample code. Fredrik Pihl Jun 10 '11 at 20:23

1 Answer
activeoldest votes
up
vote2do
wn voteaccepted
There's an Image Processing Toolbox function for that, if you have access to it: RGB2YCBCR
If you don't have access to it, here's how you can do the conversion yourself:
rgbImage = imread('peppers.png'); %# A sample RGB image
A = [65.481 -37.797 112; ... %# A 3-by-3 matrix of scale factors
128.553 -74.203 -93.786; ...
24.966 112 -18.214];

%# First convert the RGB image to double precision, scale its values to
the
%# range 0 to 1, reshape it to an N-by-3 matrix, and multiply by A:
ycbcrImage = reshape(double(rgbImage)./255,[],3)*A;

%# Shift each color plane (stored in each column of the N-by-3 matrix):
ycbcrImage(:,1) = ycbcrImage(:,1)+16;
ycbcrImage(:,2) = ycbcrImage(:,2)+128;
ycbcrImage(:,3) = ycbcrImage(:,3)+128;

%# Convert back to type uint8 and reshape to its original size:
ycbcrImage = reshape(uint8(ycbcrImage),size(rgbImage));
And here's the image you get when you display ycbcrImage:

share|improve this answer edited Jun 10 '11 at 20:27

answered Jun 10 '11 at 20:00



gnovice
57.7k477135



I need to implement it though. biz Jun 10 '11 at 20:15

@biz: gnovice has provided an implementaton based on your transformaton formula Jacob Jun 10 '11 at 20:23

THANKS A LOT for your answer, if you don't mind can you just give a quick explainaton to the two reshapes ?
biz Jun 10 '11 at 20:25

AWESOME!! I really appreciate your help! biz Jun 10 '11 at 20:28
2

@biz: When you reshape the N-by-M-by-3 RGB image into an N*M-by-3 matrix, the red values are in the frst
column, the green values are in the second column, and the blue values are in the third column. This is because
RESHAPE will draw elements from the 3-dimensional matrix column-wise then page-wise (i.e.rgbImage(:,1,1),
then rgbImage(:,2,1), ... , rgbImage(:,M-1,3), rgbImage(:,M,3)). Using []as an argument to RESHAPE
tells it to fgure out what that value should be. The second reshape simply reverses the process. gnovice Jun 10 '11
at 20:35
show 1 more comment

You might also like