Reputation: 48966
When I read some image in MATLAB
, do we say that this image is also a data matrix, or are they are two different things? If they are different, how can we convert an image to a data matrix in MATLAB
?
As far as I know, an image is a matrix of pixels, isn't it?
Upvotes: 2
Views: 1395
Reputation: 112759
It depends.
There are (at least) four kinds of images in Matlab:
Grayscale images: in this case yes, the image is just a matrix. Each matrix entry represents the gray level of the corresponding pixel.
Indexed color images: the image is described by a matrix and a colormap. Each row of the colormap is a three-element vector defining a color (in terms of three primary colors such as R, G, B). Each matrix entry contains a number which is a pointer to the colormap. Specifically it contains a row number of the colormap, such that the color of that pixel is described by the corresponding row of the colormap.
Indexed color images with transparency information: the image is described as in case 2 but in addition there is a transparency (alpha) matrix, which needs to be taken into account to render the actual color.
Non-indexed color images: the image is represented as a 3D array of size MxNx3, where MxN is the image size. The first two indices specify a pixel, and the third refers to each of the primary colors, usually R, G, B. For example, the entry (m,n,1) indicates how much red the (m,n) pixel contains.
Depending on which image file format you have and how you read it into Matlab, you may get one of this four types. Also, you may get double
or uint8
values in the matrix/3D-array. See imread
documentation for details.
Upvotes: 0
Reputation: 3574
Images are seen as data matrices, yes. There is one thing you should be aware of is that MATLAB treats coordinates of matrices and images differently.
See: http://www.mathworks.com/help/images/image-coordinate-systems.html
Confusing at first, but you get used to it. Use preferably the function of the Image Processing Toolbox, if you have it.
Exemple:
Let's create an upper triangular matrix of size (100x100) and display it as a binary image:
D=triu(ones(100,100));
imshow(D)
Up to here, all is well, if I try to access element of this matrix with the usual indexing:
D(5, 10);
I get the value one, with is as expected as the index of the row (the firs one) is smaller than the one of the column.
Now if I use the interactive tool to get coordinates of points on the image clicked with the mouse like this (I selected the left one first):
imshow(D);
[x y]=getpts;
It returns the following coordinates: (7,16) and (93,17).
Images are treated as having the x-axis from left to right and the y-axis from top to bottom.
Upvotes: 0
Reputation: 156
It is a data matrix. Most time it is uint8
format when you load the image, sometimes in three dimension (RGB, HSV...), and sometimes in two (grayscale). To get more operations between the matrix, the best way is to convert the data to double format (just use double
).
code example:
I=imread('img1.jpg');
I=double(I);
J=imread('img2.jpg');
J=double(J);
% you can implement various array operations such as multiplication, dot product, power, ..to name only a few
K = I.*J;
K1 = I.*(J<240);
K2 = J.^(0.5);
Upvotes: 1
Reputation: 1295
When you load an image, it is automaticlly represented as a matrix.
>> A = imread('example.jpeg');
>> size(A)
ans = 512 512 3
So A is a 512-512-3 array- which is representing an RGB image!
TO understand the RGB-representation, try the following snipped, it should give you insight:
I = imread('example.jpeg');
%Red
R = I(:,:,1);
image(R), colormap([[0:1/255:1]', zeros(256,1), zeros(256,1)]), colorbar;
%Blue
B = I(:,:,3);
figure;
image(B), colormap([zeros(256,1), zeros(256,1), [0:1/255:1]']), colorbar;
%Green
G = I(:,:,2);
figure;
image(G), colormap([zeros(256,1),[0:1/255:1]', zeros(256,1)]), colorbar;
Upvotes: 1