在matlab中对图像进行平均的问题
问题描述:
当我尝试对jpeg matlab图像的文件夹进行平均时,我所得到的全部都是空白图像。我已经完成了一百万次我的代码,并且我不知道我在哪里出错 。 (我也知道我硬编码的一些数字,但只是因为我想它需要一个特定的文件夹,我仔细检查过那些上百万次,他们是对的。)在matlab中对图像进行平均的问题
%takes all the images in a folder and averages their values
%opens folder
function avg_image = average_images()
folder_name = uigetdir;
folder_directory = dir(folder_name);
filename = folder_directory(3).name;
len = length(folder_directory);
org_image = imread(filename);
sum_image = org_image;
%adds files together
for i = 4:len
filename = folder_directory(i).name;
org_image = imread(filename);
sum_image = sum_image + org_image;
end
%calculates average
avg_image = sum_image/(len-2);
%saves average as a fits file and displays it
imwrite(avg_image, 'averagefile.jpg');
read_image = imread('averagefile.jpg');
imshow(read_image)
end
答
的问题你的代码是你正在用JPG格式读取uint8(默认),然后用图像作为uint8(0-255整数)的矩阵进行数学运算。正如您在org_image中读取的,在for循环的上面和内部,将结果转换为双精度:org_image = double(imread(filename))
。平均完成后,您需要将其恢复,avg_image = uint8(sum_image/(len-2))
。
当你使用uint8进行数学运算时,由于小数点被截断,所以部分是混乱的。当双方都是双打时,4除以8会得到0.5。当两者都是整数时,您得到0.