Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#生成图像中没有灰度值/噪声的二值遮罩?_C#_.net_Gdi+ - Fatal编程技术网

C#生成图像中没有灰度值/噪声的二值遮罩?

C#生成图像中没有灰度值/噪声的二值遮罩?,c#,.net,gdi+,C#,.net,Gdi+,在C#中,如何生成图像中没有灰度值/噪声的二值遮罩 现在我能够生成一个非常简单的输出,它看起来非常接近我想要的,但是在白色斑点的内部和外部的边缘都有噪声(你需要一直放大才能看到噪声)。我计划稍后使用图像进行图像处理,但图像中不能有除黑白值以外的任何内容 图片: 代码: 公共CropeImage(列表节点) { 初始化组件(); //List listpoints=新列表(); nodes.ToArray(); PointF[]points=新的PointF[nodes.Count]; 对于(i

在C#中,如何生成图像中没有灰度值/噪声的二值遮罩

现在我能够生成一个非常简单的输出,它看起来非常接近我想要的,但是在白色斑点的内部和外部的边缘都有噪声(你需要一直放大才能看到噪声)。我计划稍后使用图像进行图像处理,但图像中不能有除黑白值以外的任何内容

图片:

代码:

公共CropeImage(列表节点)
{
初始化组件();
//List listpoints=新列表();
nodes.ToArray();
PointF[]points=新的PointF[nodes.Count];
对于(int i=0;i
正如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
换衣服怎么样

g.SmoothingMode = SmoothingMode.AntiAlias;


如果我理解正确,这将解决您的问题。

只需遍历每个像素并将其值四舍五入为白色或黑色。是一个功能和一个环节;使用链接代码中的函数可快速解决问题如果sped不是很重要,请执行以下操作:在x*y像素上进行双循环,getpixel&getbrightness,然后setpixel..如果使用抗锯齿,则在黑色上绘制白色时会得到灰色值(反之亦然)。这正是抗锯齿的意义所在。请尝试
SmoothingModeNone
。这似乎是正确的解决方案。我一直在寻找一种使用GDI+包装器函数解决这个问题的方法,但这似乎是正确的解决方法。当我能够实现这一点时,我将把它标记为答案。谢谢Ashkan和eocron@Telahun我很高兴能帮上忙谢谢
 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;