Thread Subject: uint8 vs double

Subject: uint8 vs double

From: kal

Date: 2 Feb, 2012 17:08:10

Message: 1 of 5

Hi all,

I am trying to store 1000 gray scale images in .mat file. each image is of 256x248 size. After storing all the images in mat file say T, it is created as double.
when I tried to display the image using imshow(T(:, 1:248)), I cannot see the image, instead it is totally white.
Then I changed T to uint8 using T = uint8(T); there is no change in the intensity values, however I can view the image using imshow(T(:, 1:248)).
If I change the T which is double initially using T = im2uint8(T), the intensity values change to 255 and image is totally white.

I do not understand what is the difference between T = uint8(T); and using T = im2uint8(T).

Please clarify.
Kal

Subject: uint8 vs double

From: ImageAnalyst

Date: 2 Feb, 2012 17:58:15

Message: 2 of 5

I wouldn't think saving a uint8 variable to a mat file and then
recalling it would change its class. Can you show the lines where you
called save() and load()?

Subject: uint8 vs double

From: kal

Date: 2 Feb, 2012 19:19:14

Message: 3 of 5

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <2a58707d-29dd-4ef3-948f-77a8a1c5ff9b@k3g2000pbn.googlegroups.com>...
> I wouldn't think saving a uint8 variable to a mat file and then
> recalling it would change its class. Can you show the lines where you
> called save() and load()?
Here is my code: This is for loading 6 images in mat file
T = zeros(50,300);
Train_Number = 6; %Number of uniforms images per class
j = 1;k = 50;
TrainDatabasePath = uigetdir('Select images database path' );
for i = 1 : Train_Number
    x = num2str(100 + mod(i,100));
    y = x(2:3);
    str = strcat('B-uniform', y);
    str = strcat('\',str,'.ppm');
      str = strcat(TrainDatabasePath,str);
      img = imread(str);
     img = rgb2gray(img);
T(1:50,j:k) = img; j = j+50; k = k+50;
end
T = uint8(8);
save T;

Thanks,
Kal

Subject: uint8 vs double

From: James Tursa

Date: 3 Feb, 2012 00:23:09

Message: 4 of 5

"kal" wrote in message <jgenji$bgv$1@newscl01ah.mathworks.com>...
> ImageAnalyst <imageanalyst@mailinator.com> wrote in message <2a58707d-29dd-4ef3-948f-77a8a1c5ff9b@k3g2000pbn.googlegroups.com>...
> > I wouldn't think saving a uint8 variable to a mat file and then
> > recalling it would change its class. Can you show the lines where you
> > called save() and load()?
> Here is my code: This is for loading 6 images in mat file
> T = zeros(50,300);

The above line creates T as a double class variable, size 50 x 300

> Train_Number = 6; %Number of uniforms images per class
> j = 1;k = 50;
> TrainDatabasePath = uigetdir('Select images database path' );
> for i = 1 : Train_Number
> x = num2str(100 + mod(i,100));
> y = x(2:3);
> str = strcat('B-uniform', y);
> str = strcat('\',str,'.ppm');
> str = strcat(TrainDatabasePath,str);
> img = imread(str);
> img = rgb2gray(img);
> T(1:50,j:k) = img; j = j+50; k = k+50;

The above line takes the variable img, converts it to double class, then copies these double values into the T locations you have listed.

> end
> T = uint8(8);

Huh? Was this leftover debugging code?

> save T;
>
> Thanks,
> Kal

You probably want to use this pre-allocation:

    T = zeros(50,300,'uint8');



James Tursa

Subject: uint8 vs double

From: ImageAnalyst

Date: 2 Feb, 2012 21:28:53

Message: 5 of 5

On Feb 2, 2:19 pm, "kal " <kalyana...@gmail.com> wrote:
> ImageAnalyst <imageanal...@mailinator.com> wrote in message <2a58707d-29dd-4ef3-948f-77a8a1c5f...@k3g2000pbn.googlegroups.com>...
> > I wouldn't think saving a uint8 variable to a mat file and then
> > recalling it would change its class.  Can you show the lines where you
> > called save() and load()?
>
> Here is my code: This is for loading 6 images in mat file
> T = zeros(50,300);
> Train_Number = 6; %Number of uniforms images per class
> j = 1;k = 50;
> TrainDatabasePath = uigetdir('Select images database path' );
> for i = 1 : Train_Number
>     x = num2str(100 + mod(i,100));
>     y = x(2:3);
>     str = strcat('B-uniform', y);
>     str = strcat('\',str,'.ppm');
>       str = strcat(TrainDatabasePath,str);
>       img = imread(str);
>      img = rgb2gray(img);
> T(1:50,j:k) = img; j = j+50; k = k+50;
> end
> T = uint8(8);
> save T;
>
> Thanks,
> Kal

------------------------------------------------------
Yeah, that's not going to work. When you say this:
T = uint8(8);
save T;
you've just blown away your "T" and you're only saving the value 8. I
don't even know where you're saving it since you didn't give a mat
filename. Also this is not very robust code at all. For example you
don't have any check in there that the file actually exists. If it
doesn't your program will bomb. It's best to use something like

baseFileName = sprintf('B-uniform%s.ppm', y);
fullFileName = fullfile(TrainDatabasePath, baseFileName);
if exist(fullFileName, 'file')
  % Stuff to do if the file actually exists.
  img = imread(fullFileName);
  img = rgb2gray(img);
 T(1:50,j:k) = img;
  % Increment j and k.
  j = j+50;
  k = k+50;
else
  % Stuff to do if the file does not exist, for example warn the user
or log it to a file.
  warningMessage = sprintf('Warning: %s does not exist',
fullFileName);
  uiwait(warndlg(warningMessage));
end

Even then you're making assumptions, with out even checking, that your
image will have a size of 50 by 50 - again not a very robust way of
doing it. You should check those things before you just blindly blast
ahead and do them. A professional programmer would.
[rows columns numberOfColorChannels] = size(img);
if rows ~= 50 || columns ~= 50
  img = imresize(img, [50 50]);
end

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread

Contact us at files@mathworks.com