当我尝试平均jpeg 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
答案 0 :(得分:1)
您的代码的问题在于您正在将JPG作为uint8(默认)读取,然后将图像作为uint8的矩阵(0-255整数)进行数学运算。当您在org_image中读取for循环的上方和内部时,将结果转换为double:org_image = double(imread(filename))
。完成平均后,需要将其强制转换为avg_image = uint8(sum_image/(len-2))
。
当你使用uint8进行数学运算时,由于小数被截断,因此分割很麻烦。当两个都是双打时,4除以8得到0.5。当两者都是整数时,你得到0。