converting binary image to gray scale image in Matlab - 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.

Related

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.

YCbCr to RGB conversion MATLAB using ycbr2rgb results in pink picture

I'm trying to convert an YCbCr image to RGB ysing MATLAB's function ycbcr2rgb. My resulting picture ends up being pink, and converting back again afterwards (should give me the original picture?) creates yet another image mostly grey.
For reference I tried to convert each channel individually by formula and it ends up the same.
I'm using a bigtiff format because of large filesize and if any help the imfinfo shows compression using JPEG.
Here is my code:
x=imread('picture.tiff','Index',9); %(9 subresolutions)
rgb=ycbcr2rgb(x);
imshow(rgb);
Can it be because of MATLABs function using the originial definition of YCbCr using ranges from 16-235 while my image is ranging from 0-255? If so is there any means of correcting this using the inbuild function?
I have added the pictures here, first image is showing imshow(rgb), while the second image is the original ycbcr. What I noticed is that in the Windows image viewer it actually shows it correct, it's just MATLAB's imshow that displays it pink after conversion.
Is there any chance you could point me in the right direction?
Thanks
Sonny
Apparently imread reads YCbCr images as RGB when loading it, which is why the problem occured.
Thanks for the help to all of you.
imread documentation
This link gives all the conversion formulae:
http://www.easyrgb.com/index.php?X=MATH&H=11
The below code converts image from RGB space to YCbCr space and back.
rgb = imread('board.tif');
imshow(rgb);
figure;
ycbcr = rgb2ycbcr(rgb);
imshow(ycbcr);
figure;
rgb2 = ycbcr2rgb(ycbcr);
imshow(rgb2);
Use MATLABs built in functions only. Also, if you're facing issues while converting from ycbcr to rgb you should probably try to convert the image to other form and then convert that form to RGB. (a dirty hack)
Just divide the image by 256 before converting it back to RGB.
y = ycbcr2rgb(z/256); % z holds the YCbCr image.
Worked for me.
Hope that helps :)

black&white to color image in 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.

Image Conversions in Matlab

I converted an RGB image (which is in double format) to a gray scale image of the same format using rgb2gray in Matlab. Now I want to convert the same image from gray to RGB. I used gray2rgb in Matlab but it's giving an error. So how can we convert a grayscale image to an RGB image using Matlab?
Short answer: you can't. Not perfectly at least.
As Sean says, this is because you have dropped some information when converting to grayscale. In other words, converting back from grayscale to RGB is an under-determined inverse problem, so there is no easy solution.
Now this doesn't mean you can't try. If you have some prior on the image, you can use it in addition to the information you have left to compute an estimate of the original RGB image.
For example if you know (or suppose) that the original image was already grayscale (in an RGB container) then you can reverse the process exactly. This is what the gray2rgb function Sean mentions is doing.
Most of these are open problems, so it's probably beyond what you want.
I'm sorry to say it's not possible.
By converting the image to grayscale you've reduced the amount of information (3 dimensions at each pixel down to 1) and this can't be recovered.
The rgb2gray function is one included in Matlab and works fine.
The gray2rgb function is not a standard Matlab function. If you are referring to this function on Matlab central, it's documentation states it doesn't do anything useful but just creates a 3d matrix from the 1d matrix; the image will still be grayscale.