MATLAB Answers

0

how to convert color of a pixel gray to red

Asked by abdel kader on 19 Mar 2019
Latest activity Commented on by Walter Roberson
on 23 Mar 2019
g=imread('f8');
rgb2gray(g);
imshow(g);
how to convert color of a pixel gray to red plzz

  2 Comments

abdel - don't you need to assign an output to rgb2gray like
g=imread('f8');
grayscaleImage = rgb2gray(g);
imshow(grayscaleImage);
As for converting a pixel gray to red...which pixel? what are the dimensions of grayscaleImage?

Sign in to comment.

1 Answer

Answer by Walter Roberson
on 21 Mar 2019
Edited by Walter Roberson
on 23 Mar 2019

rgbimage = im2double( repmat(grayscaleImage, 1, 1, 3) );
rgbimage(some_row, some_column, 1) = 1; %R
rgbimage(some_row, some_column, 2) = 0; %G
rgbimage(some_row, some_column, 3) = 0; %B
You can use the above with vectors of some_row and some_column as long as you want to change rectangular areas. If you have scattered row and column locations to set then
rgbimage = im2double( repmat(grayscaleImage, 1, 1, 3) );
[nrow, ncol, npane] = size(rgbimage);
idx = sub2ind([nrow, ncol], scattered_row, scattered_column);
rgbimage(idx + 0*nrow*ncol) = 1; %R
rgbimage(idx + 1*nrow*ncol) = 0; %G
rgbimage(idx + 2*nrow*ncol) = 0; %B

  4 Comments

Show 1 older comment
Then use some_column instead of some_colum, and try not using im2double() and set red channel to 255 instead of 1.
The im2double and setting to 1 are there to avoid having to write code to figure out what the maximum value to write in should be, since we do not know cloass(grayscaleImage)
My code works for me with camerman.tif . The one thing to note is that if you imshow(rgbimage) then the red pixels might be difficult to notice, especially if it is against a lighter background (red on dark is easier to notice.) You might want to draw your image larger if you are using imshow().

Sign in to comment.