求:C# 将bmp转成灰度图(8位,256色),然后再转成单色图(1位,只有黑白两色)的算法

yzuxiayu 2009-02-26 02:23:31
求:C#中 将bmp格式的图片转成灰度图(8位,256色),然后再转成单色图(1位,只有黑白两色)的算法,不要把网上找来的贴上来,最好是自己编码实现过。
...全文
2131 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
deyter 2009-04-16
  • 打赏
  • 举报
回复
mark
Harvey_He 2009-04-16
  • 打赏
  • 举报
回复
灰度图象就是单色图象,首先楼主的理解有误, 我想楼主第二步想要的是 黑白二值图(1 位,要么是0,要么是1)

//灰度图

Color c = new Color();
Color cc = new Color();

Bitmap box1 = new Bitmap(pictureBox1.Image);
Bitmap box2 = new Bitmap(pictureBox1.Image);

int rr,gg,bb;

for (int i = 0; i < pictureBox1.Image.Width; i++)
{
for (int j = 0; j < pictureBox1.Image.Height; j++)
{
c = box1.GetPixel(i, j);

rr = c.R;
gg = c.G;
bb = c.B;

cc = Color.FromArgb(rr, rr, rr);
box2.SetPixel(i, j, cc);
}
}
pictureBox2.Refresh();
pictureBox2.Image = box2;


//二值图

Color c = new Color();
Color cc = new Color();

Bitmap box1 = new Bitmap(pictureBox1.Image);
Bitmap box2 = new Bitmap(pictureBox1.Image);

int rr;

for (int i = 0; i < pictureBox1.Image.Width; i++)
{
for (int j = 0; j < pictureBox1.Image.Height; j++)
{
c = box1.GetPixel(i, j);

rr = c.R;
if(rr >=128)
{
rr = 255;
}
else
{
rr = 0;
}
cc = Color.FromArgb(rr, rr, rr);
box2.SetPixel(i, j, cc);
}
}
pictureBox2.Refresh();
pictureBox2.Image = box2;

hardstone1 2009-04-16
  • 打赏
  • 举报
回复
还是使用opencv最方便阿
winbq5 2009-04-04
  • 打赏
  • 举报
回复
up





bomdy 2009-03-03
  • 打赏
  • 举报
回复
这是我原来写的一个调整位图的类,你试试合不合用
Bitmap bmp = new Bitmap("test.bmp");
BmpAdjuster.Monochrome(ref bmp);


