Shades of gray image instead of RGB - image

I need to resize image without imresize() function. But the result comes in black&white colors even if i upload RGB image. What should I change to receive RGB image?
clc;
clear;
[FileName, PathName] = uigetfile('*.JPG');
I=imread([PathName FileName]);
ms=input('Input index of compression(>1)');
[m,n,v]=size(I);
if mod(m,ms)~=0
m=ms*floor(m/ms);
end
if mod(n,ms)~=0
n=ms*floor(n/ms);
end
C=I(1:m-1,1:n-1,:);
A=double(C);
figure
imshow(C)
[x,y,~]=size(A);
result=zeros(floor(x/ms),floor(y/ms));
p=1;
t=1;
for i=ms+1:ms:x
for j=ms+1:ms:y
arr=A(i-ms:i,j-ms:j);
k=max(max(arr));
result(t,p)=k;
p=p+1;
end
t=t+1;
p=1;
end
Ci=uint8(result);
figure
imshow(Ci) ```

An RGB image has 3 dimensions. The image matrix returned by the imread function has the size of height × width × channels where the number of channels is 3 (red, green and blue).
If you want to get a result which is also an RGB image, you have to initialize it as such, and fill its values along the third dimension with the R, G, and B color values:
result=zeros(floor(x/ms),floor(y/ms), 3); % it has 3 color layers
p=1;
t=1;
for i=ms+1:ms:x
for j=ms+1:ms:y
arr=A(i-ms:i,j-ms:j, :); % the color info remains unchanged
k=max(max(arr));
result(t,p,:)=k; % `result` is a 3D array
p=p+1;
end
t=t+1;
p=1;
end

Related

Like an ordinary indexed image,in the figure’s colormap. The difference is that the matrix values are linearly

find pixels location that have value/color =white
for i=1:row
for j=1:colo
if i==x if the row of rgb image is the same of pixel location row
end
end
end
end
what's Wrong
You can use logical indexing.
For logical indexing to work, you need the mask (bw2) to be the same size as RGB.
Since RGB is 3D matrix, you need to duplicate bw2 three times.
Example:
%Read sample image.
RGB = imread('autumn.tif');
%Build mask.
bw2 = zeros(size(RGB, 1), size(RGB, 2));
bw2(1+(end-30)/2:(end+30)/2, 1+(end-30)/2:(end+30)/2) = 1;
%Convert bw2 mask to same dimensions as RGB
BW = logical(cat(3, bw2, bw2, bw2));
RGB(BW) = 255;
figure;imshow(RGB);
Result (just decoration):
In case you want to fix your for loops implementation, you can do it as follows:
[x, y] = find(bw2 == 1);
[row, colo, z]=size(RGB); %size of rgb image
for i=1:row
for j=1:colo
if any(i==x) %if the row of rgb image is the same of pixel location row
if any(j==y(i==x)) %if the colos of rgb image is the same of pixel loca colo
RGB(i,j,1)=255; %set Red color channel to 255
RGB(i,j,2)=255; %set Green color channel to 255
RGB(i,j,3)=255; %set Blue color channel to 255
end
end
end
end
[x, y] = find(bw2 == 1)
x and y are arrays unless there is only one pixel is white.
However, if i==x and if j==y are comparing a single number with an array. This is wrong.
As Anthony pointed out, x and y are arrays so i==x and j==y won't work as intended. Furthermore RGB(i,j) only uses the first two dimensions, but RGB images have three dimensions. Lastly, from an optimization point of view, the for-loops are unnecessary.
%% Create some mock data.
% Generate a black/white image.
bw2 = rand(10);
% Introduce some 1's in the BW image
bw2(1:5,1:5)=1;
% Generate a RGB image.
RGB = rand(10,10,3)*255;
%% Do the math.
% Build a mask from the bw2 image
bw_mask = bw2 == 1;
% Set RGB at bw_mask pixels to white.
RGB2 = bsxfun(#plus, bw_mask*255, bsxfun(#times, RGB, ~bw_mask)); % MATLAB 2016a and earlier
RGB2 = bw_mask*255 + RGB .* ~bw_mask; % MATLAB 2016b and later.

Convert matlab.graphics.primitive.Image (output of imagesc) to rgb array

I'm using the MATLAB function
imagesc(my_gray_valued_image)
to visualize my_gray_valued_image: [1024x1024] double array with values from 0.0 - 1.0 (gray values) using colormaps like jet.
I want to store the output as a RGB image ([1024x1024x3] double array). However the output of the function is a Image object (matlab.graphics.primitive.Image) that contains the original array (Image.CData) but doesn't allow to extract the colorscaled image.
Following a similar (although confusingly cluttered) question (How to convert an indexed image to rgb image in MATLAB?) I tried the following, but that gave me a plain blue image:
RGB = ind2rgb(my_gray_valued_image, jet);
imshow(RGB);
here for arbitrary colormap:
im = rand(5); % input [0-1] image
figure;
h = imagesc(im); % imagesc handle
title('imagesc output')
cdata = h.CData; % get image data (if you don't have variable 'im')
cm = colormap(h.Parent); % get axes colormap
n = size(cm,1); % number of colors in colormap
c = linspace(h.Parent.CLim(1),h.Parent.CLim(2),n); % intensity range
ind = reshape(interp1(c,1:n,im(:),'nearest'),size(im)); % indexed image
rgb = ind2rgb(ind,cm); % rgb image
figure;
imshow(rgb,'InitialMagnification','fit');
title('rgb image')
You can use ind2rgb to convert an intensity image into RGB using a colormap of your choice; but make sure that the range of the input is from 1 to the number of colors in the colormap. This is because ind2rgb maps value 1 to the first color, 2 to the second etc.
im = rand(5,5); % example intensity image
cmap = jet(256); % desired colormap
result = ind2rgb(ceil(size(cmap,1)*im), cmap);
The reason why you are getting a blue image is that ind2rgb clips the values of the input image to the range from 1 to the number of colors in the colormap. So, if the input image has values between 0 and 1 they are all mapped to 1, that is, to the first color in the colormap.

access and change the values of the R, G and B of a desired pixel in an image using MATLAB

How can I change the values of the R, G and B in an image manually using MATLAB?
The computation for a green color enhancement needs to be done so how do we access and change the values of RGB using MATLAB?
Assuming you are using imread to read in the image, an RGB image is stored as an M x N x 3 matrix, where M,N are the rows and columns of the image. This is essentially a 3D matrix, where each colour plane is in a particular dimension. The red plane is the first of the third dimensions, the green plane is the second, and the blue plane is the third. As such, you can do something like:
im = imread('onion.png'); % // Built-in to MATLAB
red = im(:,:,1); %// Red channel
green = im(:,:,2); % // Green channel
blue = im(:,:,3); % // Blue channel
You can also merge the planes back by doing: im2 = cat(3, red, green, blue); Now, you can manipulate any of these planes by themselves. If you want to grab a subset of the image, you can do:
imSubset = im(row1:row2, col1:col2, :);
This will grab all pixels between rows row1 to row2 and columns col1 to col2. You can then split up the image into their corresponding planes.
Now, if you want to manually change pixels, you simply access whichever rows and columns you want in each of the planes and set them to whatever you want. For example, if you wanted to set a particular region in your image to all yellow pixels, you can do this:
im(1:50,1:50,1) = 0;
im(1:50,1:50,2) = 255;
im(1:50,1:50,3) = 255;
imshow(im);
This should place a yellow square of 50 pixels wide in the top left corner. You can also do the subset approach by:
imSubset = im(1:50,1:50,:); %// Extract
imSubset(:,:,1) = 0; %// Set
imSubset(:,:,2) = 255;
imSubset(:,:,3) = 255;
im(1:50,1:50,:) = imSubset; %// Place back
If I can be a shameless promoter, take a look at my Introduction to Digital Image Processing using MATLAB slides - http://www.slideshare.net/rayryeng1/introduction-to-digital-image-processing-using-matlab
Good luck!

Image upsampling in MATLAB producing white images using image() and imshow()

When I display my reconstructed images, they are just white. Is there something obviously wrong with my program?
The reconstructed images should have the values of the downsampled image at one pixel in the upsampled 2x2 pixel block. The interpolation method I'm using here is simply taking the value from one row above and filling the next row with it, repeating this process for the columns.
%% Image Resampling
close all; clear all; clc;
s_dir=pwd;
cd Images;
I=imread('aivazovsky78g.tif','tif');
cd(s_dir)
[N M]=size(I);
figure;
imshow(I)
axis image; hold on;
for k=1:4
pause(1)
I=I(1:2:N, 1:2:M);
[N M]=size(I);
image(I)
end
%% Image Reconstruction
Irec=zeros(2*size(I));
for r=1:5
for n=1:N-1
for m=1:M-1
Irec(2*n-1,2*m-1)=I(n,m);
end
end
[N M]=size(Irec);
for n=2:2:N
for m=2:2:M
Irec(n,:)=Irec(n-1,:);
Irec(:,m)=Irec(:,m-1);
end
end
I=Irec;
figure;
imshow(I)
end
You may use B = imresize(A, scale, 'box') where a scale of 2 doubles the amount of pixels in x and y. The z dimension will still have the same value.
The resizing method box will copy the initial pixel value (i, j) to its 3 new neighbors (i+1, j), (i, j+1), and (i+1, j+1) - the same method you programmed.
Not the most efficient way, but here is a working code:
% 256x256 grayscale image
I = imread('cameraman.tif');
% double in size
I2 = zeros(2*size(I),class(I));
for i=1:2:size(I2,1)
for j=1:2:size(I2,2)
I2([i i+1],[j j+1]) = I((i-1)/2 + 1, (j-1)/2 + 1);
end
end
% compare against #Magla's solution
I3 = imresize(I,2,'box');
isequal(I2,I3)

How to show histogram of RGB image in Matlab?

I read an image in matlab using
input = imread ('sample.jpeg');
Then I do
imhist(input);
It gives this error:
??? Error using ==> iptcheckinput
Function IMHIST expected its first input, I or X, to be two-dimensional.
Error in ==> imhist>parse_inputs at 275
iptcheckinput(a, {'double','uint8','logical','uint16','int16','single'}, ...
Error in ==> imhist at 57
[a, n, isScaled, top, map] = parse_inputs(varargin{:});
After running size(input), I see my input image is of size 300x200x3. I know the third dimension is for color channel, but is there any way to show histogram of this? Thanks.
imhist displays a histogram of a grayscale or binary images. Use rgb2gray on the image, or use imhist(input(:,:,1)) to see one of the channel at a time (red in this example).
Alternatively you can do this:
hist(reshape(input,[],3),1:max(input(:)));
colormap([1 0 0; 0 1 0; 0 0 1]);
to show the 3 channels simultaneously...
I pefere to plot the histogram for Red, Green and Blue in one plot:
%Split into RGB Channels
Red = image(:,:,1);
Green = image(:,:,2);
Blue = image(:,:,3);
%Get histValues for each channel
[yRed, x] = imhist(Red);
[yGreen, x] = imhist(Green);
[yBlue, x] = imhist(Blue);
%Plot them together in one plot
plot(x, yRed, 'Red', x, yGreen, 'Green', x, yBlue, 'Blue');
An histogarm plot will have number of pixels for the intensity levels.
Yours is an rgb image. So you first need to convert it to an intensity image.
The code here will be:
input = imread ('sample.jpeg');
input=rgb2gray(input);
imhist(input);
imshow(input);
You will be able to get the histogram of the image.
img1=imread('image.jpg');
img1=rgb2gray(img1);
subplot(2,2,1);
imshow(img1);
title('original image');
grayImg=mat2gray(img1);
subplot(2,2,2);
imhist(grayImg);
title('original histogram');
Remember to include
mat2gray();
because it converts the matrix A to the intensity image grayImg. The returned matrix grayImg contains values in the range 0.0 (black) to 1.0 (full intensity or white).
Histogram is useful to analyze pixel distribution in an image. Histogram plots number of pixel in an image with respect to intensity value.
img1=imread('image.jpg');
hist(img1);

Resources