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

1 次查看(过去 30 天)
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

回答(1 个)

Joseph Cheng
Joseph Cheng 2014-4-2
编辑:Joseph Cheng 2014-4-2
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.

类别

Help CenterFile Exchange 中查找有关 Image Processing Toolbox 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by