C#阈值分割处理图像
想用c#编一个可以对图像阈值分割的函数,但是老是运行不正确
private static Bitmap robot(Bitmap curBitmap)
{
Rectangle rect = new Rectangle(0, 0, curBitmap.Width, curBitmap.Height);
System.Drawing.Imaging.BitmapData bmpData = curBitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, curBitmap.PixelFormat);
IntPtr ptr = bmpData.Scan0;
int bytes = curBitmap.Width * curBitmap.Height;
byte[] grayValues = new byte[bytes];
System.Runtime.InteropServices.Marshal.Copy(ptr, grayValues, 0, bytes);
byte T = 0, S = 0;
byte[] neighb = new byte[bytes];
byte temp = 0;
byte maxGray = 0;
byte minGray = 255;
int[] countPixel = new int[256];
for (int i = 0; i < grayValues.Length; i++)
{
temp = grayValues[i];
countPixel[temp]++;
if (temp > maxGray)
{
maxGray = temp;
}
if (temp < minGray)
{
minGray = temp;
}
}
double mu1, mu2;
int numerator;
double sigma;
double tempMax = 0;
double w1 = 0, w2 = 0;
double sum = 0;
numerator = 0;
for (int i = minGray; i <= maxGray; i++)
{
sum += i * countPixel[i];
}
for (int i = minGray; i < maxGray; i++)
{
w1 += countPixel[i];
numerator += i * countPixel[i];
mu1 = numerator / w1;
w2 = grayValues.Length - w1;
mu2 = (sum - numerator) / w2;
sigma = w1 * w2 * (mu1 - mu2) * (mu1 - mu2);
if (sigma > tempMax)
{
tempMax = sigma;
T = Convert.ToByte(i);
}
} for (int i = 0; i < bytes; i++)
{
if (grayValues[i] < T)
grayValues[i] = 0;
else
grayValues[i] = 255;
}
System.Runtime.InteropServices.Marshal.Copy(grayValues, 0, ptr, bytes);
curBitmap.UnlockBits(bmpData);
return curBitmap;
}
这是程序,处理时总是只能处理上半部分,有没有大神能给改改,或者给一个更好的处理方法。
private static Bitmap robot(Bitmap curBitmap)
{
Rectangle rect = new Rectangle(0, 0, curBitmap.Width, curBitmap.Height);
System.Drawing.Imaging.BitmapData bmpData = curBitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, curBitmap.PixelFormat);
IntPtr ptr = bmpData.Scan0;
int bytes = curBitmap.Width * curBitmap.Height;
byte[] grayValues = new byte[bytes];
System.Runtime.InteropServices.Marshal.Copy(ptr, grayValues, 0, bytes);
byte T = 0, S = 0;
byte[] neighb = new byte[bytes];
byte temp = 0;
byte maxGray = 0;
byte minGray = 255;
int[] countPixel = new int[256];
for (int i = 0; i < grayValues.Length; i++)
{
temp = grayValues[i];
countPixel[temp]++;
if (temp > maxGray)
{
maxGray = temp;
}
if (temp < minGray)
{
minGray = temp;
}
}
double mu1, mu2;
int numerator;
double sigma;
double tempMax = 0;
double w1 = 0, w2 = 0;
double sum = 0;
numerator = 0;
for (int i = minGray; i <= maxGray; i++)
{
sum += i * countPixel[i];
}
for (int i = minGray; i < maxGray; i++)
{
w1 += countPixel[i];
numerator += i * countPixel[i];
mu1 = numerator / w1;
w2 = grayValues.Length - w1;
mu2 = (sum - numerator) / w2;
sigma = w1 * w2 * (mu1 - mu2) * (mu1 - mu2);
if (sigma > tempMax)
{
tempMax = sigma;
T = Convert.ToByte(i);
}
} for (int i = 0; i < bytes; i++)
{
if (grayValues[i] < T)
grayValues[i] = 0;
else
grayValues[i] = 255;
}
System.Runtime.InteropServices.Marshal.Copy(grayValues, 0, ptr, bytes);
curBitmap.UnlockBits(bmpData);
return curBitmap;
}
这是程序,处理时总是只能处理上半部分,有没有大神能给改改,或者给一个更好的处理方法。
...全文
相关推荐
用大津阈值(Otsu Thresholding)实现的阈值分割C#程序实例
大津阈值(Otsu Thresholding)是一种广泛应用于图像处理领域的自适应阈值分割方法,由日本学者大津(Nobuyuki Otsu)于1979年提出。
C#图像处理:深入掌握阈值分割算法实战
GDI+(Graphics Device Interface Plus)是微软公司开发的用于图形处理和显示的编程接口,它为开发人员提供了一种简便的方法来创建复杂的图形和图像。GDI+扩展了早期的GDI(Graphics Device Interface),提供了更多的绘图功能、图像处理能力和字体支持。在C#中,System.Drawing命名空间为图像处理提供了支持。使用GDI+,可以加载、创建、操作以及保存图像。此外,GDI+还支持绘制图形、处理颜色、绘制文本以及复杂的图形变换等。
一个用labview编写的基于一维最大熵的图像阈值分割方法.zip
如果项目中确实包含了C#代码,那么可能是在实现与LabVIEW的交互,例如通过.NET框架调用LabVIEW编写的DLL库,或者C#用于处理非图像处理的部分逻辑。
阈值分割算法
阈值分割算法是图像处理中的一个关键步骤,用于将图像二值化,即将图像分成前景和背景两部分。在C#编程环境下,你可以利用这些算法对图像进行加工处理,如分析、识别或保存等操作。
otsu阈值分割c#语言
### otsu阈值分割C#语言实现在图像处理领域,阈值分割是一种非常重要的技术,用于将图像划分为不同的区域。


