【问题标题】:Issue with averaging images in matlab在matlab中平均图像的问题
【发布时间】:2015-07-21 20:58:24
【问题描述】:

当我尝试平均 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 

【问题讨论】:

    标签: image matlab average


    【解决方案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。

    【讨论】:

      猜你喜欢
      • 2012-04-21
      • 2016-10-31
      • 1970-01-01
      • 2011-01-27
      • 2018-06-08
      • 1970-01-01
      • 2014-04-24
      • 1970-01-01
      • 2017-02-19
      相关资源
      最近更新 更多