Find the average of two images
Show older comments
Hello,
I have these two dark images and I was wondering how to get the average of them.
Thanks!
Accepted Answer
More Answers (1)
5 Comments
You're taking the truncated average of row subscripts into two different color tables. The result is a nonsense number. The given images are forgiving in that the result appears sensible, but this answer is generally very wrong.
Consider the two images:

% read only the index arrays
A = imread('A.gif');
B = imread('B.gif');
% average the indices, allowing truncation
C = (A+B)/2;
% display the average of the indices
imagesc(C)
colormap(gray)
Even if the truncation were avoided, the result is still nonsense. This isn't the average of gray/color values. It's the average of array subscripts referencing two different arrays.
% average the indices
C = (double(A)+double(B))/2;
% display the average of the indices
imagesc(C)
colormap(gray)
@DGM thanks for edifying me on this topic. Obviously there is a lot more to this than I realized. @Marcus Johnson Maybe change your accepted answer to @DGM's
DGM
on 16 Nov 2023
Half of this is keeping track of trivia, like what to expect of GIF files, and the default output class of certain operations. I had originally posted as a comment hoping you could update and get credit, but my comment got moved and well, this is how it wound up.
Jon
on 17 Nov 2023
@DGM- Thanks for the thoughtful followup. No problem, on crediting the answer, I'm just trying to help when I can, but also learn, and I learned a lot here. Also made me realize I should be a little more careful when jumping into things I'm not well versed in. I do occassional image processing but usually monchrome .png's with simple pixel intensity stored in the array. I definitely should have remembered that these are returned as integers not doubles, and so they should be converted to doubles before, for example computing an average. I hadn't been aware of the further complexities of .gif files and indices to color maps. Thanks for helping me learn more.
As far as I'm concerned, I appreciate sincere motivations a lot more these days. There's plenty of room for answers that aren't ideal, so long as there's also room to improve. I'm not exactly careful myself :D
The canonical workflows suggested by the form of things in MATLAB/IPT leaves plenty of room for these sorts of problems with numeric class magically changing or causing incompatibility with mixed-class operations. MIMT might be a giant hobbyist embarrassment, but at least it's working toward providing class and depth-agnostic workflows in some ways. As I mentioned to IA, the way I would've done the averaging using imblend() wouldn't need to know the class of either input.
Categories
Find more on Images in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!




