필터 지우기
필터 지우기

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

조회 수: 1 (최근 30일)
DEVANAND
DEVANAND 2014년 4월 2일
편집: Joseph Cheng 2014년 4월 2일
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.

Community Treasure Hunt

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

Start Hunting!

Translated by