How to change a gray scale image to red colored image? - matlab

I have an image of a thick wave which changes from black to white smoothly like the black slowly changes from dark to white. How can I change this image to red instead of having different shades of black I need different shades of red going from dark red to white.

Create an array of size (rows,cols,3) where (rows,cols) is the size of gray image. Make its red channel equal to 255 and other channels equal to gray image
img = imread('moon.tif');
[r,c,z] = size(img);
red_img = uint8(zeros(r,c,3));
red_img(:,:,1) = 255;
red_img(:,:,2) = img;
red_img(:,:,3) = img;
subplot(1,2,1);
imshow(img,[]);
subplot(1,2,2);
imshow(red_img,[]);

Related

Chromatic aberration correction of an image

I am stuck with figuring out how to correct following image with chromatic abberation through matlab. It seems there is not enough information on the internet, that is why it was hard for me to get my head around it.
I have tried the following code that splits the image into RGB and produces a histogram. Histogram then shows that blue channel is left the same but green and red are distorted.
I would appreciate any help, thank you.
cl = imread('raw3-image22.png');
% Extract colour channels
redChannel = cl(:,:,1); % Red channel
greenChannel = cl(:,:,2); % Green channel
blueChannel = cl(:,:,3); % Blue channel
allBlack = zeros(size(cl, 1), size(cl, 2), 'uint8')
red = cat(3, redChannel, allBlack, allBlack);
green = cat(3, allBlack, greenChannel, allBlack);
blue = cat(3, allBlack, allBlack, blueChannel);
imshow(cl);
improfile;
Chromatic abberation image: (https://i.stack.imgur.com/ajNd6.png)
From what I can see, the blue color plane is in focus, while the green and red are increasingly blurred.
You might try supervised deblurring, using stars for the point spread functions, green and red planes separately.
Deblurring is never perfect, though.
[Presumably, you won't appreciate this other solution: to blur the green and blue planes and after recomposition, reduce the image resolution by decimation.]

Background and foreground color changes

Hello i need to make background color black and foreground color white. As u can see i did this with transfering image to 2 dimension. I want to make this color changes in 3 dimension, so we are nor allowed to transfer it bw. Is there any way to do this ?
logo=imread('logo.png');
subplot(2,2,1);
imshow(logo);
b=rgb2gray(logo);
subplot(2,2,2);
imshow(b);
c=im2bw(b,0.92)
subplot(2,2,3);
imshow(c);
c = 1-c;
subplot(2,2,4);
imshow(c);
Preface:
To set the pixel to white or black each layer of the pixel needs to be set to an intensity value of 0 (black) or 255 (white).
White Pixel → rgb(255,255,255)
Black Pixel → rgb(0,0,0)
The colon can be used to obtain all the indices in the 3rd dimension (grab all the layers). To grab one RGB-pixel in the top-left corner of the image:
RGB_Pixel = Image(1,1,:);
Method 1:
If you wish to retain the three colour channels you can use matrix indexing to change the white background to black. Matrix indexing can also be used to change anywhere that isn't white to white. This method may, unfortunately, break down if you have a coloured component with a 255 intensity component. This doesn't seem to be the case for your image though. You can use method 2 for a more safe approach.
logo = imread('logo.png');
[Image_Height,Image_Width,Depth]= size(logo);
new_logo = zeros(Image_Height,Image_Width,Depth);
new_logo(logo == 255) = 0;
new_logo(logo ~= 255) = 255;
imshow(new_logo);
Method 2:
Checks each pixel (RGB-triplet) using a set of for-loops that scan through the entire image. If the RGB-intensities of the pixel are rgb(255,255,255) then the pixels are set to 0 (black). If the RGB-intensities of the pixel are anything else the pixels are set to 255 (white). The ~ismember() function is used to check if the RGB-pixel has an intensity that is not 255 (not-white).
logo = imread('logo.png');
%Grabbing the size of the image%
[Image_Height,Image_Width,~]= size(logo);
for Row = 1: Image_Height
for Column = 1: Image_Width
%Grabbing RGB pixel%
RGB_Pixel = logo(Row,Column,:);
if(~ismember(255,RGB_Pixel))
%RGB pixel is white change
logo(Row,Column,:) = 255;
else
%RGB pixel is coloured change to black%
logo(Row,Column,:) = 0;
end
end
end
imshow(logo);
Using the repmat() function is also a great solution that the above comment suggested. Which possibly may be the quickest method since you already have the code that generates one layer from the greyscale image.
Ran using MATLAB R2019b

How can i detect silver pixels in an image?

I'm trying to detect all the pixels with silver colors in my image.
I tried to convert the image from RGB to HSV but I saw that every silver pixel is very different from another and therefore we couldn't identify all the silver pixels.
I'm trying to understand if it's possible to use HSV to solve this problem or maybe there is a better way to detect all the silver pixels?
I'm trying to use this code that will mark all the silver pixels in blue:
I chose the Hue,Saturation and Value (HSV) according to wikipedia to identify the silver color, therefore Hue doesn't matter, Saturation is 0-0.1 and Value is 0.65-0.85.
clear all; close all; clc; imtool close all;
im = imread('image.jpg');
size_x = size(im,1);
size_y = size(im,2);
size_z = size(im,3);
hsv = rgb2hsv(im);
for i = 1:size_x
for j = 1:size_y
if (hsv(i,j,2) < 0.1) && (hsv(i,j,3) > 0.65) && (hsv(i,j,3) < 0.85)
im(i,j,1) = 0;
im(i,j,2) = 0;
im(i,j,3) = 255;
end
end
end
imtool(im);
with this Image:
Thanks in advance.
The difference between a white pixel and a grey pixel is the amount of light that falls on it. You can see this in your picture, the whitish background changes brightness from left to right, going from middle grey to light grey. We perceive it all as white.
The same is true for silver. Silver is a white/grey color, but we perceive it differently because it has specular reflections. This is what characterizes metal surfaces. But the color of the pixels themselves are just grey and white.
That is to say, you cannot use color segmentation to distinguish silver contacts from white paper.
I would suggest you look for the shapes of interest, rather than the colors of interest.

How to overlap color figure on a gray one with colorbar in MATLAB?

I'm trying to color code some parameter values on a gray image in MATLAB. I'm wondering how to show gray image, make parameter values colored on the gray appearance image and on some pixels, and finally draw a colorbar aside the image just showing parameter values range.
Unsuccessful Code since now:
I = Igray; % gray image
Icc = zeros(size(I,1),size(I,2),3); % color coded image
Icc(:,:,1) = I;
Icc(:,:,2) = I;
Icc(:,:,3) = I;
Icc('some address in the image',3) = 'some number between 0 and 255';
imshow(Icc,[])
colorbar % colorbar showing colored parts spectrum
Result image that I need:
Try something like this:
I = Igray; % gray image
RGB = [1.0,0.7,0.2]; % color for patch
x = 30:50;
y = 70:90;
% If you gray image is in the range [0,255], make it range [0,1]
I = double(I)/255;
Icc = repmat(I,[1,1,3]);
block = I(y,x);
Icc(y,x,1) = 1 - ((1-block) * (1-RGB(1)));
Icc(y,x,2) = 1 - ((1-block) * (1-RGB(2)));
Icc(y,x,3) = 1 - ((1-block) * (1-RGB(3)));
imshow(Icc)
I'm sure there is a prettier way to encode this, but this way it shows the intent.
You're basically multiplying the grey values with the RGB color you want to make the patch. By inverting the patch and the color first, and inverting the result, the multiplication makes the patch brighter, not darker. That way you get the effect you want where the dark parts show the color also. If you multiply directly without inverting first, black stays black and doesn't show the color.
You'll have to figure out how to coordinate with the color bar after that. There are commands in MATLAB to set the limits of the color bar, you can find those reading the documentation.
The color bar you show uses the PARULA color map. You could do this to find the right RGB value to color your patch:
T; % value to color your patch in, in range [0,1]
cm = parula(256);
RGB = interp1(cm,T*255,'linear')

Colouring specific pixels in an image

Say I have an image. How can I colour some specific pixels in that image using MATLAB?
Thanks.
RGB Pixels
I'd suggest working with an RGB image, so that you can easily represent color and gray pixels. Here's an example of making two red blocks on an image:
img = imread('moon.tif');
imgRGB = repmat(img,[1 1 3]);
% get a mask of the pixels you want and set an RGB vector to those pixels...
colorMask = false(size(imgRGB,1),size(imgRGB,2));
colorMask(251:300,151:200,:) = true; % two discontiguous blocks
colorMask(50:100,50:100,:) = true;
redPix = permute([255 0 0],[1 3 2]);
imgRGB(repmat(colorMask,[1 1 3])) = repmat(redPix, numel(find(colorMask)),1);
AlphaData image property
Another cool way of doing this is with an image's AlphaData property. See this example on a MathWorks blog. This essentially turns color on or off in certain parts of the image by making the gray image covering the color image transparent. To work with a gray image, do like the following:
img = imread('moon.tif');
influenceImg = abs(randn(size(img)));
influenceImg = influenceImg / (2*max(influenceImg(:)));
imshow(img, 'InitialMag', 'fit'); hold on
green = cat(3, zeros(size(img)), ones(size(img)), zeros(size(img)));
h = imshow(green); hold off
set(h, 'AlphaData', influenceImg)
See the second example at the MathWorks link.