drjrm3
drjrm3

Reputation: 4728

What is the difference between a matrix and image in Matlab?

I can save a .png image as pic = imread(image_name) and I can treat it as a matrix including the size so that I can retrieve [nrows, ncols, ~] = size(pic). I can then easily show this image with imshow(pic).

However, if I create my own matrix with test = zeros(nrows, ncols, 3) and try to copy over the image with test(:, :, :) = pic and try to use imshow(test) it doesn't work. I can compare test and pic element by element and they are the same, but I can't subtract the two or I get an error of

Error using  - 
Integers can only be combined with integers of the same class, or scalar doubles.

How can I create a matrix and assign pixels from an image to it and still treat the matrix as an image? The reason I am trying to do this is that I have many pictures I am trying to combine into one larger picture, so I need to create a large matrix beforehand, then copy pixels from each seperate image into the larger matrix, but this larger matrix is no longer treated as an image when I use imshow or imwrite.

Upvotes: 0

Views: 271

Answers (1)

bla
bla

Reputation: 26069

the error is telling you the thing that you miss. It is the class of the variable that matters. an image has some class type (uint8, uint16...), a generic matrix in matlab, unless stated otherwise is double. Try to define

 test = zeros(size(pic),class(pic));

Upvotes: 1

Related Questions