8 bpp索引图像的图像乘法 [英] Image Multiplication of 8 bpp Indexed Image
问题描述
我有两个图像,两个图像均为8位索引,我正在尝试对它们执行以下计算:
1.将两个图像相乘
2.步骤1的结果中所有像素值的总和.
SrcBitmap1
http://i.stack.imgur.com/xQe1B.png [ http://i.stack.imgur.com/9mBHi.png [ http://i.stack.imgur.com/PFTna.png [
I have two images two images which are 8 bit indexed, I am trying to perform the following calculations on them:
1. Multiply both images
2. Total of all pixel values from result of step 1.
SrcBitmap1
http://i.stack.imgur.com/xQe1B.png[^]
SrcBitmap2
http://i.stack.imgur.com/9mBHi.png[^]
ResultBitmap:
http://i.stack.imgur.com/PFTna.png[^]
below is my code, and for some reason, I am getting color images after multiplying two gray images. Can''t figure out what is going on. Any advise is greatly appreciated.
public Bitmap MaskImage(Bitmap SrcBitmap1, Bitmap SrcBitmap2)
{
int width;
int height;
//Message = "Impossible.";
if (SrcBitmap1.Width < SrcBitmap2.Width)
width = SrcBitmap1.Width;
else
width = SrcBitmap2.Width;
if (SrcBitmap1.Height < SrcBitmap2.Height)
height = SrcBitmap1.Height;
else
height = SrcBitmap2.Height;
Bitmap bitmap = new Bitmap(width, height);
try
{
BitmapData Src1Data = SrcBitmap1.LockBits(new Rectangle(0, 0, SrcBitmap1.Width, SrcBitmap1.Height), ImageLockMode.ReadOnly, PixelFormat.Format8bppIndexed);
BitmapData Src2Data = SrcBitmap2.LockBits(new Rectangle(0, 0, SrcBitmap2.Width, SrcBitmap2.Height), ImageLockMode.ReadOnly, PixelFormat.Format8bppIndexed);
BitmapData DestData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);
unsafe
{
//Following is list of offset for different bit images
//8 bit : 1
//16 bit : 2
//24 bit : 3 and
//32 bit : 4
int xOffset = 1;
for (int col = 0; col < bitmap.Height - 1; col++)
{
byte* Src1Ptr = (byte*)Src1Data.Scan0 + col * Src1Data.Stride;
byte* Src2Ptr = (byte*)Src2Data.Scan0 + col * Src2Data.Stride;
byte* DestPtr = (byte*)DestData.Scan0 + col * DestData.Stride;
for (int row = 0; row < bitmap.Width - 1; row++)
{
byte i1 = Src1Ptr[row * xOffset];
bbyte i2 = Src2Ptr[row * xOffset];
DestPtr[row * xOffset + 0] = (byte)((((ushort)(i1) * (ushort)(i2)) >> 8));
DestPtr[row * xOffset + 1] = (byte)((ushort)(((ushort)(i1) * (ushort)(i2)) >> 8));
DestPtr[row * xOffset + 2] = (byte)((ushort)(((ushort)(i1) * (ushort)(i2)) >> 8));
}
}
}
bitmap.UnlockBits(DestData);
SrcBitmap1.UnlockBits(Src1Data);
SrcBitmap2.UnlockBits(Src2Data);
SrcBitmap1.Dispose();
SrcBitmap2.Dispose();
}
catch (Exception ex)
{
Console.Write(ex);
}
return bitmap;
}
推荐答案
我不认为您可以在索引模式下直接复制图像.我建议将图像转换为某些附加颜色模型,然后执行该操作,如果您确实需要索引表示,请重新索引生成的图像.请参阅:
https://en.wikipedia.org/wiki/Color_model [ https://en.wikipedia.org/wiki/Additive_color [
I don''t think you can multiply images directly in indexed mode. I would advise to convert the image to some additive color model, perform the operation, and, if you really need indexed representation, re-index the resulting image. Please see:
https://en.wikipedia.org/wiki/Color_model[^],
https://en.wikipedia.org/wiki/Additive_color[^].
—SA
这篇关于8 bpp索引图像的图像乘法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!