class BmpAdjuster
{
public delegate ColorPalette PaletteAdjustEvent(ColorPalette plt);
public unsafe delegate void ConvertScanLineEvent(IntPtr srcLine, IntPtr dstLine, int width, int srcPixBit, int dstPixBit, Bitmap srcBmp, Bitmap dstBmp);

public static void AdjustColor(ref Bitmap bmp, PixelFormat format, PaletteAdjustEvent PalleteAdjust, ConvertScanLineEvent ConvertScanLine)
{
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
Bitmap bmpOut = new Bitmap(bmp.Width, bmp.Height, format);

bmpOut.Palette = PalleteAdjust(bmpOut.Palette);

PixelFormat srcFmt = bmp.PixelFormat;
PixelFormat dstFmt = bmpOut.PixelFormat;
int srcPixBit = GetPixelSize(srcFmt);
int dstPixBit = GetPixelSize(dstFmt);

BitmapData srcData = null;
BitmapData dstData = null;
try
{
srcData = bmp.LockBits(rect, ImageLockMode.ReadOnly, srcFmt);
dstData = bmpOut.LockBits(rect, ImageLockMode.WriteOnly, dstFmt);

unsafe
{
byte* srcLine = (byte*)srcData.Scan0.ToPointer();
byte* dstLine = (byte*)dstData.Scan0.ToPointer();
for (int L = 0; L < srcData.Height; L++)
{
ConvertScanLine((IntPtr)srcLine, (IntPtr)dstLine, srcData.Width, srcPixBit, dstPixBit, bmp, bmpOut);

srcLine += srcData.Stride;
dstLine += dstData.Stride;
}
}
}
finally
{
bmp.UnlockBits(srcData);
bmpOut.UnlockBits(dstData);
}

bmp = bmpOut;
}

internal static int GetPixelSize(PixelFormat format)
{
switch (format)
{
case PixelFormat.Format16bppRgb555: return 16;
case PixelFormat.Format16bppRgb565: return 16;
case PixelFormat.Format24bppRgb: return 24;
case PixelFormat.Format32bppRgb: return 32;
case PixelFormat.Format1bppIndexed: return 1;
case PixelFormat.Format4bppIndexed: return 4;
case PixelFormat.Format8bppIndexed: return 8;
case PixelFormat.Format16bppArgb1555: return 16;
case PixelFormat.Format32bppPArgb: return 32;
case PixelFormat.Format16bppGrayScale: return 16;
case PixelFormat.Format48bppRgb: return 48;
case PixelFormat.Format64bppPArgb: return 64;
case PixelFormat.Canonical: return 32;
case PixelFormat.Format32bppArgb: return 32;
case PixelFormat.Format64bppArgb: return 64;
}
return 0;
}

public unsafe static void Monochrome(ref Bitmap bmp)
{
AdjustColor(ref bmp, PixelFormat.Format1bppIndexed,
new PaletteAdjustEvent(SetBlackWhitePallete),
new ConvertScanLineEvent(ConvertBlackWhiteScanLine));
}

static ColorPalette SetBlackWhitePallete(ColorPalette plt)
{
plt.Entries[0] = Color.Black;
plt.Entries[1] = Color.White;
return plt;
}

unsafe static void ConvertBlackWhiteScanLine(IntPtr srcLine, IntPtr dstLine, int width, int srcPixBit, int dstPixBit, Bitmap srcBmp, Bitmap dstBmp)
{
byte* src = (byte*)srcLine.ToPointer();
byte* dst = (byte*)dstLine.ToPointer();
int srcPixByte = srcPixBit / 8;
int x, v, t = 0;

for (x = 0; x < width; x++)
{
v = 28 * src[0] + 151 * src[1] + 77 * src[2];
t = (t << 1) | (v > 200*256 ? 1 : 0);
src += srcPixByte;

if (x % 8 == 7)
{
*dst = (byte)t;
dst++;
t = 0;
}
}

if ((x %= 8) != 7)
{
t <<= 8-x;
*dst = (byte)t;
}
}

public static void Gray(ref Bitmap bmp)
{
AdjustColor(ref bmp, PixelFormat.Format8bppIndexed,
new PaletteAdjustEvent(SetGrayPallete),
new ConvertScanLineEvent(ConvertGaryScanLine));
}

static ColorPalette SetGrayPallete(ColorPalette plt)
{
for (int i = plt.Entries.Length - 1; i >= 0; i--)
plt.Entries[i] = Color.FromArgb(i, i, i);
return plt;
}

unsafe static void ConvertGaryScanLine(IntPtr srcLine, IntPtr dstLine, int width, int srcPixBit, int dstPixBit, Bitmap srcBmp, Bitmap dstBmp)
{
byte* src = (byte*)srcLine.ToPointer();
byte* dst = (byte*)dstLine.ToPointer();
int srcPixByte = srcPixBit / 8;

for (int x = 0; x < width; x++)
{
*dst = (byte)((28 * src[0] + 151 * src[1] + 77 * src[2]) >> 8);
src += srcPixByte;
dst++;
}
}

}
wenbin 2009-03-03
  • 打赏
  • 举报
回复
AForge找这个开源工程,里面你想要都有。
宝_爸 2009-03-03
  • 打赏
  • 举报
回复
没实现过。。。
hbmy9107lwt 2009-03-03
  • 打赏
  • 举报
回复
up
derek02 2009-03-03
  • 打赏
  • 举报
回复
up..

110,549

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