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> ...
随机推荐
- Gibbs 采样的应用
Gibbs 采样的最大作用在于使得对高维连续概率分布的抽样由复杂变得简单. 可能的应用: 计算高维连续概率分布函数的数学期望, Gibbs 采样得到 n 个值,再取均值: 比如用于 RBM:
- 【转】A* A星 算法 C语言 实现代码
http://blog.csdn.net/shanshanpt/article/details/8977512 关于A*算法,很早就想写点什么,可是貌似天天在忙活着什么,可事实又没有做什么,真是浮躁啊 ...
- IT增值服务客户案例(二):河南郑州大四实习生,职业规划和项目开发指导
客户整体情况,河南郑州大四在校学生,目前在企业实习,从事Java开发工作.有一定的项目开发经验,对Java周边技术有基本的理解. 客户购买的是"拜师学艺"服务,按月付款. 经过多次 ...
- [Angular] Reactive Form -- FormControl & formControlName, FormGroup, formGroup & formGroupName
First time dealing with Reactive form might be a little bit hard to understand. I have used Angular- ...
- 【codeforces 546A】Soldier and Bananas
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【hdu 3389】Game
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s) ...
- iOS 9和xcode7设置
升级了Xcode7各种问题来了,折腾两天 一.Xcode7 http适配设置 1.大部分社交平台接口不支持https协议. 2.大部分社交平台SDK不支持bitcode. 3.添加Scheme白名单 ...
- 【最大M子段和】dp + 滚动数组
题目描述 给定 n 个数求这 n 个数划分成互不相交的 m 段的最大 m 子段和. 给出一段整数序列 A1,A2,A3,A4,...,Ax,...,An ,其中 1≤x≤n≤1,000,000, -3 ...
- webcollector + selenium 爬取空间相册图片
package cn.hb.util; import java.io.File; import java.io.FileNotFoundException; import java.io.FileWr ...
- spring quartz使用多线程并发“陷阱”
定义一个job:ranJob,设置每秒执行一次,设置不允许覆盖并发执行 <bean id="rankJob" class="com.chinacache.www.l ...