How to replace RGB values

42 ビュー (過去 30 日間)
RG
RG 2016 年 5 月 3 日
コメント済み: Image Analyst 2016 年 5 月 4 日
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.

採用された回答

Guillaume
Guillaume 2016 年 5 月 4 日
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 件のコメント
RG
RG 2016 年 5 月 4 日
It worked, thanks for your efforts to help me sort this out.

サインインしてコメントする。

その他の回答 (2 件)

Alessandro Masullo
Alessandro Masullo 2016 年 5 月 3 日
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 件のコメント
RG
RG 2016 年 5 月 3 日
編集済み: RG 2016 年 5 月 3 日
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?

サインインしてコメントする。


Image Analyst
Image Analyst 2016 年 5 月 3 日
Try this:
binaryImage = RGB_Values(:, :, 3) == 1600;
imshow(binaryImage);
  7 件のコメント
Guillaume
Guillaume 2016 年 5 月 4 日
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 2016 年 5 月 4 日
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.

サインインしてコメントする。

Community Treasure Hunt

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

Start Hunting!

Translated by