How to replace RGB values

Hi Everyone,
I was wondering if it is possible to replace the RGB values of an image with values of certain range?
I have this image below and would like to change scale from 1600 to 3800.
Many thanks in advance.

 Respuesta aceptada

Guillaume
Guillaume el 4 de Mayo de 2016

1 voto

To go from the colour image to a grayscale image, assuming you have the original colour bar, use rgb2ind:
grayimg = rgb2ind(colourimg, map, 'nodither'); %where map is an nx3 matrix
This should give you a gray image with intensities from 0 to the number of colours in your map - 1.
You can then rescale that to whatever range you want with multiplication and addition.
To display the image with whatever colour map you want, use imshow to show the image, colormap to use a different colour map, colorbar to actually show the colour map, and caxis to change the mapping between intensity and colour map.

1 comentario

RG
RG el 4 de Mayo de 2016
It worked, thanks for your efforts to help me sort this out.

Iniciar sesión para comentar.

Más respuestas (2)

Alessandro Masullo
Alessandro Masullo el 3 de Mayo de 2016

0 votos

If I understood it correctly, you may simply use a linear scale. Once you read you image, you have a 3d matrix (row,col,3). The third dimension is the RGB. (:,:,1) is R, is (:,:,2) is G and the last one is B.
If you want to replace colours, you simply need to scale those matrices using some constants. If your image is 8 bits, the matrix will range from 0 to 255 (2^8-1). Convert it to double first, so that you won't lose information during the scale due to the rounding, and then scale the colours according to what you need:
RGB_Values = double(imread('RGB.jpg'));
RGB_Values(:,:,3) = RGB_Values(:,:,3)/2+40; % example of scaling

1 comentario

RG
RG el 3 de Mayo de 2016
Editada: RG el 3 de Mayo de 2016
Hi Alessandro,
Thanks for taking the time to help with this. Given the scaling interval is from 1600 to 3800, while scaling is it possible to know which element of RGB_Values(:, :, 3) corresponds to 1600?

Iniciar sesión para comentar.

Image Analyst
Image Analyst el 3 de Mayo de 2016

0 votos

Try this:
binaryImage = RGB_Values(:, :, 3) == 1600;
imshow(binaryImage);

7 comentarios

RG
RG el 3 de Mayo de 2016
Editada: RG el 3 de Mayo de 2016
This turned all the elements of RGB_Values(:,:,3) to "0" and the resulting image was all black.
Apologies if I have led to any confusions. I would like to reiterate what I wish to do. The image above has R, G and B values, and I was wondering if I can read this image with a scale from 1600 to 3800, either by finding the average of R, G and B values and scaling them, or through other possible ways?
Thanks for trying to help.
Image Analyst
Image Analyst el 3 de Mayo de 2016
I still don't know what you want. The binary image I gave you is an array where it's true or false according to your question "is it possible to know which element of RGB_Values(:, :, 3) corresponds to 1600" If you now want between 1600 and 3800 you'll need to do this
binaryImage = (RGB_Values(:, :, 3) >= 1600) & (RGB_Values(:, :, 3) <= 3800);
This will give an image where every white pixel means that the pixel value of RGB_Values is in the range 1600 to 3800 inclusive.
But I really don't know what "read this image with a scale from 1600 to 3800" means. Using imread() will read in all the values regardless of what value they have. I don't know what "reading with a scale" means. Please explain that request.
RG
RG el 4 de Mayo de 2016
Editada: RG el 4 de Mayo de 2016
I want to use the structure of the image (ideally the R,G and B values or any other possible routes) and replace the values from 3800 to 1600 (3800 being the dark red zone and 1600 being the yellow and green zones). I am not going to save it as an image at the end, the file will be written as a seismic file.
I have done replacing the values using excel, but it is a very slow and inefficient process, therefore I was wondering if it would be possible to do this in MATLAB.
Guillaume
Guillaume el 4 de Mayo de 2016
Editada: Guillaume el 4 de Mayo de 2016
@RMGH, your question does not make much sense because you don't appear to be using a standard way of displaying images.
In particular your statement that "3800 being the dark red zone and 1600 being the yellow and green zones" is puzzling. Higher values in RGB mean brighter pixels, so your intensity scale appears to be the wrong way round. Moreover, RGB values are triplets (one value for red, one for green, one for blue), not a single intensity value.
It would be clearer if you gave us the raw image you started with and explained how you displayed it.
RG
RG el 4 de Mayo de 2016
Editada: RG el 4 de Mayo de 2016
Guillaume, I can certainly elaborate more on what I am trying to do. I have, in fact, achieved what I want to do, but using a rather slow method, what I did was:
1.Read image in MATLAB;
2.Divide both the image and a scale bar (where green reads 1600 and red reads 3800) into equal amount of zones;
3.Extract R,G and B values and find average of them both for the image and colour bar;
4.Match each averaged RGB value with corresponding value from colour bar;
5.Then I moved onto Excel where I had to replace 2D matrix containing averaged RGB values with new set of values from 1600 to 3800.
So I was wondering if I can do the last two steps in MATLAB without switching to Excel.
Guillaume
Guillaume el 4 de Mayo de 2016
What you are trying to do is to apply a false colour scale to your original image. You can of course do that in matlab.
This is normally done on grayscale images. Is your original image really in colour to start with, and with the colour scale shown? Note that this colour scale is not very good since it is ambiguous (very high and low values are both black). Rainbow colour maps are not particularly useful anyway.
Or perhaps, you are trying to do the inverse, going from a false colour image to a grayscale image (hence go from RGB triplets to a single value per pixel)?
Image Analyst
Image Analyst el 4 de Mayo de 2016
Guillaume is right. You'd be much better off doing everything you possibly can do get the original monochrome image at the start, not a color image that you then have to reconstruct a monochrome image from the colorbar.

Iniciar sesión para comentar.

Categorías

Más información sobre Images en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

RG
el 3 de Mayo de 2016

Comentada:

el 4 de Mayo de 2016

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by