[英]Multiply two images in C# as multiply two layers in Photoshop
我有两个图像,我想在C#中将这两个图像相乘在一起,因为我们在Photoshop中相乘了两个图层。
I have found the method by which the layers are multiplied in photoshop or any other application. 我发现了在photoshop或任何其他应用程序中将图层相乘的方法。
Following is the formula that I have found on GIMP documentation. 以下是我在GIMP文档中找到的公式。 It says that
它说
E=(M*I)/255 E =(M * I)/ 255
where M and I are the color component(R,G,B) values of the two layers. 其中M和I是两层的颜色分量(R,G,B)值。 We have to apply this to every color component.
我们必须将此应用于每个颜色组件。 E will be the resultant value for that color component.
E将是该颜色分量的结果值。
If the color component values are >255 then it should be set to white ie 255 and if it is <0 then it should be set as Black ie 0 如果颜色分量值> 255,则应将其设置为白色,即255;如果颜色值<0,则应将其设置为黑色,即0
Here I have a suggestion - I didn't test it, so sorry for any errors - I'm also assuming that both images have the same size and are greylevel. 在这里,我有一个建议-我没有测试过,很抱歉出现任何错误-我还假设两个图像的大小相同且为灰度级。
Basically I'm multiplying the image A for the relative pixel percentage of image B. 基本上,我将图像A乘以图像B的相对像素百分比。
You can try different formulas like: 您可以尝试不同的公式,例如:
int result = ptrB[0] * ( (ptrA[0] / 255) + 1); int结果= ptrB [0] *((ptrA [0] / 255)+1); or int result = (ptrB[0] * ptrA[0]) / 255;
或int结果=(ptrB [0] * ptrA [0])/ 255;
Never forget to check for overflow (above 255) 永远不要忘记检查溢出(超过255)
public void Multiply(Bitmap srcA, Bitmap srcB, Rectangle roi)
{
BitmapData dataA = SetImageToProcess(srcA, roi);
BitmapData dataB = SetImageToProcess(srcB, roi);
int width = dataA.Width;
int height = dataA.Height;
int offset = dataA.Stride - width;
unsafe
{
byte* ptrA = (byte*)dataA.Scan0;
byte* ptrB = (byte*)dataB.Scan0;
for (int y = 0; y < height; ++y)
{
for (int x = 0; x < width; ++x, ++ptrA, ++ptrB)
{
int result = ptrA[0] * ( (ptrB[0] / 255) + 1);
ptrA[0] = result > 255 ? 255 : (byte)result;
}
ptrA += offset;
ptrB += offset;
}
}
srcA.UnlockBits(dataA);
srcB.UnlockBits(dataB);
}
static public BitmapData SetImageToProcess(Bitmap image, Rectangle roi)
{
if (image != null)
return image.LockBits(
roi,
ImageLockMode.ReadWrite,
image.PixelFormat);
return null;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.