问题描述
我想将truecolor图像转换为双精度,据我所知有两种方法可以做到:
I want to convert a truecolor image to double precision, and as far as I know there are two ways to do that:
double(rgb_img);
-
im2double(rgb_img);
哪个效率更高?
谢谢!
推荐答案
下面是一个基准测试代码,用于将 double
与 im2double
进行比较,即使它们不一定产生相同的结果其他解决方案中说明的结果-
Here's a benchmarking code to compare double
against im2double
even though they don't necessarily produce the same results as explained in the other solution(s) -
N_arr = [100 200 500 1000 2000 4000]; %// datasize array
timeall = zeros(4,numel(N_arr));
for k1 = 1:numel(N_arr)
rgb_img = uint8(randi([0 255],N_arr(k1))); %// Input to functions
f = @() double(rgb_img);
timeall(1,k1) = timeit(f);
clear f
f = @() im2double(rgb_img);
timeall(2,k1) = timeit(f);
clear f
end
figure,hold on,grid on
plot(N_arr,timeall(1,:),'-ro'),plot(N_arr,timeall(2,:),'-kx')
legend('DOUBLE','IM2DOUBLE'),
xlabel('Datasize ->'),ylabel('Time(sec) ->')
结果-
现在,这是有道理的,因为在内部 im2double
调用 double
.
Now, this makes sense because internally im2double
calls double
.
现在,假设您要处理 uint8
图像,要具有与 double
相同的功能,则需要在以后缩小比例.因此,您需要相应地编辑功能句柄-
Now, assuming you are dealing with uint8
images, to have the same functionality with double
, you need to scale it down afterwards. So you need to edit the function handle accordingly -
f = @() double(rgb_img)./255;
当时的情节结果是-
所以,现在看来差别不大.
So, apparently not much of a difference now.
这篇关于在Matlab中转换数据类型的有效方法(double与im2double)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!