Merge Two images with different channels ( R,G,B ) using C# or MATLAB
How to merge ( overlay ) two same images with different effects into one image. I have one image like natural.jpg , I added some effects into that natural.jpg image Now I want to merge that two same image into one image like OutNatural.jpg i开发者_C百科mage.
How can I achieve this in C# language or Matlab.. if any other way then reply me..
Thanks
Mathematically, you can get the desired result by averaging the two images. If you want to emphasize one image or the other, you can use a weighted average. In MATLAB:
function imgC = AverageImages(imgA, imgB, weight)
%# AverageImages - average two input images according to a specified weight.
%# The two input images must be of the same size and data type.
if weight < 0 || weight > 1
error('Weight out of range.')
end
c = class(imgA);
if strcmp(c, class(imgB)) != 1
error('Images should be of the same datatype.')
end
%# Use double matrices for averaging so we don't lose a bit
x = double(imgA);
y = double(imgB);
z = weight*x + (1-weight)*y;
imgC = cast(z, c); %# return the same datatype as the input images
The actual averaging takes place in the line z = weight*x + (1-weight)*y;
If you specify weight = 0.5
, the output image will be an equal mix of the two inputs. If you specify 0.9
, the output will be 90% imgA
and 10% imgB
.
The reason for casting the input images to a double data type is because images are usually uint8
, which is limited to 0..255. The simple math here should not run into any problems with this data type, but it's a good habit to do your math in floating-point and then cast back to the image's desired data type.
For aesthetic reasons, you might want to average different areas of the images with different weights. You can extend the function to accept either a scalar or a 2D matrix for weight
. In the latter case, you just multiply each pixel by the corresponding entry in the weight
matrix.
As shown by @mtrw, one way to merge the images is using a linear combination of the two. The Image Processing Toolbox for MATLAB provide two functions for this purpose, namely IMADD and IMLINCOMB (Unfortunately the Image package for Octave have no similar functions, though they are not hard to implement by yourself).
One advantage of MATLAB's implementation is that on Intel architecture processors, they use the IPP library to speed up execution.
精彩评论