簡體   English   中英

以下 MATLAB 代碼中單精度格式的應用是什么?

[英]What is the application of single-precision format in the following MATLAB code?

我正在模仿以下 MATLAB 代碼。 實際上,這段代碼實現了人臉識別的 eigenface 方法。

%You are free to use, modify or distribute this code
loaded_Image=load_img();
random_Index=round(400*rand(1,1));          
random_Image=loaded_Image(:,random_Index);                         
rest_of_the_images=loaded_Image(:,[1:random_Index-1 random_Index+1:end]);         
image_Signature=20;                            
white_Image=uint8(ones(1,size(rest_of_the_images,2)));
mean_value=uint8(mean(rest_of_the_images,2));                
mean_Removed=rest_of_the_images-uint8(single(mean_value)*single(white_Image)); 
L=single(mean_Removed)'*single(mean_Removed);
[V,D]=eig(L);
V=single(mean_Removed)*V;
V=V(:,end:-1:end-(image_Signature-1));          
all_image_Signatire=zeros(size(rest_of_the_images,2),image_Signature);
for i=1:size(rest_of_the_images,2);
    all_image_Signatire(i,:)=single(mean_Removed(:,i))'*V;  
end
subplot(121);
imshow(reshape(random_Image,112,92));
title('Looking for this Face','FontWeight','bold','Fontsize',16,'color','red');
subplot(122);
p=random_Image-mean_value;
s=single(p)'*V;
z=[];
for i=1:size(rest_of_the_images,2)
    z=[z,norm(all_image_Signatire(i,:)-s,2)];
    if(rem(i,20)==0),imshow(reshape(rest_of_the_images(:,i),112,92)),end;
    drawnow;
end
[a,i]=min(z);
subplot(122);
imshow(reshape(rest_of_the_images(:,i),112,92));
title('Recognition Completed','FontWeight','bold','Fontsize',16,'color','red');

你知道為什么它使用單精度格式嗎? 這種格式的應用是什么? 據我所知,單精度格式和雙精度格式的區別在於用於指數、尾數等的位數。

loaded_Imagerest_of_the_imagesrandom_Image等都存儲為uint8類型以節省空間,因為像素值的范圍從[0 0 0] (黑色)到[255 255 255] (白色)並且 8 位就足夠了(0 :255)。 但是整數類不支持某些數學運算。 例如,如果我嘗試將兩個uint8矩陣相乘,則會出現以下錯誤:

>> uint8(ones(5)) * uint8(ones(5))
Error using  * 
MTIMES (*) is not fully supported for integer classes. 
At least one argument must be scalar.

eig相同:

>> eig(uint8(ones(5)))
Error using eig
Invalid data type. Input matrix must be double or single.

因此,必須在操作之前轉換為支持的數據類型,並在存儲圖像時重新轉換回uint8 作者選擇single是因為它比double節省了 50% 的內存,但在現代架構上速度差異可以忽略不計。

不過,作者進行了不必要的轉換。 例如,可以編寫以下行而不進行任何轉換:

mean_Removed = rest_of_the_images - uint8(single(mean_value)*single(white_Image))
% could be written
mean_Removed = rest_of_the_images - mean_value

其他可以選擇single的地方是在處理特定架構(如 GPU)時,因為 GPU 完全支持single數據類型,但還不支持double

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM