二值图像处理解决办法
二值图像处理
怎么将只有0和1 两个灰度级的二值图像进行处理,将其扩展成只有0和255二值图像,VC++怎么实现
------解决方案--------------------
试试下面这种方法 自己根据实际情况修改一下
public Bitmap Threshoding(Bitmap b, byte threshold)//threshold可以传1进来
{
int width = b.Width;
int height = b.Height;
BitmapData data = b.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
unsafe
{
byte* p = (byte*)data.Scan0;
int offset = data.Stride - width * 4;
byte R, G, B, gray;
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
R = p[2];
G = p[1];
B = p[0];
gray = (byte)((R * 19595 + G * 38469 + B * 7472) >> 16);
if (gray >= threshold)
{
p[0] = p[1] = p[2] = 255;
}
else
{
p[0] = p[1] = p[2] = 0;
}
p += 4;
}
p += offset;
}
b.UnlockBits(data);
return b;
}
}
怎么将只有0和1 两个灰度级的二值图像进行处理,将其扩展成只有0和255二值图像,VC++怎么实现
------解决方案--------------------
试试下面这种方法 自己根据实际情况修改一下
public Bitmap Threshoding(Bitmap b, byte threshold)//threshold可以传1进来
{
int width = b.Width;
int height = b.Height;
BitmapData data = b.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
unsafe
{
byte* p = (byte*)data.Scan0;
int offset = data.Stride - width * 4;
byte R, G, B, gray;
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
R = p[2];
G = p[1];
B = p[0];
gray = (byte)((R * 19595 + G * 38469 + B * 7472) >> 16);
if (gray >= threshold)
{
p[0] = p[1] = p[2] = 255;
}
else
{
p[0] = p[1] = p[2] = 0;
}
p += 4;
}
p += offset;
}
b.UnlockBits(data);
return b;
}
}