Issue with averaging images in matlab -
when try average folder of jpeg matlab images, blank image. i've gone on code million times, , don't know i'm going wrong. (also know hard coded of numbers because wanted take specific folder , i've double checked million times, they're right.)
%takes images in folder , averages 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 = 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 fits file , displays imwrite(avg_image, 'averagefile.jpg'); read_image = imread('averagefile.jpg'); imshow(read_image) end
the problem code reading in jpgs uint8 (default) , doing math images matrices of uint8's (0-255 integers). read in org_image, above , inside loop, cast result double:org_image = double(imread(filename))
. after you're done averaging, need cast back, avg_image = uint8(sum_image/(len-2))
.
when math uint8's, divisions messy since decimals truncated. 4 divided 8 when both doubles gives 0.5. when both integers, 0.
Comments
Post a Comment