Reputation: 3
Idk why the code won't work. All it does is give me a blank white image. And if you don't declare a matrix before by zeros(x, y) then it works fine. Wth is wrong here?
I tried not declaring the zeros matrix before and only that works. I even tried doing img2(i,j) = img2(i,j)+img1(i,j)
function [imgOut] = scaleLoopBased(img,s)
%UNTITLED4 Summary of this function goes here
% creating a zero matrix of the given scale
[rows,columns]=size(img);
imgTemp=zeros(rows, columns);
for i=1:rows
for j=1:columns
imgTemp(i, j) = img(i, j);
end
end
imshow(imgTemp);
imgOut = imgTemp;
end
Blank white image
Upvotes: 0
Views: 73
Reputation: 125854
This is a result of your new image (of type double
, what zeros
creates by default) not having the same type as your original image (typically type uint8
). You can fix this by initializing your new image to have the same data type as the original using the class
function and passing an additional argument to zeros
:
imgTemp = zeros(rows, columns, class(img));
The reason it works correctly when you don't initialize imgTemp
is because MATLAB initializes the variable for you using the data type of img
by default when you perform the first indexed assignment.
The imshow
utility expects one of the standard image types in MATLAB. An image of type double
is expected to have values spanning the range [0 1]
, while images of type uint8
will have values in the range [0 255]
. In your example you likely have a matrix imgTemp
that is of type double
but has values spanning [0 255]
. Another way to fix your issue is to explicitly tell imshow
what value range to use for the display (since the default [0 1]
doesn't work):
imshow(imgTemp, [0 255]);
Always be aware of the data type when manipulating or processing images. You may need to scale or convert back and forth, using a double
type for calculations (since integers saturate) and a uint8
type for display and reading/writing to files.
Upvotes: 1