C# 生成图像中没有灰色 values/noise 的二进制掩码?
C# generate a binary mask without gray values/noise in the image?
在 C# 中,如何生成图像中没有灰色 values/noise 的二进制掩码?
现在我能够生成一个相当简单的输出,它看起来非常接近我想要的,但在白色斑点的内部和外部边缘周围都有噪音(您需要缩放所有查看噪音的方式)。我打算稍后使用该图像进行图像处理,但图像中只能有黑白值。
图片:
Crop 1
Crop 2
代码:
public CropedImage(List<Node> nodes)
{
InitializeComponent();
//List<PointF> listpoints = new List<PointF>();
nodes.ToArray();
PointF[] points = new PointF[nodes.Count];
for(int i = 0; i < nodes.Count; ++i)
{
points[i].X = nodes[i].X;
points[i].Y = nodes[i].Y;
}
Image SourceImage = ImageOperations.GetImage(MainForm.imageMatrix, pictureBox1);
using (Graphics g = Graphics.FromImage(SourceImage))
{
Color black = ColorTranslator.FromHtml("#000000");
Color white = ColorTranslator.FromHtml("#FFFFFF");
using (Brush b = new SolidBrush(black))
{
Rectangle rect = new Rectangle();
g.FillRectangle(b, 0, 0, MainForm.WIDTH, MainForm.HEIGHT);
}
using (Brush b2 = new SolidBrush(white))
{
g.SmoothingMode = SmoothingMode.AntiAlias;
g.FillClosedCurve(b2, points, 0);
}
}
}
正如 eocron 所说,您必须检查所有像素并将它们四舍五入为黑色或白色。最简单的方法(如果你只是创建一个临时应用程序来为一些图像执行此操作)是使用 GetPixel()
和 SetPixel()
方法:
Bitmap bmp = Bitmap.FromFile("image.png");
for(int i=0;i<bmp.Width;i++)
for(int j=0;j<bmp.Height;j++)
bmp.SetPixel(i,j, bmp.GetPixel(i,j).R > 127? Color.White: Color.Black); // as its gray I'm only checking Red
但是,如果您要处理大量图像,尤其是大图像,或者它不是处理几张图像的临时应用程序,并且它将成为您应用程序的一个功能,则上面的代码不是您想要的喜欢使用,因为它很慢,你应该使用 LockBits
,因为它更快:
Bitmap bmp = Bitmap.FromFile("image.png");
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
System.Drawing.Imaging.BitmapData bmpData =
bmp.LockBits(rect,System.Drawing.Imaging.ImageLockMode.ReadWrite,bmp.PixelFormat;
IntPtr ptr = bmpData.Scan0;
int bytes = Math.Abs(bmpData.Stride) * bmp.Height;
byte[] rgbValues = new byte[bytes];
System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);
for (int counter = 0; counter < rgbValues.Length; counter += 4)
{
int c = rgbValues[counter] > 127 ? 255: 0;
rgbValues[counter] = c;
rgbValues[counter+1] = c;
rgbValues[counter+2] = c;
}
System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes);
bmp.UnlockBits(bmpData);
改一下怎么样
g.SmoothingMode = SmoothingMode.AntiAlias;
到
g.SmoothingMode = SmoothingMode.None;
如果我没理解错的话,就可以解决你的问题了。
在 C# 中,如何生成图像中没有灰色 values/noise 的二进制掩码?
现在我能够生成一个相当简单的输出,它看起来非常接近我想要的,但在白色斑点的内部和外部边缘周围都有噪音(您需要缩放所有查看噪音的方式)。我打算稍后使用该图像进行图像处理,但图像中只能有黑白值。
图片: Crop 1 Crop 2
代码:
public CropedImage(List<Node> nodes)
{
InitializeComponent();
//List<PointF> listpoints = new List<PointF>();
nodes.ToArray();
PointF[] points = new PointF[nodes.Count];
for(int i = 0; i < nodes.Count; ++i)
{
points[i].X = nodes[i].X;
points[i].Y = nodes[i].Y;
}
Image SourceImage = ImageOperations.GetImage(MainForm.imageMatrix, pictureBox1);
using (Graphics g = Graphics.FromImage(SourceImage))
{
Color black = ColorTranslator.FromHtml("#000000");
Color white = ColorTranslator.FromHtml("#FFFFFF");
using (Brush b = new SolidBrush(black))
{
Rectangle rect = new Rectangle();
g.FillRectangle(b, 0, 0, MainForm.WIDTH, MainForm.HEIGHT);
}
using (Brush b2 = new SolidBrush(white))
{
g.SmoothingMode = SmoothingMode.AntiAlias;
g.FillClosedCurve(b2, points, 0);
}
}
}
正如 eocron 所说,您必须检查所有像素并将它们四舍五入为黑色或白色。最简单的方法(如果你只是创建一个临时应用程序来为一些图像执行此操作)是使用 GetPixel()
和 SetPixel()
方法:
Bitmap bmp = Bitmap.FromFile("image.png");
for(int i=0;i<bmp.Width;i++)
for(int j=0;j<bmp.Height;j++)
bmp.SetPixel(i,j, bmp.GetPixel(i,j).R > 127? Color.White: Color.Black); // as its gray I'm only checking Red
但是,如果您要处理大量图像,尤其是大图像,或者它不是处理几张图像的临时应用程序,并且它将成为您应用程序的一个功能,则上面的代码不是您想要的喜欢使用,因为它很慢,你应该使用 LockBits
,因为它更快:
Bitmap bmp = Bitmap.FromFile("image.png");
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
System.Drawing.Imaging.BitmapData bmpData =
bmp.LockBits(rect,System.Drawing.Imaging.ImageLockMode.ReadWrite,bmp.PixelFormat;
IntPtr ptr = bmpData.Scan0;
int bytes = Math.Abs(bmpData.Stride) * bmp.Height;
byte[] rgbValues = new byte[bytes];
System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);
for (int counter = 0; counter < rgbValues.Length; counter += 4)
{
int c = rgbValues[counter] > 127 ? 255: 0;
rgbValues[counter] = c;
rgbValues[counter+1] = c;
rgbValues[counter+2] = c;
}
System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes);
bmp.UnlockBits(bmpData);
改一下怎么样
g.SmoothingMode = SmoothingMode.AntiAlias;
到
g.SmoothingMode = SmoothingMode.None;
如果我没理解错的话,就可以解决你的问题了。