Labfans是一个针对大学生、工程师和科研工作者的技术社区。 | 论坛首页 | 联系我们(Contact Us) |
![]() |
|
![]() |
#1 |
高级会员
注册日期: 2019-11-21
帖子: 3,006
声望力: 66 ![]() |
![]()
如何将具有不同效果的两个相同图像合并(叠加)到一个图像中。我有一个图像,例如natural.jpg,我向该natural.jpg图像中添加了一些效果,现在我想将两个相同的图像合并到一个图像中,例如OutNatural.jpg图像。
我怎样才能用C#语言或Matlab来实现这一目标。 谢谢 回答: 从数学上讲,您可以通过平均两个图像来获得所需的结果。如果要强调一幅图像或另一幅图像,则可以使用加权平均值。在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 实际的平均值发生在z = weight*x + (1-weight)*y;如果您指定weight = 0.5 ,则输出图像将是两个输入的均等混合。如果指定0.9 ,则输出将为90% imgA和10% imgB 。 将输入图像转换为双uint8数据类型的原因是因为图像通常是uint8 ,其限制为0..255。这里的简单数学运算不会对此数据类型造成任何问题,但是,这是一个好习惯,即以浮点数进行数学运算,然后将其转换回图像所需的数据类型。 出于美学原因,您可能希望对具有不同权重的图像的不同区域求平均。您可以扩展该函数以接受weight的标量或2D矩阵。在后一种情况下,您只需将每个像素乘以weight矩阵中的相应条目即可。 更多&回答... |
![]() |
![]() |