Placing greyscale .jpg images into a matrix

In summary, the person is trying to upload 10 jpg files, each of which is 400x400 pixels, into a matrix. The first error they get is when trying to read the file, the second error is when trying to create the matrix. The person then tried imread(), which did not work because it was trying to use Tiff(), which does not take string arguments 'r' and 'l' as far as the person can tell. The person then tried imformats, which told them that their version of MATLAB could read in jpeg2000 files, which are uncompressed.
  • #1
Larry Gopnik
Gold Member
34
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
  • #2
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 Larry Gopnik
  • #3
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
 
  • #4
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?
 

1. What is a greyscale .jpg image?

A greyscale .jpg image is an image file that only contains black, white, and various shades of grey. It does not have any other colors or hues.

2. How do you place a greyscale .jpg image into a matrix?

To place a greyscale .jpg image into a matrix, you can use software or coding languages such as MATLAB or Python. These programs have functions that allow you to read the image file and convert it into a matrix of pixels.

3. What is the benefit of placing a greyscale .jpg image into a matrix?

Converting a greyscale .jpg image into a matrix allows for easier manipulation and analysis of the image. The matrix contains numerical values for each pixel, which can be used for various mathematical operations and algorithms.

4. Can you convert a colored .jpg image into a greyscale .jpg image?

Yes, you can convert a colored .jpg image into a greyscale .jpg image using image editing software or coding. The process involves removing the color information from the image and replacing it with varying shades of grey.

5. Are there any limitations to placing greyscale .jpg images into a matrix?

One limitation is that the matrix may not accurately represent the original image if it contains complex patterns or details. Also, the size of the matrix may be limited by the memory and processing power of the software or coding language used.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
5K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
6K
  • Programming and Computer Science
Replies
4
Views
14K
Back
Top