Vectorize a per-pixel classification
古いコメントを表示
Hi, I am using the following code to calculate the likelihood of every pixel x in a RGB image, belonging to a given class;
for count=1:height
for count2=1:width
%pixel vector x to be classified
x=[image(count,count2,1);image(count,count2,2);image(count,count2,3)];
%for each class
for count3=1:noOfClasses
detZ = pDet(count3);
invZ = pInv(:,:,count3);
y=(pMean(count3,:)');
%calculate the likelihood
post= 1/sqrt((2*pi)^2*detZ) * exp(-(x(:,1)-y)'*invZ*(x(:,1)-y)/2);
......
The application is classification of video frames and so the current approach using for loops is impractical as it is very slow.
Any suggestions?
thanks, John
採用された回答
その他の回答 (1 件)
Jan
2011 年 4 月 12 日
Move as many operations as possible out of the loops:
C1 = 1 ./ sqrt((2*pi)^2 * pDet);
pMeanT = transpose(pMean);
for count=1:height
for count2=1:width
x = reshape(image(count, count2, :), 3, 1);
for count3=1:noOfClasses
invZ = pInv(:,:,count3);
y = pMeanT(:, count3);
C2 = x - y;
post = C1(count3) * exp(-C2' * invZ * C2 * 0.5);
I do not expect a dramatic acceleration, but this is a very general method which works in all programming languages.
カテゴリ
ヘルプ センター および File Exchange で Image Processing Toolbox についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!