首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在下面的MATLAB代码中,单精度格式的应用是什么?

问在下面的MATLAB代码中,单精度格式的应用是什么?
EN

Stack Overflow用户
提问于 2022-07-02 16:12:27
回答 1查看 46关注 0票数 2

我正在模仿下面的MATLAB代码。实际上,该代码实现了人脸识别的特征脸方法。

代码语言:javascript
复制
%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');

你知道为什么它使用单精度格式吗?这种格式的应用程序是什么?据我所知,单精度格式和双精度格式的区别在于用于指数、尾数等的位数。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-07-02 19:13:48

loaded_Image、rest_of_the_images、random_Image等都存储为uint8类型,以节省空间,因为像素值从[0 0 0] (黑色)到[255 255 255] (白色)不等,8位就足够了(0:255)。但整数类不支持某些数学操作。例如,如果我试图将两个uint8矩阵相乘,就会得到以下错误:

代码语言:javascript
复制
>> uint8(ones(5)) * uint8(ones(5))
Error using  * 
MTIMES (*) is not fully supported for integer classes. 
At least one argument must be scalar.

同样适用于eig

代码语言:javascript
复制
>> eig(uint8(ones(5)))
Error using eig
Invalid data type. Input matrix must be double or single.

因此,必须在操作之前转换为受支持的数据类型,并在存储映像时将其重新转换回uint8。选择single是因为与double相比,它节省了50%的内存,但是在现代体系结构中,速度差异可以忽略不计。

然而,作者们做出了不必要的转变。例如,可以在不进行任何转换的情况下编写以下行:

代码语言:javascript
复制
mean_Removed = rest_of_the_images - uint8(single(mean_value)*single(white_Image))
% could be written
mean_Removed = rest_of_the_images - mean_value

在处理诸如GPU这样的特定体系结构时,single将是正确的选择,因为GPU完全支持single数据类型,但还不支持double的数据类型。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72840590

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档