How to convert a matrix to an image?

As clear as title declares, question is how to convert a matrix to an image with adjusting bitdepth?

I'm creating a 5x5 matrix setting values at all cells. Like,

A=[.....;.....;.....;.....;.....]

And then using imwrite function,

imwrite(A, Path, 'BitDepth', 2)

After using this function an image file occurs in the 'Path'. When i check bitdepth of the image, it is correct (2-bit) but when i read that image i see all zeros in image data matrix.

I want to create a 2-bit depth 5x5 image file which i could define all pixel values. How do i overcome that issue?

edit:

Complete code:

A=[0 0 2 1 1;1 2 2 2 2;2 2 2 2 3;1 2 3 3 2;2 3 1 3 2];
imwrite(A, 'Path', 'BitDepth', 2);
I=imread('Path'); //Path is 'C:\Users\...\...\...\...\...\...\A.png'

And i see all zeros in I matrix.

Upvotes: 0

Views: 3134

Answers (1)

Nopeman
Nopeman

Reputation: 56

You can use Image = mat2gray(A) to convert your matrix A to a grayscale image. Basically mat2gray() will just scale the values in the matrix from 0..1

After that imwrite(Image, 'Path.png', 'Bitdepth', 2) works fine.

Upvotes: 1

Related Questions