How to read a large image with tiff format in Matlab? - matlab

How to read a large image with tiff format in Matlab?
I am trying to read an image with the size of 1.970.654 k in matab and later on I would like to crop the image into tiles. When I use the imread code it gives me an error of :
'Out of memory. Type HELP MEMORY for your options.'
I would really appreciate it if sb could kindly help me with it.

Do you have enough memory? If you only have 2 GB of memory I doubt there is even enough memory allocated to Matlab to do that.
Also, a workaround might just be to use another editor to split the image into separate sets of TIFFs and then perform your analysis on each set.

Related

Matlab Computer Vision: How to use image in memory for imageSet?

I am currently working on a project at home and hoping to use the Computer Vision toolbox in Matlab to retrieve images from a set that match based on my query image. In fact, the example I'm using from the Matlab documentation here: Image Matching Example
The snag I keep bumping into is that it appears the imageSet class in Matlab only works on files saved to disk. Unfortunately, the work I'm doing has a 4D matrix of an image collection I've created artificially. More specifically, it has the shape (M,N,RGB,I) where
M = number of pixels in X-dir
N = number of pixels in Y-dir
RGB =
size of 3, where each channel for RGB is stored as a page
I = the
image number (up to 10,000, for example)
It seems pretty silly that I have to write everything to files for me to employ the imageSet class object.
So, the question is: Does anyone know a way to create the imageSet object (or similar) without have to write everything to a tmp dir on disk to carry out the analysis, that is, create imageSet from workspace variables?
For the life of me this one had me stumped all weekend. I know I could capitulate and write to files, but somehow that just bothers me.
Any help is greatly appreciated.
You are correct, imageSet only stores file names, and gives you a read method to read a particular image from disk.
In general, if you already have the images in memory, you can simply store them in a cell array. Or, if your images are all the same size, you can keep them in a single multi-dimensional array.
However, in this particular case, you are using bagOfFeatures, which currently only takes imageSet. So you will have to save your images to files.

error reading a large binary file in MATLAB

I have to read in a large Binary file whose size is 92,504 KB. When I am using fread command MATLAB is giving me error:
Error using fread Out of memory. Type HELP MEMORY for your options.
I tried to restart MATLAB also so that if I am using any virtual memory it should be cleared but still the problem persists.
How can I solve this problem of reading data.
The problem is the code that you are using to read the data:
[data,count] = fread(fid,'uint8');
The above line tells matlab to read in as many uint8s as it can and put them into a vector.
The trouble is that matlab will put it into a vector of doubles. So rather than a vector where each element is one byte, you have a vector where each element is 8 bytes. This ends up making your 92Mb of data take up 92*8 = 736mb which is probably going to be bigger than the maximum possible array size shown by the memory command.
The solution here is to tell matlab to put the data you are reading into a vector of uint8 which can be achieved as follows:
[data,count] = fread(fid,'*uint8');
This method for reading in the data tells matlab that the output vector should be the same type as the input data. You can read more about it in the precision section of the fread documentation.
In a 32-bit system, you may have very less memory available to MATLAB. The fread command you are using reads the entire file at once. This is probably a bad idea, since you system is not having enough memory. A better way to implement would be to read file part by part. See,
A = fread(fileID, sizeA)
in link below[1]. You can put this code inside a loop. In case you want to read whole file at once, what i would recommend is to use a 64-bit system with 3GB RAM.
[1] http://www.mathworks.in/help/matlab/ref/fread.html

Why is .mat files in matlab less than the actual variable in size?

I feel that my question isn't clear. I will explain that.
im = dicomread('image.dcm');
whos im
Name Size Bytes Class Attributes
im 2294x1914 8781432 uint16
notice that im reserved 8,781,432 bytes in memory.
On the other side, if I executed the next line.
save('im.mat','im');
im.mat will reserve 6,245,906 bytes in memory
the question is why is that? Are matlab compressing before saving .mat files?
thank you.
Yes it does. The official documentation says that:
Beginning with Version 7, MATLAB compresses data when writing to MAT-files to save storage space.
I guess your MATLAB version is 7 or higher.
Yes, Matlab compresses .mat files.
Here is an explanation about it: http://www.mathworks.com.au/support/solutions/en/data/1-PM5NN/index.html?product=ML&solution=1-PM5NN

How big image can be read and displayed in matlab?

I have a problem with image reading. I want to make sure how big image can be read and displayed in matlab? It is possible to display huge images like (12689,4562,7). If not, how can I check whether this image loaded correctly in matlab?
Thanks a lot
There are two questions here:
Is it possible to load a large image from the disk to RAM?
Is it possible to show a large image?
The answer to the first question is that it depends on your amount of RAM and operating system. The answer to the second question is that Matlab (or any program) downscales the image before showing, since there aren't that much pixels on the image. So it depends on the internal algorithm, and again, on your amount of RAM.
The number of MB of RAM required for such an image would be (assuming 8 bits/pixel (uint8)):
12689*4562*7 / 1e6 = 405.2 MB
The number of elements a single matrix can contain in your version of Matlab:
[~, numEls] = computer;
which is 2.147483647e+09 on my 32-bit R2010b. This is much more than 12689*4562*7, so in principle, if you have 406GB of unused RAM, you should be able to load the image in its entirety into RAM. And in principle, displaying said image will involve some additional RAM (and probably take a looong time), but should nevertheless be possible (aside from the fact that displaying an image with 7 colour layers is not very standard AFAIK).

An error while importing an image as a variable into Matlab

I wanted to import a tif image into Matlab workspace as a variable using File/import data tool. But I got the following error "Warning: The datatype for tag SamplesPerPixel should be TIFF_SHORT instead of TIFF_LONG. This may cause data corruption". The image type is float single, 32 bit. and size is really big (4144,12619,7). Can matlab read and display such an image. What does this error mean? and how can I correct it?
Thank you so much
Read the TIFF specification.
From the warning message it appears there's some problem with the format chosen. When reading a TIFF file, each IFD has many entries, one of them being SamplesPerPixel (see page 24 of the specification). This should be of type SHORT (see page 15 for the list of types and what they are). However, apparently, you have type LONG there. That seems to be causing problem. Either matlab is identifying it incorrectly, or the software you used to save the image is not following the TIFF specification.