最近看了一些Sobel算法,并试了一下,源码如下:

private void Sobel(Bitmap img) {
int width = img.Width;
int height = img.Height; int[,] Gx = new int[, ]{ {-, , },
{-, , } ,
{-, , } }; int[,] Gy = new int[, ]{ {-,-,-},
{ , , },
{ , , }}; int[,] TotalGx = new int[img.Width, img.Height];
int[,] TotalGy = new int[img.Width, img.Height];
int[,] GTotal = new int[img.Width, img.Height]; Bitmap bitmapTemp = new Bitmap(img.Width, img.Height, PixelFormat.Format24bppRgb); LockBitmap lockBitmap1 = new LockBitmap(bitmapTemp);
lockBitmap1.LockBits(); LockBitmap lockBitmap = new LockBitmap(img);
lockBitmap.LockBits();
for (int i = ; i < img.Width - ; i++)
{
for (int j = ; j < img.Height - ; j++)
{
Color a = lockBitmap.GetPixel(i - , j - );//[0][0]
Color b = lockBitmap.GetPixel(i - , j); //[0][1]
Color c = lockBitmap.GetPixel(i - , j + );//[0][2] Color d = lockBitmap.GetPixel(i, j - ); //[1][0]
Color f = lockBitmap.GetPixel(i, j); //[1][1]
Color g = lockBitmap.GetPixel(i, j + ); //[1][2] Color h = lockBitmap.GetPixel(i + , j - );//[2][0]
Color l = lockBitmap.GetPixel(i + , j); //[2][1]
Color n = lockBitmap.GetPixel(i + , j + ); //[2][2] TotalGx[i, j] = Gx[, ] * Avg(a) + Gx[, ] * Avg(b) + Gx[, ] * Avg(c)
+ Gx[, ] * Avg(d) + Gx[, ] * Avg(f) + Gx[, ] * Avg(g)
+ Gx[, ] * Avg(h) + Gx[, ] * Avg(l) + Gx[, ] * Avg(n); //if (TotalGx[i, j] < 0) { TotalGx[i, j] = 0; }
//if (TotalGx[i, j] > 255) { TotalGx[i, j] = 255; } TotalGy[i, j] = Gy[, ] * Avg(a) + Gy[, ] * Avg(b) + Gy[, ] * Avg(c)
+ Gy[, ] * Avg(d) + Gy[, ] * Avg(f) + Gy[, ] * Avg(g)
+ Gy[, ] * Avg(h) + Gy[, ] * Avg(l) + Gy[, ] * Avg(n); //if (TotalGy[i, j] < 0) { TotalGy[i, j] = 0; }
//if (TotalGy[i, j] > 255) { TotalGy[i, j] = 255; } //GTotal[i, j] = TotalGx[i, j] + TotalGy[i, j];
GTotal[i, j] = (int)Math.Sqrt(TotalGx[i, j] * TotalGx[i, j] + TotalGy[i, j] * TotalGy[i, j]); if (GTotal[i, j] >= )
{ GTotal[i, j] = ; } if (GTotal[i, j] < )
{ GTotal[i, j] = ; }
//bitmapTemp.SetPixel(i, j, Color.FromArgb(GTotal[i, j], GTotal[i ,j], GTotal[i, j]));
lockBitmap1.SetPixel(i, j, Color.FromArgb(GTotal[i, j], GTotal[i, j], GTotal[i, j]));
}
}
lockBitmap1.UnlockBits();
lockBitmap.UnlockBits();
pictureBox2.Image = lockBitmap1.GetBitmap(); }
public class LockBitmap
{
Bitmap source = null;
IntPtr Iptr = IntPtr.Zero;
BitmapData bitmapData = null; public byte[] Pixels { get; set; }
public int Depth { get; private set; }
public int Width { get; private set; }
public int Height { get; private set; } public LockBitmap(Bitmap source)
{
this.source = source;
} public Bitmap GetBitmap() { return this.source; } /// <summary>
/// Lock bitmap data
/// </summary>
public void LockBits()
{
try
{
// Get width and height of bitmap
Width = source.Width;
Height = source.Height; // get total locked pixels count
int PixelCount = Width * Height; // Create rectangle to lock
Rectangle rect = new Rectangle(, , Width, Height); // get source bitmap pixel format size
Depth = System.Drawing.Bitmap.GetPixelFormatSize(source.PixelFormat); // Check if bpp (Bits Per Pixel) is 8, 24, or 32
if (Depth != && Depth != && Depth != )
{
throw new ArgumentException("Only 8, 24 and 32 bpp images are supported.");
} // Lock bitmap and return bitmap data
bitmapData = source.LockBits(rect, ImageLockMode.ReadWrite,
source.PixelFormat); // create byte array to copy pixel values
int step = Depth / ;
Pixels = new byte[PixelCount * step];
Iptr = bitmapData.Scan0; // Copy data from pointer to array
Marshal.Copy(Iptr, Pixels, , Pixels.Length);
}
catch (Exception ex)
{
throw ex;
}
} /// <summary>
/// Unlock bitmap data
/// </summary>
public void UnlockBits()
{
try
{
// Copy data from byte array to pointer
Marshal.Copy(Pixels, , Iptr, Pixels.Length); // Unlock bitmap data
source.UnlockBits(bitmapData);
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// Get the color of the specified pixel
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
public Color GetPixel(int x, int y)
{
Color clr = Color.Empty; // Get color components count
int cCount = Depth / ; // Get start index of the specified pixel
int i = ((y * Width) + x) * cCount; if (i > Pixels.Length - cCount)
throw new IndexOutOfRangeException(); if (Depth == ) // For 32 bpp get Red, Green, Blue and Alpha
{
byte b = Pixels[i];
byte g = Pixels[i + ];
byte r = Pixels[i + ];
byte a = Pixels[i + ]; // a
clr = Color.FromArgb(a, r, g, b);
}
if (Depth == ) // For 24 bpp get Red, Green and Blue
{
byte b = Pixels[i];
byte g = Pixels[i + ];
byte r = Pixels[i + ];
clr = Color.FromArgb(r, g, b);
}
if (Depth == )
// For 8 bpp get color value (Red, Green and Blue values are the same)
{
byte c = Pixels[i];
clr = Color.FromArgb(c, c, c);
}
return clr;
} /// <summary>
/// Set the color of the specified pixel
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <param name="color"></param>
public void SetPixel(int x, int y, Color color)
{
// Get color components count
int cCount = Depth / ; // Get start index of the specified pixel
int i = ((y * Width) + x) * cCount; if (Depth == ) // For 32 bpp set Red, Green, Blue and Alpha
{
Pixels[i] = color.B;
Pixels[i + ] = color.G;
Pixels[i + ] = color.R;
Pixels[i + ] = color.A;
}
if (Depth == ) // For 24 bpp set Red, Green and Blue
{
Pixels[i] = color.B;
Pixels[i + ] = color.G;
Pixels[i + ] = color.R;
}
if (Depth == )
// For 8 bpp set color value (Red, Green and Blue values are the same)
{
Pixels[i] = color.B;
}
} //public Color GetPixel(int x, int y)
//{
// unsafe
// {
// byte* ptr = (byte*)Iptr;
// ptr = ptr + bitmapData.Stride * y;
// ptr += Depth * x / 8;
// Color c = Color.Empty;
// if (Depth == 32)
// {
// int a = ptr[3];
// int r = ptr[2];
// int g = ptr[1];
// int b = ptr[0];
// c = Color.FromArgb(a, r, g, b);
// }
// else if (Depth == 24)
// {
// int r = ptr[2];
// int g = ptr[1];
// int b = ptr[0];
// c = Color.FromArgb(r, g, b);
// }
// else if (Depth == 8)
// {
// int r = ptr[0];
// c = Color.FromArgb(r, r, r);
// }
// return c;
// }
//} //public void SetPixel(int x, int y, Color c)
//{
// unsafe
// {
// byte* ptr = (byte*)Iptr;
// ptr = ptr + bitmapData.Stride * y;
// ptr += Depth * x / 8;
// if (Depth == 32)
// {
// ptr[3] = c.A;
// ptr[2] = c.R;
// ptr[1] = c.G;
// ptr[0] = c.B;
// }
// else if (Depth == 24)
// {
// ptr[2] = c.R;
// ptr[1] = c.G;
// ptr[0] = c.B;
// }
// else if (Depth == 8)
// {
// ptr[2] = c.R;
// ptr[1] = c.G;
// ptr[0] = c.B;
// }
// }
//} //return data[((width * y) + x) * 4 + i];
}

效果如下:

总结:用自带的图片处理性能低下,建议使用指针或者其他图像库处理,比如OpenCV的.NET库。

Sobel算法的更多相关文章

  1. opencl+opencv实现sobel算法

    这几天在看opencl编程指南.照着书中的样例实现了sobel算法: 1.结合opencv读取图像,保存到缓冲区中. 2.编写和编译内核.并保存显示处理后的结果. 内核: const sampler_ ...

  2. 14FPGA综设之图像边沿检测的sobel算法

    连续学习FPGA基础课程接近一个月了,迎来第一个有难度的综合设计,图像的边沿检测算法sobel,用verilog代码实现算法功能. 一设计功能 (一设计要求) (二系统框图) 根据上面的系统,Veri ...

  3. sobel算法的Soc FPGA实现之框架分析(二)

    重点分析一.AXI_VDMA_1 之前一直认为这个就是内含有DDR的ip核(......最近才搞懂是个啥),后来经过对FDMA的分析发现这就是个框架,通AXI总线挂载到bus总线,可以实现PL端FPG ...

  4. Hls平台实现sobel算法(一)

    索贝尔(Sobel)算子主要用于边缘检测,根据像素点的上下.左右邻点的灰度加权差与阈值进行比较,在边缘处达到极值的方法实现边缘检测. -------------序 一.原理性运行 流水线操作,将输入图 ...

  5. 基于Vivado HLS在zedboard中的Sobel滤波算法实现

     基于Vivado HLS在zedboard中的Sobel滤波算法实现 平台:zedboard  + Webcam 工具:g++4.6  + VIVADO HLS  + XILINX EDK + ...

  6. 每天进步一点点------Sobel算子(3)基于彩色图像边缘差分的运动目标检测算法

    摘  要: 针对目前常用的运动目标提取易受到噪声影响.易出现阴影和误检漏检等情况,提出了一种基于Sobel算子的彩色边缘图像检测和帧差分相结合的检测方法.首先用Sobel算子提取视频流中连续4帧图像的 ...

  7. opencv算法学习

    1.改变图像的亮度和对比度: 算法介绍:对每一点像素值的r,g,b,值进行乘法和加法的运算. 代码使用: ; y < image.rows; y++ ) { ; x < image.col ...

  8. 图像特征提取:Sobel边缘检测

    前言 点和线是做图像分析时两个最重要的特征,而线条往往反映了物体的轮廓,对图像中边缘线的检测是图像分割与特征提取的基础.文章主要讨论两个实际工程中常用的边缘检测算法:Sobel边缘检测和Canny边缘 ...

  9. OpenCV探索之路(六):边缘检测(canny、sobel、laplacian)

    边缘检测的一般步骤: 滤波--消除噪声 增强--使边界轮廓更加明显 检测--选出边缘点 Canny算法 Canny边缘检测算法被很多人推崇为当今最优秀的边缘检测算法,所以我们第一个就介绍他. open ...

随机推荐

  1. C++面试题(三)

    1 什么是函数对象?有什么作用? 函数对象却具有许多函数指针不具有的有点,函数对象使程序设计更加灵活,而且能够实现函数的内联(inline)调用,使整个程序实现性能加速. 函数对象:这里已经说明了这是 ...

  2. Python生成器/推导式/生成器表达式

    一   生成器 生成器的本质就是迭代器 生成器的特点和迭代器一样,取值方式和迭代器一样(__next__(),  send():  给上一个yield传值) 生成器一般由生成器函数或者生成器表达式来创 ...

  3. K12(在线学习的平台)

    项目:K12(在线学习的平台) 一.背景 目的是做一个在线的学习平台,提高学生的课程完成度 K12:大目标是要取代线下班 - 录制专门的视频 - 导师的监管:如果没有主动和那个学生聊天,就扣钱 - 学 ...

  4. 关于datatable对象的用法

    在项目中经常用到DataTable,如果DataTable使用得当,不仅能使程序简洁实用,而且能够提高性能,达到事半功倍的效果,现对DataTable的使用技巧进行一下总结. 一.DataTable简 ...

  5. Tornado 高并发源码分析之四--- HTTPServer 与 TCPServer 对象

    主要工作: 服务器启动的时候做的事: 1.把包含了各种配置信息的 application 对象封装到了 HttpServer 对象的 request_callback 字段中,等待被调用 2.TCPS ...

  6. C++深度解析教程学习笔记(4)C++中的新成员

    1. 动态内存分配 (1)C++通过 new 关键字进行动态内存申请,是以类型为单位来申请空间大小的 (2)delete 关键字用于内存释放 ▲注意释放数组时要加[],否则只释放这个数组中的第 1 个 ...

  7. undefined&nbsp;reference&nbsp;to…

    照着GUN/Linux编程指南中的一个例子输入编译,结果出现如下错误: undefined reference to 'pthread_create' undefined reference to ' ...

  8. java基础知识(三)之数组

    声明数组: 语法:数据类型[ ] 数组名://例:int[ ] scores;  或者 数据类型 数组名[ ]://例:int scores[ ];分配空间 语法:数组名 = new 数据类型 [ 数 ...

  9. python利用utf-8编码判断中文字符

    下面这个小工具包含了 判断unicode是否是汉字,数字,英文,或者其他字符. 全角符号转半角符号. unicode字符串归一化等工作. 还有一个能处理多音字的汉字转拼音的程序,还在整理中. #!/u ...

  10. OpenGL顶点缓冲区对象

    [OpenGL顶点缓冲区对象] 显示列表可以快速简单地优化立即模式(glBegin/glEnd)的代码.在最坏的情况下,显示列表的命令被预编译存到命令缓冲区中,然后发送给图形硬件.在最好的情况下,是编 ...