How to efficiently write an alternate code for the below given code?
1 view (last 30 days)
Show older comments
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
0 Comments
Answers (1)
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.
0 Comments
See Also
Categories
Find more on Image Processing Toolbox in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!