black&white to color image in Matlab - matlab

I am doing simple image processing in matlab. I turned my original image (jpg) into black and white image with the function im2bw and I did some modifications on this image. Do you know if it is possible to turn again this image to the original colors?

Grayscaling an image is a one-way function. Without the original data, you have no way of determining the hue of the color used before it was converted to grayscale, and so there is actually loss of data in the conversion.

Related

Image processing-YUV to Rgb

Why does one convert from YUV to RGB , what is the advantage in image processing using matlab of doing such a conversion. I know the answer partially that Y is the light component which gets eliminated in RGB format? what is the basis of such conversions?
I'll tell you what you could have easily found on the internet:
YUV was introduced when colour tvs came up. there should be minimum interference with existing monochrome tvs. so they added color information uv to the luminance signal y.
Due to the way digital colour images are captured (using red, green or blue pass-filtered pixels) the native colour space for digital images is RGB.
Modern displays also use red, green and blue pixels.
For printing you will find YMCK colour space as printing.
Nowadays RGB is the default colour space in digital image processing as we usually process the raw image information. you won't find many algorithms that can handle YUV images directly.

MATLAB imshow for 3D color image

I have a medical imaging matrix of size [200x200x200].
In order to display it, I am currently using imshow3D function, which is an excellent tool, built by Maysam Shahedi.
This tool displays the 3D image slice by slice, with mouse based slice browsing
In my current project, I generate an RGB image for each z-layer from the original input image. The output is a 3D color image of size [200x200x200x3] (each layer is now represented by 3 channels).
The imshow3D function works great on grayscale images. Is it possible to use it to display RGB images?
I took a look at this nice imshow3D function from Matlab FileExchange, and it is quite straight-forward to change it to allow working with a stack of RGB images.
The magic part of the function is
imshow(Img(:,:,S))
which displays the slice S of the image Img. We can simply change it to show all 3 channels of image S by changing this to Img(:,:,S,:). The result will be of size 200-by-200-by-1-by-3, while MATLAB expects RGB images to be of size 200-by-200-by-3. Simply squeeze this image to get the correct dimension. This results in:
imshow(squeeze(Img(:,:,S,:))
So to show RGB images, do a search-and-replace inside the function imshow3D, to replace all occurrences of Img(:,:,S) with squeeze(Img(:,:,S,:)) and it works!

Colormap and GIF images in Matlab

I have a problem understanding colormaps in Matlab and using them to import and diplay .gif images.
I would like to import an image using
im = imread('I.gif')
and then display it using
imshow(im)
but the result is wrong
If I do
[im,map] = imread('I.gif')
and then display it using
imshow(im,map)
it works properly, but still I don't understand the need of this colormap
Is there a way to import and convert my gif image to greyscale so that when I do
imshow(im)
it shows the correct greyscale image without having to worry about the colormap?
SOrry for the noob question but I am just starting with image processing in Matlab and I would really appreciate some help. It is my first question! :)
Bye and thanks!
If you want to convert your gif to grayscale, use ind2gray:
[im,map] = imread('I.gif');
imGray = ind2gray(im,map);
The reason that you need the colormap is that the gif format doesn't store image intensities, it stores indices into the colormap. So color 0 could be red or green or a very light shade of mauve. It's the colormap that stores the actual RGB colors that the image needs. ind2gray will take each of those colors, convert them to a grayscale intensity, and replace the indices in the image with those intensities.
It looks like you've really already answered the question. It seems that .gif files support an indexed color format, as a way of saving space. See:
https://en.wikipedia.org/wiki/Indexed_color
This is different than a more typical RGB color, which is what is often received from an IMREAD call.
To convert to a grayscale, you would need to go through the colormap and assign a grayscale value to each color, and then substitute those values back into the im variable.

matlab image blurring using fspecial

I am trying to write a matlab program for image blurring. I am required to use fspecial('average') and conv2 function. So far I have written the following code:
x=imread('ghoul.jpg');
subplot(211),imshow(x)
h=fspecial('average');
y=conv2(double(x),double(h));
subplot(212),imshow(y)
size of x is 250X250 uint8
The problem with the code is that it displays the original image fine but the image is only blurred at the bottom and white in the remaining area.
So far I have guessed that I haven't specified the size in h. But I am having problem in how to define the size in h. Whether it should be the size of x or not. It would be helpful if someone can just tell me how to write the size or another tip.
Thanks for your help.
The problem with the matlab code is that it was using imshow on double data type which caused the image intensity value to distort (barely visible or invisible in certain areas of the image). The filtered image needed rescaling of intensity values and as #eigenchris pointed out using:
imshow(y,[])
readjusted the image intensity values and the image was blurred perfectly.
side note: The size of the filter didn't had any effect on image distortion. (size is just used as a measure to how much you want to blur the image)

converting binary image to gray scale image in Matlab

I working on Optical Character Recognition system.
I want to convert the license plate image from binary to gray scale.
let's look at the next example:
this is the binary image:
and this is the gray scale:
what I want to know is if there is a way to convert it from binary to the gray, or this is not possible because i've lost the information when I converted the picture to binary at the beginning.
any idea how to do this? thanks
To convert a binary image of class logical to a grayscale image of class double, you simply call
double(yourBinaryImage)
EDIT
To revert from a binary image to the grayscale image you had before thresholding is impossible without the grayscale image, since by thresholding you have dropped all the grayscale texture information.
Maybe you can use the distance transform to achieve a gray scale image from a binary image. In MATLAB, try bwdist or something like that.
The result, of course, will not be the original gray scale image.
I think you cannot exactly get the grayscale image which you have shown from the binary image. What you can do is convert the image into grayscale and then do gaussian noising to spread the edge and then you can also add random noise to the whole image. So, now your new grayscale image will look a lot different than binary image.