How to add multiple gray images without getting white output?

조회 수: 6 (최근 30일)
Luq Ops
Luq Ops 2016년 4월 17일
댓글: Luq Ops 2016년 4월 17일
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

답변 (1개)

Image Analyst
Image Analyst 2016년 4월 17일
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, []);
  댓글 수: 1
Luq Ops
Luq Ops 2016년 4월 17일
Thanks for fast answer.
Yes, these variable names are poor, sorry for that.
Unfortunately, i've wrongly described problem. What i meant is that when i'm adding gray images, the output IMAGE is white, because addition. That's why there's alpha variable, to nullify that problem, and it's working.
But now, i need to manually change alpha variable everytime when i'm changing loop range.
For example alpha = 0.01 works for loop of range 3:50, but when i increase loop range to 3:150 i need alpha = 0.005.
Is there any way to merge many gray images without getting white image as a output?
I've forgotten mention that i'm using "Faces in the wild", which is database of face images and this code is creating face template from these.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by