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

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.

Related

How to test cntk object detection example on custom image?

I am trying to run CNTK object detecion example on PascalVoc pretrained dataset. I run all required scripts in fastrcnn and get the visual output for the test data defined in dataset. Now I want to test network on my own image, how can I do that?
For Fast R-CNN you need a library that generates candidate ROIs (regions of interest) for your test images, e.g. selective search.
If you want to evaluate a batch of images you can follow the description in the tutorial to generate the test mapping file and the ROI coordinates (see test.txt and test.rois.txt in the corresponding proc sub folder). If you want to evaluate a single you would need to pass the image and the candidate ROI coordinates as inputs to cntk eval, similar to this example:
# compute model output
arguments = {loaded_model.arguments[0]: [hwc_format]}
output = loaded_model.eval(arguments)
For FastRCNN you need to first run your custom image through Selective Search algorithm to generate ROIs (regions of interest) and then feed it to your model with sth like this:
output = frcn_eval.eval({image_input: image_file, roi_proposals: roi_proposals})
You can find more details here: https://github.com/Microsoft/CNTK/tree/release/latest/Examples/Image/Detection/FastRCNN
Anyway FastRCNN is not the most efficient way to do it because of usage of Selective Search (which is a real bottleneck here). If you want to improve the performance you can try FasterRCNN as it gets rid of SS algorithm and replaces it with Region Proposal Network which performs much, much better.
If you're interested, you can check my repo on GitHub: https://github.com/karolzak/CNTK-Hotel-pictures-classificator

NIfTI spatial position/orientation changes after using NIfTI_tools

I'm using a particular toolbox (Tools for NIfTI and Analyze image) to process NIfTI files with MATLAB, but when I do a simple value modification, the spatial information associated with the image is somehow changed. I can't directly compare the before and after images because they have different origins and/or some slight translation. I did not (intentionally) modify the NIfTI header information. In fact, I used only three commands:
matlab_nii = load_nii('original.nii');
matlab_nii.img(matlab.nii.img > 10) = NaN;
save_nii(matlab_nii, 'new.nii');
Despite not editing any of the header information, only modifying the image values directly, the new NIfTI file has differing spatial properties, making it impossible to compare directly to the original, to verify that, indeed, the modifications took place.
I'm hoping someone has had experience with these tools and has encountered this issue.
Found the solution in one of the answers to this question about NIfTI/MATLAB.
The answer is to use load_untouch_nii instead of load_nii and save_untouch_nii instead of save_nii.
The functions I used initially will apply affine transformation data (if it exists, which apparently it did) to the image. Using these functions results in a direct spatial association between voxels.

How to extract image features using caffe matlab wrapper?

Does anyone use the matlab wrapper for the caffe framework? Is there a way how to extract an 4096 dimensional feature vector from an image?
I was already following
https://github.com/BVLC/caffe/issues/432
and also tried to remove the last lines in imagenet_deploy.prototxt to remove layers as suggested in another forum on github.
But still when I run "matcaffe_demo(im, 1)" I only get a 1000 dim vector of scores (for the image net classes).
Any help would be appreciated
Kind regards
It seems that you might not be calling the correct prototxt file. If the last layer defined in the prototxt has the top blob of 4096 dimension, there is no way for the output to be 1000 dimension.
To be sure, try creating a bug in the prototxt file and see whether the program crashes. If it doesn't then the program indeed is reading some other prototxt file.

Need a method to store a lot of data in Matlab

I've asked this before, but I feel I wasn't clear enough so I'll try again.
I am running a network simulation, and I have several hundreds output files. Each file holds the simulation's test result for different parameters.
There are 5 different parameters and 16 different tests for each simulation. I need a method to store all this information (and again, there's a lot of it) in Matlab with the purpose of plotting graphs using a script. suppose the script input is parameter_1 and test_2, so I get a graph where parameter_1 is the X axis and test_2 is the Y axis.
My problem is that I'm not quite familier to Matlab, and I need to be directed so it doesn't take me forever (I'm short on time).
How do I store this information in Matlab? I was thinking of two options:
Each output file is imported separately to a different variable (matrix)
All output files are merged to one output file and imprted together. In the resulted matrix each line is a different output file, and each column is a different test. Problem is, I don't know how to store the simulation parameters
Edit: maybe I can use a dataset?
So, I would appreciate any suggestion of how to store the information, and what functions might help me fetch the only the data I need.
If you're still looking to give matlab a try with this problem, you can iterate through all the files and import them one by one. You can create a list of the contents of a folder with the function
ls(name)
and you can import data like this:
A = importdata(filename)
if your data is in txt files, you should consider this Prev Q
A good strategy to avoid cluttering your workspace is to import them all into a single matrix. SO if you have a matrix called VAR, then VAR{1,1}.{1,1} could be where you put your test results and VAR{1,1}.{2,1} could be where you put your simulation parameters of the first file. I think that is simpler than making a data structure. Just make sure you uniformly place the information in the same indexes of the arrays. You could also organize your VAR row v col by parameter vs test.
This is more along the lines of your first suggestion
Each output file is imported separately to a different variable
(matrix)
Your second suggestion seems unnecessary since you can just iterate through your files.
You can use the command save to store your data.
It is very convenient, and can store as much data as your hard disk can bear.
The documentation is there:
http://www.mathworks.fr/help/techdoc/ref/save.html
Describe the format of text files. Because if it has a systematic format then you can use dlmread or similar commands in matlab and read the text file in a matrix. From there, you can plot easily. If you try to do it in excel, it will be much slower than reading from a text file. If speed is an issue for you, I suggest that you don't go for Excel.

Using HDD memory for the MATLAB

As in my previous question I have the following problem. I have a matrix P nxn which elements are matrices P{i,j} which are also nxn. So the total amount of elements is n^4. For n=100 there is an error about the lack of memory. I calculate this matrix only one time and then operate with it. Could you advise me, how to store matrices P{i,j} on the HDD?
I mean that maybe it is possible to store each of them in a file like "data_i_j.dat" and then load it while doing computations in a loop for i and j?
The save function will write data to a file, and the load function will read it back again. save(filename,varname,varname,varname...), followed by S = load(filename) and referring to S.varname (there's also a version of load that just dumps stuff into your current workspace, but that seems like poor practice).