Placing greyscale .jpg images into a matrix

  • Context: LaTeX 
  • Thread starter Thread starter Larry Gopnik
  • Start date Start date
  • Tags Tags
    Images Matrix
Click For Summary

Discussion Overview

The discussion revolves around the challenges of loading a series of greyscale .jpg images into a 3D matrix in MATLAB for analysis of image intensities across different wavelengths. Participants explore various methods and troubleshoot code errors related to image reading functions.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant describes their attempt to create a 3D matrix of size 1550x1000x10 for image analysis but encounters errors with the imread function, suggesting a possible misunderstanding of file types.
  • Another participant suggests using imread() instead of Tiff() for .jpg images and questions the purpose of additional string arguments ('r' and 'l') in the imread function, proposing that they may not be necessary.
  • A different participant mentions that the original creator of the jpgs indicated they were uncompressed, which could lead to confusion regarding file handling.
  • One participant points out that there is an uncompressed jpeg option (jpeg2000) and suggests checking the number of bytes to determine if the images are compressed.
  • There is a question about whether removing the additional arguments from the imread function resolved the initial error.

Areas of Agreement / Disagreement

Participants generally agree on the use of imread() for .jpg images, but there are differing views on the necessity of certain input arguments and the implications of image compression. The discussion remains unresolved regarding the best approach to handle the image loading errors.

Contextual Notes

Participants express uncertainty about the correct usage of MATLAB functions for image reading and the implications of image compression on their analysis. There are also unresolved questions about the specific requirements for loading images into a matrix.

Larry Gopnik
Gold Member
Messages
34
Reaction score
19
Hi, I hope someone can help me!

I could not find a solution online of which could help me. My problem:

I have imagecubes - they are a "cube" of 10 images of the same place of a photo, one at 400nm, one at 450nm etc etc.

I need to upload these into MATLAB so I can then analyse the intensities at certain places in certain wavelengths. The best way I have thought of doing this is to place it into a 3d matrix of size 1550x1000x10 (where 1550x1000 are the size of each image) (if there is a better way of doing this I would be glad to hear them)

Code:
Code:
%-----------------------------%
%----IMAGE CUBE------%
%-----------------------------%
sizex = 1550;         
sizey = 1000;

imageslice = 10;                %amount of slices the image cube is made of
cube=zeros(sizex,sizey,imageslice);
cubedir = 'C:\proc';
ab = [1 2 3 4 5 6 7 8 9 10];
for ij = 1:imageslice
 
   slice = ab(ij);
    slice1 = 1;
    slice2 = slice-slice1;
    slicenumber = int2str(slice2);
    filepath=[cubedir, '1_190716.f' slicenumber '.0.0.jpg'];
    fidr = imread(filepath,'r','l');
    cube{:,:,ij} = fread(fidr,[sizex,sizey],'uint8');
 
 
end

I'm getting the error:

Code:
Error using readjpg
Too many input arguments.

Error in imread (line 415)
    [X, map] = feval(fmt_s.read, filename, extraArgs{:});

I've looked online and the people who normally have this error are using tiff files. Where am I going wrong?!

Thank you

EDIT:

I now have a different error.

After contemplation I saw that maybe MatLab thinks the files are .tiff files, therefore I have changed the filetypes and hence the code to:

Code:
%code code code

   slice = ab(ij);
    slice1 = 1;
    slice2 = slice-slice1;
    slicenumber = int2str(slice2);
    filepath=[cubedir, '_190716.f' slicenumber '.0.0.tiff'];
    fidr = Tiff('open',filepath,'r','l');
    cube(:,:,ij) = fread(fidr,[sizex,sizey],'uint8');
  
%more code

and I am now getting the error :

Error using Tiff
Too many input arguments.

Error in Build_1 (line 107)
fidr = Tiff('open',filepath,'r','l');
 
Last edited:
Physics news on Phys.org
I have a hyperspectral app that uses 30 wavelength PNG images. Using a 3D image is fine - just depends on what you want to do and how you want to do it. Or you might just process the images one image at a time without building a 3-D image in advance.

You should use imread(), not Tiff() if they're jpg images. However imread() does not take string arguments 'r' and 'l' as far as I can tell from looking at the help. Just what do you think those arguments were supposed to be for? Try it giving just the filename and not those other two input arguments.
 
  • Like
Likes   Reactions: Larry Gopnik
Image Analyst said:
I have a hyperspectral app that uses 30 wavelength PNG images. Using a 3D image is fine - just depends on what you want to do and how you want to do it. Or you might just process the images one image at a time without building a 3-D image in advance.

You should use imread(), not Tiff() if they're jpg images. However imread() does not take string arguments 'r' and 'l' as far as I can tell from looking at the help. Just what do you think those arguments were supposed to be for? Try it giving just the filename and not those other two input arguments.

I had talked to the person who had created the jpgs and they said that they used no compression at all in them so it was possible that the images were being treated like Tiffs hence the edit.

As for the other arguments in the line - I had used the 'r', 'l' as that's what I used when uploading some .raw files earlier on in my program (I have never done image analysis before in MATLAB so the upload of many jpgs into a matrix is all new to me) so I just assumed that I'd need that for imread too - what a silly error!Thank you
 
Larry, there is an uncompressed jpeg option. The format is jpeg2000 with an extension of j2k. Type imformats on the command line to see what formats your version of MATLAB can read in.

To see if it's compressed, see what numel(rgbImage) is - that's how many pixels in all color channels - and then multiply by the number of bytes - 1 for uint8 and 2 for uint16. Then see how many bytes it is on disk with Windows File Manager or something. If it's uncompressed, then they will be the same number of bytes.

Anyway, did imread() work once you took out the other two arguments?
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
7K
  • · Replies 7 ·
Replies
7
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
15K