How to efficiently write an alternate code for the below given code?

1 view (last 30 days)
I have a piece of code, which reads 100000 images, convert it to grayscale and find the histogram of each and finally concatenates all histograms columnwise. My question is how to rewrite this code so as to maximize the speed and use very less memory? Is there any option at all. I am a beginner. The code is given below:
histo =[];
for k = 1:100000
jpgFilename = strcat(num2str(k), '.jpg');
imageData = imread(jpgFilename);
imageGray = rgb2gray(imageData);
histo = cat(2,histo,imhist(imageGray));
end

Answers (1)

Joseph Cheng
Joseph Cheng on 2 Apr 2014
Edited: Joseph Cheng on 2 Apr 2014
If you know the total size of histo, pre-allocating it should make it run faster.
histo = zeros(N,100000) %where N = number of bins in your histogram.
Additionally writing to rows and writing to columns are not the same. http://www.matlabtips.com/columns-and-rows-are-not-the-same/
If you need to use less memory, perhaps appending to a csv file using dlmwrite(), such that you get 100000xN table of values. It may not be faster (haven't tested) however you wouldn't be generating/keeping a large variable histo until you need to use it.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!