Win8 Metro(C#)数字图像处理--2.48Canny边缘检测算法
原文:Win8 Metro(C#)数字图像处理--2.48Canny边缘检测算法
[算法说明]
Canny边缘检测算法可以分为4步:高斯滤波器平滑处理、梯度计算、非极大值抑制、双阈值边缘检
测和边缘连接。
1,高斯滤波器平滑处理。由于图像中经常包含一些高斯噪声,因此在边缘检测前我们要先用高斯
滤波器对其进行滤波,为了方便,通常是使用一些高斯模板,这里我们使用如下的高斯滤波器模板。
/// <summary>
/// Canny edge detect process.
/// </summary>
/// <param name="src">The source image.</param>
/// <param name="highThreshould">The high threshould value. </param>
/// <param name="lowThreshould">The low threshould value. </param>
/// <returns></returns>
public static WriteableBitmap CannyedgedetectProcess(WriteableBitmap src,int highThreshould,int lowThreshould)////图像油画效果
{
if (src != null)
{
int w = src.PixelWidth;
int h = src.PixelHeight;
WriteableBitmap srcImage = new WriteableBitmap(w, h);
byte[] temp = src.PixelBuffer.ToArray();
byte[] tempMask = (byte[])temp.Clone();
int[,] srcBytes = new int[w, h];
for (int j = 0; j < h; j++)
{
for (int i = 0; i < w; i++)
{
srcBytes[i, j] = (int)(tempMask[i * 4 + j * w * 4] * 0.114 + tempMask[i * 4 + 1 + j * w * 4] * 0.587 + tempMask[i * 4 + 2 + j * w * 4] * 0.299);
}
}
float gradientMax = 0;
float[,] gradient = new float[w, h];
byte[,] degree = new byte[w, h];
GaussFilter(ref srcBytes, w, h);
GetGradientDegree(srcBytes, ref gradient, ref degree, ref gradientMax, w, h);
NonMaxMini(gradient, ref srcBytes, gradientMax, w, h, degree);
TwoThreshouldJudge(highThreshould, lowThreshould, ref srcBytes, w, h);
for (int j = 0; j < h; j++)
{
for (int i = 0; i < w; i++)
{
temp[i * 4 + j * w * 4] = temp[i * 4 + 1 + j * w * 4] = temp[i * 4 + 2 + j * w * 4] = (byte)srcBytes[i, j];
}
}
Stream sTemp = srcImage.PixelBuffer.AsStream();
sTemp.Seek(0, SeekOrigin.Begin);
sTemp.Write(temp, 0, w * 4 * h);
return srcImage;
}
else
{
return null;
}
}
//高斯滤波
private static void GaussFilter(ref int[,] src, int x, int y)
{
for (int j = 1; j < y - 1; j++)
{
for (int i = 1; i < x - 1; i++)
{
src[i, j] = (4 * src[i, j] + src[i - 1, j - 1] + src[i + 1, j - 1] + src[i - 1, j + 1] + src[i + 1, j + 1] + 2 * src[i, j - 1] + 2 * src[i - 1, j] + 2 * src[i, j + 1] + 2 * src[i + 1, j]) / 16;
}
}
}
//梯度相位角获取
private static void GetGradientDegree(int[,] srcBytes, ref float[,] gradient, ref byte[,] degree, ref float GradientMax, int x, int y)
{
gradient = new float[x, y];
degree = new byte[x, y];
int gx, gy;
int temp;
double div;
for (int j = 1; j < y - 1; j++)
{
for (int i = 1; i < x - 1; i++)
{
gx = srcBytes[i + 1, j - 1] + 2 * srcBytes[i + 1, j] + srcBytes[i + 1, j + 1] - srcBytes[i - 1, j - 1] - 2 * srcBytes[i - 1, j] - srcBytes[i - 1, j + 1];
gy = srcBytes[i - 1, j - 1] + 2 * srcBytes[i, j - 1] + srcBytes[i + 1, j - 1] - srcBytes[i - 1, j + 1] - 2 * srcBytes[i, j + 1] - srcBytes[i + 1, j + 1];
gradient[i, j] = (float)Math.Sqrt((double)(gx * gx + gy * gy));
if (GradientMax < gradient[i, j])
{
GradientMax = gradient[i, j];
}
if (gx == 0)
{
temp = (gy == 0) ? 0 : 90;
}
else
{
div = (double)gy / (double)gx;
if (div < 0)
{
temp = (int)(180 - Math.Atan(-div) * 180 / Math.PI);
}
else
{
temp = (int)(Math.Atan(div) * 180 / Math.PI);
}
if (temp < 22.5)
{
temp = 0;
}
else if (temp < 67.5)
{
temp = 45;
}
else if (temp < 112.5)
{
temp = 90;
}
else if (temp < 157.5)
{
temp = 135;
}
else
temp = 0;
}
degree[i, j] = (byte)temp;
}
}
}
//非极大值抑制
private static void NonMaxMini(float[,] gradient, ref int[,] srcBytes, float GradientMax, int x, int y, byte[,] degree)
{
float leftPixel = 0, rightPixel = 0;
for (int j = 1; j < y - 1; j++)
{
for (int i = 1; i < x - 1; i++)
{
switch (degree[i, j])
{
case 0:
leftPixel = gradient[i - 1, j];
rightPixel = gradient[i + 1, j];
break;
case 45:
leftPixel = gradient[i - 1, j + 1];
rightPixel = gradient[i + 1, j - 1];
break;
case 90:
leftPixel = gradient[i, j + 1];
rightPixel = gradient[i, j - 1];
break;
case 135:
leftPixel = gradient[i + 1, j + 1];
rightPixel = gradient[i - 1, j - 1];
break;
default:
break;
}
if ((gradient[i, j] < leftPixel) || (gradient[i, j] < rightPixel))
{
srcBytes[i, j] = 0;
}
else
{
srcBytes[i, j] = (int)(255 * gradient[i, j] / GradientMax);
}
}
}
}
//双阈值边缘判断
private static void TwoThreshouldJudge(int highThreshold, int lowThreshould, ref int[,] srcBytes, int x, int y)
{
for (int j = 1; j < y - 1; j++)
{
for (int i = 1; i < x - 1; i++)
{
if (srcBytes[i, j] > highThreshold)
{
srcBytes[i, j] = 255;
}
else if (srcBytes[i, j] < lowThreshould)
{
srcBytes[i, j] = 0;
}
else
{
if (srcBytes[i - 1, j - 1] < highThreshold && srcBytes[i, j - 1] < highThreshold && srcBytes[i + 1, j - 1] < highThreshold && srcBytes[i - 1, j] < highThreshold
&& srcBytes[i + 1, j] < highThreshold && srcBytes[i - 1, j + 1] < highThreshold && srcBytes[i, j + 1] < highThreshold && srcBytes[i + 1, j + 1] < highThreshold)
{
srcBytes[i, j] = 0;
}
else
srcBytes[i, j] = 255;
}
}
}
}
Win8 Metro(C#)数字图像处理--2.48Canny边缘检测算法的更多相关文章
- Win8 Metro(C#)数字图像处理--2.75灰度图像的形态学算法
原文:Win8 Metro(C#)数字图像处理--2.75灰度图像的形态学算法 前面章节中介绍了二值图像的形态学算法,这里讲一下灰度图的形态学算法,主要是公式,代码略. 1,膨胀算法 2,腐蚀算法 3 ...
- Win8 Metro(C#)数字图像处理--4图像颜色空间描述
原文:Win8 Metro(C#)数字图像处理--4图像颜色空间描述 图像颜色空间是图像颜色集合的数学表示,本小节将针对几种常见颜色空间做个简单介绍. /// <summary> / ...
- Win8 Metro(C#)数字图像处理--3.2图像方差计算
原文:Win8 Metro(C#)数字图像处理--3.2图像方差计算 /// <summary> /// /// </summary>Variance computing. / ...
- Win8 Metro(C#)数字图像处理--3.3图像直方图计算
原文:Win8 Metro(C#)数字图像处理--3.3图像直方图计算 /// <summary> /// Get the array of histrgram. /// </sum ...
- Win8 Metro(C#)数字图像处理--3.4图像信息熵计算
原文:Win8 Metro(C#)数字图像处理--3.4图像信息熵计算 [函数代码] /// <summary> /// Entropy of one image. /// </su ...
- Win8 Metro(C#)数字图像处理--3.5图像形心计算
原文:Win8 Metro(C#)数字图像处理--3.5图像形心计算 /// <summary> /// Get the center of the object in an image. ...
- Win8 Metro(C#)数字图像处理--2.73一种背景图像融合特效
原文:Win8 Metro(C#)数字图像处理--2.73一种背景图像融合特效 /// <summary> /// Image merge process. /// </summar ...
- Win8 Metro(C#)数字图像处理--3.1图像均值计算
原文:Win8 Metro(C#)数字图像处理--3.1图像均值计算 /// <summary> /// Mean value computing. /// </summary> ...
- Win8 Metro(C#)数字图像处理--2.74图像凸包计算
原文:Win8 Metro(C#)数字图像处理--2.74图像凸包计算 /// <summary> /// Convex Hull compute. /// </summary> ...
随机推荐
- svn: E200033: database is locked解决办法
svn执行update,却被告知database is locked! 执行 svn update,却抛出个错误警报: svn: E200033: database is locked, execut ...
- 在ArcEngine下实现图层属性过滤的两种方法
转自chanyinhelv原文 在ArcEngine下实现图层属性过滤的两种方法 Normal 0 7.8 磅 0 2 false false false EN-US ZH-CN X-NONE /* ...
- Gibbs 采样的应用
Gibbs 采样的最大作用在于使得对高维连续概率分布的抽样由复杂变得简单. 可能的应用: 计算高维连续概率分布函数的数学期望, Gibbs 采样得到 n 个值,再取均值: 比如用于 RBM:
- javaScript DOM编程经常使用的方法与属性
DOM是Document Object Model文档对象模型的缩写.依据W3C DOM规范,DOM是一种与浏览器,平台,语言无关的接口,使得你能够訪问页面其它的标准组件. Node接口的特性和方法 ...
- ios9 xcode7以后编译需要进行的几项设置
http://blog.csdn.net/hero82748274/article/details/48629461 1.库后缀变了:.dylib->tbd libsqlite3.0.dylib ...
- 理解Erlang/OTP Supervisor
http://www.cnblogs.com/me-sa/archive/2012/01/10/erlang0030.html Supervisors are used to build an hie ...
- 毕设二:python 爬取京东的商品评论
# -*- coding: utf-8 -*- # @author: Tele # @Time : 2019/04/14 下午 3:48 # 多线程版 import time import reque ...
- 极光推送Jpush功能(具体参照官网说明文档,注意此文红色字体)
1.导入框架 2. //推送 #import "APService.h" - (BOOL)application:(UIApplication *)application didF ...
- 【43.26%】【codeforces 732C】Sanatorium
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- AndroidStudio封装SDK的那些事
来自自己简书博客:原文地址:https://www.jianshu.com/p/4d092c915ef1 首先SDK是提供给别人调用的工具.所以常见的SDK都是以jar包,so库,aar包等方式导入A ...