2D binary matrix conversion to an black and white image - matlab

I have a 2D binary matrix. How can I convert this into a black and white image?

you should write
I = mat2gray(A)
you can make a
imshow(I)
to see it working.

Related

How to create an inverse gray scale?

I have an image with dark blue spots on a black background. I want to convert this to inverse gray scale. By inverse, I mean, I want the black ground to be white.
When I convert it to gray scale, it makes everything look black and it makes it very hard to differentiate.
Is there a way to do an inverse gray scale where the black background takes the lighter shades?
Or, another preferable option is to represent the blue as white and the black as black.
I am using img = rgb2gray(img); in MATLAB for now.
From mathworks site:
IM2 = imcomplement(IM)
Is there a way to do an inverse gray scale where the black
background takes the lighter shades?
Based on your image description I created an image sample.png:
img1 = imread('sample.png'); % Read rgb image from graphics file.
imshow(img1); % Display image.
Then, I used the imcomplement function to obtain the complement of the original image (as suggested in this answer).
img2 = imcomplement(img1); % Complement image.
imshow(img2); % Display image.
This is the result:
Or, another preferable option is to represent the blue as white and
the black as black.
In this case, the simplest option is to work with the blue channel. Now, depending on your needs, there are two approaches you can use:
Approach 1: Convert the blue channel to a binary image (B&W)
This comment suggests using the logical operation img(:,:,3) > 0, which will return a binary array of the blue channel, where every non-zero valued pixel will be mapped to 1 (white), and the rest of pixels will have a value of 0 (black).
While this approach is simple and valid, binary images have the big disadvantage of loosing intensity information. This can alter the perceptual properties of your image. Have a look at the code:
img3 = img1(:, :, 3) > 0; % Convert blue channel to binary image.
imshow(img3); % Display image.
This is the result:
Notice that the round shaped spots in the original image have become octagon shaped in the binary image, due to the loss of intensity information.
Approach 2: Convert the blue channel to grayscale image
A better approach is to use a grayscale image, because the intensity information is preserved.
The imshow function offers the imshow(I,[low high]) overload, which adjusts the color axis scaling of the grayscale image through the DisplayRange parameter.
One very cool feature of this overload, is that we can let imshow do the work for us.
From the documentation:
If you specify an empty matrix ([]), imshow uses [min(I(:)) max(I(:))]. In other words, use the minimum value in I as black, and the maximum value as white.
Have a look at the code:
img4 = img1(:, :, 3); % Extract blue channel.
imshow(img4, []); % Display image.
This is the result:
Notice that the round shape of the spots is preserved exactly as in the original image.

Converting colour image to grey scale (Matlab)

I'm having some trouble converting a tif image into grey scale so I can have a look at the pixel values. I first used the code
im = imread('etc.');
im = rgb2gray(im);
imshow(im)
but this doesn't display grey scale values - it shows an RGB colour map. I've also tried loading the colour map to then convert the map
[im,map] = imread('etc.');
but the map is empty. Any pointers appreciated - I couldn't see anything useful in the Matlab help.
Edit: imshow results
There will not be a colour map in the file if it's a rgb image - only if it's an indexed image.
imshow uses a default colour map (jet). If you want a greyscale map, you have to deliberately set colormap gray.
im = rgb2gray(imread('filename'));
imshow(im)
colormap gray
I think with the .tif format you have to use the command
[X,map] = imread('imagename.tif')
newmap = rgb2gray(map)
imshow(X,newmap)
Link to online Matlab help where's this exact example: RGB to gray

imnoise gaussian and dct2() in matlab

I'm trying to do dicrete cosine transformation on black and white image and also I'm trying to do that transformation on the same image but with some noise on it. The problem is that imnoise adds "RGB noise" I would say, and because of that I cant do dct2() on my image. Easy example that I tried:
B = imnoise(A,'gaussian');
C = dct2(B);
How can I put noise on my image so it stays in grayscale mode?

Filling some region with colour, and the rest of the image as black

I have drawn some polygon on an image after using imshow and hold on, and filled it with white as follows:
fill(x(k),y(k),[1 1 1])
How can I make the rest of the image black while keeping the polygon white? In other words, how can I make a binary image, where the polygon is white, and the rest of the image is black? Provided that the polygon is a bit complex.
Thanks.
Use roipoly:
BW = roipoly( I, x(k), y(k) );
Where I is your input image (you only need it to get the desired output size of the binary maxk BW). y and x are the corners of your polygon.

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.