How to add multiple gray images without getting white output?
Show older comments
Hello.
So, i've written short Matlab code which should take all photos from folder, and merge them.
Problem is, i don't know how to add multiple gray images without getting white output.
Currently i'm trying with multiplying images by alpha, but i need to rescale it everytime i change amount of images... Is there any other way to deal with this problem?
My code below:
clc
clear all
folder = ('C:\Users\Kerak\Desktop\MATLAB\lfw');
S = dir;
A = zeros(250, 250);
A = im2uint8(A);
ile = numel(S);
alpha = .01;
for i = 3:50
if(S(i).isdir)
cd (S(i).name);
Foty = dir;
ilefot = numel(Foty);
for j = 3:ilefot
image = imread(fullfile(folder, S(i).name, Foty(j).name));
image = rgb2gray(image);
A = A + image*alpha;
end
cd ('..');
end
end
imshow(A);
Any ideas? Thanks
Answers (1)
Image Analyst
on 17 Apr 2016
Don't use image for a variable name since it's the name of a very important built in function. That could be one reason why it doesn't work (but it's not). The reason it doesn't work is that A is a double matrix and all the values are > 1. If imshow() gets a double matrix, it expects it (rightly or wrongly) to be in the range 0-1. To fix, add [] after the (badly-named) A in imshow(). Call A "sumImage" to be more descriptive. To fix:
fullFileName = fullfile(folder, S(i).name, Foty(j).name);
thisImage = double(imread(fullFileName));
[rows, columns, numberOfColorChannels] = size(thisImage);
if numberOfColorChannels > 1 % Only call rgb2gray if it's really RGB, otherwise you'll get an error.
thisImage = rgb2gray(image);
end
if rows == size(sumImage, 1) && columns == size(sumImage, 2)
sumImage = sumImage + thisImage*alpha;
else
message = 'Size mismatch';
uiwait(errordlg(message));
end
After the loop, use
imshow(sumImage, []);
Categories
Find more on Blue in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!