图像处理之Canny边缘检测

http://blog.csdn.net/jia20003/article/details/41173767

图像处理之Canny 边缘检测

一:历史

Canny边缘检测算法是1986年有John F. Canny开发出来一种基于图像梯度计算的边缘

检测算法,同时Canny本人对计算图像边缘提取学科的发展也是做出了很多的贡献。尽

管至今已经许多年过去,但是该算法仍然是图像边缘检测方法经典算法之一。

二:Canny边缘检测算法

经典的Canny边缘检测算法通常都是从高斯模糊开始,到基于双阈值实现边缘连接结束

。但是在实际工程应用中,考虑到输入图像都是彩色图像,最终边缘连接之后的图像要

二值化输出显示,所以完整的Canny边缘检测算法实现步骤如下:

1.      彩色图像转换为灰度图像

2.      对图像进行高斯模糊

3.      计算图像梯度,根据梯度计算图像边缘幅值与角度

4.      非最大信号压制处理(边缘细化)

5.      双阈值边缘连接处理

6.      二值化图像输出结果

三:各步详解与代码实现

1.      彩色图像转灰度图像

根据彩色图像RGB转灰度公式:gray  =  R * 0.299 + G * 0.587 + B * 0.114

将彩色图像中每个RGB像素转为灰度值的代码如下:

[java] view plaincopy

  1. <span style="font-size:18px;">int gray = (int) (0.299 * tr + 0.587 * tg + 0.114 * tb);</span>

2.      对图像进行高斯模糊

图像高斯模糊时,首先要根据输入参数确定高斯方差与窗口大小,这里我设置默认方

差值窗口大小为16x16,根据这两个参数生成高斯卷积核算子的代码如下:

[java] view plaincopy

  1. <span style="font-size:18px;">      float kernel[][] = new float[gaussianKernelWidth][gaussianKernelWidth];
  2. for(int x=0; x<gaussianKernelWidth; x++)
  3. {
  4. for(int y=0; y<gaussianKernelWidth; y++)
  5. {
  6. kernel[x][y] = gaussian(x, y, gaussianKernelRadius);
  7. }
  8. }</span>

获取了高斯卷积算子之后,我们就可以对图像高斯卷积模糊,关于高斯图像模糊更详

细的解释可以参见这里:http://blog.csdn.net/jia20003/article/details/7234741实现

图像高斯卷积模糊的代码如下:

[java] view plaincopy

  1. <span style="font-size:18px;">// 高斯模糊 -灰度图像
  2. int krr = (int)gaussianKernelRadius;
  3. for (int row = 0; row < height; row++) {
  4. for (int col = 0; col < width; col++) {
  5. index = row * width + col;
  6. double weightSum = 0.0;
  7. double redSum = 0;
  8. for(int subRow=-krr; subRow<=krr; subRow++)
  9. {
  10. int nrow = row + subRow;
  11. if(nrow >= height || nrow < 0)
  12. {
  13. nrow = 0;
  14. }
  15. for(int subCol=-krr; subCol<=krr; subCol++)
  16. {
  17. int ncol = col + subCol;
  18. if(ncol >= width || ncol <=0)
  19. {
  20. ncol = 0;
  21. }
  22. int index2 = nrow * width + ncol;
  23. int tr1 = (inPixels[index2] >> 16) & 0xff;
  24. redSum += tr1*kernel[subRow+krr][subCol+krr];
  25. weightSum += kernel[subRow+krr][subCol+krr];
  26. }
  27. }
  28. int gray = (int)(redSum / weightSum);
  29. outPixels[index] = gray;
  30. }
  31. }</span>

3.      计算图像X方向与Y方向梯度,根据梯度计算图像边缘幅值与角度大小

高斯模糊的目的主要为了整体降低图像噪声,目的是为了更准确计算图像梯度及边缘

幅值。计算图像梯度可以选择算子有Robot算子、Sobel算子、Prewitt算子等。关于

图像梯度计算更多的解释可以看这里:

http://blog.csdn.net/jia20003/article/details/7664777。

这里采用更加简单明了的2x2的算子,其数学表达如下:

[java] view plaincopy

  1. <span style="font-size:18px;">// 计算梯度-gradient, X放与Y方向
  2. data = new float[width * height];
  3. magnitudes = new float[width * height];
  4. for (int row = 0; row < height; row++) {
  5. for (int col = 0; col < width; col++) {
  6. index = row * width + col;
  7. // 计算X方向梯度
  8. float xg = (getPixel(outPixels, width, height, col, row+1) -
  9. getPixel(outPixels, width, height, col, row) +
  10. getPixel(outPixels, width, height, col+1, row+1) -
  11. getPixel(outPixels, width, height, col+1, row))/2.0f;
  12. float yg = (getPixel(outPixels, width, height, col, row)-
  13. getPixel(outPixels, width, height, col+1, row) +
  14. getPixel(outPixels, width, height, col, row+1) -
  15. getPixel(outPixels, width, height, col+1, row+1))/2.0f;
  16. // 计算振幅与角度
  17. data[index] = hypot(xg, yg);
  18. if(xg == 0)
  19. {
  20. if(yg > 0)
  21. {
  22. magnitudes[index]=90;
  23. }
  24. if(yg < 0)
  25. {
  26. magnitudes[index]=-90;
  27. }
  28. }
  29. else if(yg == 0)
  30. {
  31. magnitudes[index]=0;
  32. }
  33. else
  34. {
  35. magnitudes[index] = (float)((Math.atan(yg/xg) * 180)/Math.PI);
  36. }
  37. // make it 0 ~ 180
  38. magnitudes[index] += 90;
  39. }
  40. }</span>

在获取了图像每个像素的边缘幅值与角度之后

4.      非最大信号压制

信号压制本来是数字信号处理中经常用的,这里的非最大信号压制主要目的是实现边

缘细化,通过该步处理边缘像素进一步减少。非最大信号压制主要思想是假设3x3的

像素区域,中心像素P(x,y) 根据上一步中计算得到边缘角度值angle,可以将角度分

为四个离散值0、45、90、135分类依据如下:

其中黄色区域取值范围为0~22.5 与157.5~180

绿色区域取值范围为22.5 ~ 67.5

蓝色区域取值范围为67.5~112.5

红色区域取值范围为112.5~157.5

分别表示上述四个离散角度的取值范围。得到角度之后,比较中心像素角度上相邻

两个像素,如果中心像素小于其中任意一个,则舍弃该边缘像素点,否则保留。一

个简单的例子如下:

[java] view plaincopy

  1. <span style="font-size:18px;">// 非最大信号压制算法 3x3
  2. Arrays.fill(magnitudes, 0);
  3. for (int row = 0; row < height; row++) {
  4. for (int col = 0; col < width; col++) {
  5. index = row * width + col;
  6. float angle = magnitudes[index];
  7. float m0 = data[index];
  8. magnitudes[index] = m0;
  9. if(angle >=0 && angle < 22.5) // angle 0
  10. {
  11. float m1 = getPixel(data, width, height, col-1, row);
  12. float m2 = getPixel(data, width, height, col+1, row);
  13. if(m0 < m1 || m0 < m2)
  14. {
  15. magnitudes[index] = 0;
  16. }
  17. }
  18. else if(angle >= 22.5 && angle < 67.5) // angle +45
  19. {
  20. float m1 = getPixel(data, width, height, col+1, row-1);
  21. float m2 = getPixel(data, width, height, col-1, row+1);
  22. if(m0 < m1 || m0 < m2)
  23. {
  24. magnitudes[index] = 0;
  25. }
  26. }
  27. else if(angle >= 67.5 && angle < 112.5) // angle 90
  28. {
  29. float m1 = getPixel(data, width, height, col, row+1);
  30. float m2 = getPixel(data, width, height, col, row-1);
  31. if(m0 < m1 || m0 < m2)
  32. {
  33. magnitudes[index] = 0;
  34. }
  35. }
  36. else if(angle >=112.5 && angle < 157.5) // angle 135 / -45
  37. {
  38. float m1 = getPixel(data, width, height, col-1, row-1);
  39. float m2 = getPixel(data, width, height, col+1, row+1);
  40. if(m0 < m1 || m0 < m2)
  41. {
  42. magnitudes[index] = 0;
  43. }
  44. }
  45. else if(angle >=157.5) // angle 0
  46. {
  47. float m1 = getPixel(data, width, height, col, row+1);
  48. float m2 = getPixel(data, width, height, col, row-1);
  49. if(m0 < m1 || m0 < m2)
  50. {
  51. magnitudes[index] = 0;
  52. }
  53. }
  54. }
  55. }</span>

1.      双阈值边缘连接

非最大信号压制以后,输出的幅值如果直接显示结果可能会少量的非边缘像素被包

含到结果中,所以要通过选取阈值进行取舍,传统的基于一个阈值的方法如果选择

的阈值较小起不到过滤非边缘的作用,如果选择的阈值过大容易丢失真正的图像边

缘,Canny提出基于双阈值(Fuzzy threshold)方法很好的实现了边缘选取,在实际

应用中双阈值还有边缘连接的作用。双阈值选择与边缘连接方法通过假设两个阈值

其中一个为高阈值TH另外一个为低阈值TL则有

a.      对于任意边缘像素低于TL的则丢弃

b.      对于任意边缘像素高于TH的则保留

c.      对于任意边缘像素值在TL与TH之间的,如果能通过边缘连接到一个像素大于

TH而且边缘所有像素大于最小阈值TL的则保留,否则丢弃。代码实现如下:

[java] view plaincopy

  1. <span style="font-size:18px;">Arrays.fill(data, 0);
  2. int offset = 0;
  3. for (int row = 0; row < height; row++) {
  4. for (int col = 0; col < width; col++) {
  5. if(magnitudes[offset] >= highThreshold && data[offset] == 0)
  6. {
  7. edgeLink(col, row, offset, lowThreshold);
  8. }
  9. offset++;
  10. }
  11. }</span>

基于递归的边缘寻找方法edgeLink的代码如下:

[java] view plaincopy

  1. <span style="font-size:18px;">private void edgeLink(int x1, int y1, int index, float threshold) {
  2. int x0 = (x1 == 0) ? x1 : x1 - 1;
  3. int x2 = (x1 == width - 1) ? x1 : x1 + 1;
  4. int y0 = y1 == 0 ? y1 : y1 - 1;
  5. int y2 = y1 == height -1 ? y1 : y1 + 1;
  6. data[index] = magnitudes[index];
  7. for (int x = x0; x <= x2; x++) {
  8. for (int y = y0; y <= y2; y++) {
  9. int i2 = x + y * width;
  10. if ((y != y1 || x != x1)
  11. && data[i2] == 0
  12. && magnitudes[i2] >= threshold) {
  13. edgeLink(x, y, i2, threshold);
  14. return;
  15. }
  16. }
  17. }
  18. }</span>

6.      结果二值化显示 - 不说啦,直接点,自己看吧,太简单啦

[java] view plaincopy

  1. <span style="font-size:18px;">// 二值化显示
  2. for(int i=0; i<inPixels.length; i++)
  3. {
  4. int gray = clamp((int)data[i]);
  5. outPixels[i] = gray > 0 ? -1 : 0xff000000;
  6. }</span>

最终运行结果:


四:完整的Canny算法源代码

[java] view plaincopy

  1. package com.gloomyfish.filter.study;
  2. import java.awt.image.BufferedImage;
  3. import java.util.Arrays;
  4. public class CannyEdgeFilter extends AbstractBufferedImageOp {
  5. private float gaussianKernelRadius = 2f;
  6. private int gaussianKernelWidth = 16;
  7. private float lowThreshold;
  8. private float highThreshold;
  9. // image width, height
  10. private int width;
  11. private int height;
  12. private float[] data;
  13. private float[] magnitudes;
  14. public CannyEdgeFilter() {
  15. lowThreshold = 2.5f;
  16. highThreshold = 7.5f;
  17. gaussianKernelRadius = 2f;
  18. gaussianKernelWidth = 16;
  19. }
  20. public float getGaussianKernelRadius() {
  21. return gaussianKernelRadius;
  22. }
  23. public void setGaussianKernelRadius(float gaussianKernelRadius) {
  24. this.gaussianKernelRadius = gaussianKernelRadius;
  25. }
  26. public int getGaussianKernelWidth() {
  27. return gaussianKernelWidth;
  28. }
  29. public void setGaussianKernelWidth(int gaussianKernelWidth) {
  30. this.gaussianKernelWidth = gaussianKernelWidth;
  31. }
  32. public float getLowThreshold() {
  33. return lowThreshold;
  34. }
  35. public void setLowThreshold(float lowThreshold) {
  36. this.lowThreshold = lowThreshold;
  37. }
  38. public float getHighThreshold() {
  39. return highThreshold;
  40. }
  41. public void setHighThreshold(float highThreshold) {
  42. this.highThreshold = highThreshold;
  43. }
  44. @Override
  45. public BufferedImage filter(BufferedImage src, BufferedImage dest) {
  46. width = src.getWidth();
  47. height = src.getHeight();
  48. if (dest == null)
  49. dest = createCompatibleDestImage(src, null);
  50. // 图像灰度化
  51. int[] inPixels = new int[width * height];
  52. int[] outPixels = new int[width * height];
  53. getRGB(src, 0, 0, width, height, inPixels);
  54. int index = 0;
  55. for (int row = 0; row < height; row++) {
  56. int ta = 0, tr = 0, tg = 0, tb = 0;
  57. for (int col = 0; col < width; col++) {
  58. index = row * width + col;
  59. ta = (inPixels[index] >> 24) & 0xff;
  60. tr = (inPixels[index] >> 16) & 0xff;
  61. tg = (inPixels[index] >> 8) & 0xff;
  62. tb = inPixels[index] & 0xff;
  63. int gray = (int) (0.299 * tr + 0.587 * tg + 0.114 * tb);
  64. inPixels[index] = (ta << 24) | (gray << 16) | (gray << 8)
  65. | gray;
  66. }
  67. }
  68. // 计算高斯卷积核
  69. float kernel[][] = new float[gaussianKernelWidth][gaussianKernelWidth];
  70. for(int x=0; x<gaussianKernelWidth; x++)
  71. {
  72. for(int y=0; y<gaussianKernelWidth; y++)
  73. {
  74. kernel[x][y] = gaussian(x, y, gaussianKernelRadius);
  75. }
  76. }
  77. // 高斯模糊 -灰度图像
  78. int krr = (int)gaussianKernelRadius;
  79. for (int row = 0; row < height; row++) {
  80. for (int col = 0; col < width; col++) {
  81. index = row * width + col;
  82. double weightSum = 0.0;
  83. double redSum = 0;
  84. for(int subRow=-krr; subRow<=krr; subRow++)
  85. {
  86. int nrow = row + subRow;
  87. if(nrow >= height || nrow < 0)
  88. {
  89. nrow = 0;
  90. }
  91. for(int subCol=-krr; subCol<=krr; subCol++)
  92. {
  93. int ncol = col + subCol;
  94. if(ncol >= width || ncol <=0)
  95. {
  96. ncol = 0;
  97. }
  98. int index2 = nrow * width + ncol;
  99. int tr1 = (inPixels[index2] >> 16) & 0xff;
  100. redSum += tr1*kernel[subRow+krr][subCol+krr];
  101. weightSum += kernel[subRow+krr][subCol+krr];
  102. }
  103. }
  104. int gray = (int)(redSum / weightSum);
  105. outPixels[index] = gray;
  106. }
  107. }
  108. // 计算梯度-gradient, X放与Y方向
  109. data = new float[width * height];
  110. magnitudes = new float[width * height];
  111. for (int row = 0; row < height; row++) {
  112. for (int col = 0; col < width; col++) {
  113. index = row * width + col;
  114. // 计算X方向梯度
  115. float xg = (getPixel(outPixels, width, height, col, row+1) -
  116. getPixel(outPixels, width, height, col, row) +
  117. getPixel(outPixels, width, height, col+1, row+1) -
  118. getPixel(outPixels, width, height, col+1, row))/2.0f;
  119. float yg = (getPixel(outPixels, width, height, col, row)-
  120. getPixel(outPixels, width, height, col+1, row) +
  121. getPixel(outPixels, width, height, col, row+1) -
  122. getPixel(outPixels, width, height, col+1, row+1))/2.0f;
  123. // 计算振幅与角度
  124. data[index] = hypot(xg, yg);
  125. if(xg == 0)
  126. {
  127. if(yg > 0)
  128. {
  129. magnitudes[index]=90;
  130. }
  131. if(yg < 0)
  132. {
  133. magnitudes[index]=-90;
  134. }
  135. }
  136. else if(yg == 0)
  137. {
  138. magnitudes[index]=0;
  139. }
  140. else
  141. {
  142. magnitudes[index] = (float)((Math.atan(yg/xg) * 180)/Math.PI);
  143. }
  144. // make it 0 ~ 180
  145. magnitudes[index] += 90;
  146. }
  147. }
  148. // 非最大信号压制算法 3x3
  149. Arrays.fill(magnitudes, 0);
  150. for (int row = 0; row < height; row++) {
  151. for (int col = 0; col < width; col++) {
  152. index = row * width + col;
  153. float angle = magnitudes[index];
  154. float m0 = data[index];
  155. magnitudes[index] = m0;
  156. if(angle >=0 && angle < 22.5) // angle 0
  157. {
  158. float m1 = getPixel(data, width, height, col-1, row);
  159. float m2 = getPixel(data, width, height, col+1, row);
  160. if(m0 < m1 || m0 < m2)
  161. {
  162. magnitudes[index] = 0;
  163. }
  164. }
  165. else if(angle >= 22.5 && angle < 67.5) // angle +45
  166. {
  167. float m1 = getPixel(data, width, height, col+1, row-1);
  168. float m2 = getPixel(data, width, height, col-1, row+1);
  169. if(m0 < m1 || m0 < m2)
  170. {
  171. magnitudes[index] = 0;
  172. }
  173. }
  174. else if(angle >= 67.5 && angle < 112.5) // angle 90
  175. {
  176. float m1 = getPixel(data, width, height, col, row+1);
  177. float m2 = getPixel(data, width, height, col, row-1);
  178. if(m0 < m1 || m0 < m2)
  179. {
  180. magnitudes[index] = 0;
  181. }
  182. }
  183. else if(angle >=112.5 && angle < 157.5) // angle 135 / -45
  184. {
  185. float m1 = getPixel(data, width, height, col-1, row-1);
  186. float m2 = getPixel(data, width, height, col+1, row+1);
  187. if(m0 < m1 || m0 < m2)
  188. {
  189. magnitudes[index] = 0;
  190. }
  191. }
  192. else if(angle >=157.5) // angle 0
  193. {
  194. float m1 = getPixel(data, width, height, col, row+1);
  195. float m2 = getPixel(data, width, height, col, row-1);
  196. if(m0 < m1 || m0 < m2)
  197. {
  198. magnitudes[index] = 0;
  199. }
  200. }
  201. }
  202. }
  203. // 寻找最大与最小值
  204. float min = 255;
  205. float max = 0;
  206. for(int i=0; i<magnitudes.length; i++)
  207. {
  208. if(magnitudes[i] == 0) continue;
  209. min = Math.min(min, magnitudes[i]);
  210. max = Math.max(max, magnitudes[i]);
  211. }
  212. System.out.println("Image Max Gradient = " + max + " Mix Gradient = " + min);
  213. // 通常比值为 TL : TH = 1 : 3, 根据两个阈值完成二值化边缘连接
  214. // 边缘连接-link edges
  215. Arrays.fill(data, 0);
  216. int offset = 0;
  217. for (int row = 0; row < height; row++) {
  218. for (int col = 0; col < width; col++) {
  219. if(magnitudes[offset] >= highThreshold && data[offset] == 0)
  220. {
  221. edgeLink(col, row, offset, lowThreshold);
  222. }
  223. offset++;
  224. }
  225. }
  226. // 二值化显示
  227. for(int i=0; i<inPixels.length; i++)
  228. {
  229. int gray = clamp((int)data[i]);
  230. outPixels[i] = gray > 0 ? -1 : 0xff000000;
  231. }
  232. setRGB(dest, 0, 0, width, height, outPixels );
  233. return dest;
  234. }
  235. public int clamp(int value) {
  236. return value > 255 ? 255 :
  237. (value < 0 ? 0 : value);
  238. }
  239. private void edgeLink(int x1, int y1, int index, float threshold) {
  240. int x0 = (x1 == 0) ? x1 : x1 - 1;
  241. int x2 = (x1 == width - 1) ? x1 : x1 + 1;
  242. int y0 = y1 == 0 ? y1 : y1 - 1;
  243. int y2 = y1 == height -1 ? y1 : y1 + 1;
  244. data[index] = magnitudes[index];
  245. for (int x = x0; x <= x2; x++) {
  246. for (int y = y0; y <= y2; y++) {
  247. int i2 = x + y * width;
  248. if ((y != y1 || x != x1)
  249. && data[i2] == 0
  250. && magnitudes[i2] >= threshold) {
  251. edgeLink(x, y, i2, threshold);
  252. return;
  253. }
  254. }
  255. }
  256. }
  257. private float getPixel(float[] input, int width, int height, int col,
  258. int row) {
  259. if(col < 0 || col >= width)
  260. col = 0;
  261. if(row < 0 || row >= height)
  262. row = 0;
  263. int index = row * width + col;
  264. return input[index];
  265. }
  266. private float hypot(float x, float y) {
  267. return (float) Math.hypot(x, y);
  268. }
  269. private int getPixel(int[] inPixels, int width, int height, int col,
  270. int row) {
  271. if(col < 0 || col >= width)
  272. col = 0;
  273. if(row < 0 || row >= height)
  274. row = 0;
  275. int index = row * width + col;
  276. return inPixels[index];
  277. }
  278. private float gaussian(float x, float y, float sigma) {
  279. float xDistance = x*x;
  280. float yDistance = y*y;
  281. float sigma22 = 2*sigma*sigma;
  282. float sigma22PI = (float)Math.PI * sigma22;
  283. return (float)Math.exp(-(xDistance + yDistance)/sigma22)/sigma22PI;
  284. }
  285. }

转载请务必注明出自本博客-gloomyfish

时间: 2024-07-30 10:15:47

图像处理之Canny边缘检测的相关文章

六 OpenCV图像处理4 Canny 边缘检测

1.Canny 边缘检测原理 步骤: ·1噪声去除: 由于边缘检测很容易受到噪声影响,所以第一步是使用 5x5 的高斯滤波器 去除噪声 ·2计算图像梯度: 对平滑后的图像使用 Sobel 算子计算水平方向和竖直方向的一阶导数(图 像梯度)(Gx 和 Gy) 根据得到的这两幅梯度图(Gx 和 Gy)找到边界的梯 度和方向 梯度的方向一般总是与边界垂直.梯度方向被归为四类:垂直,水平,和 两个对角线. ·3非极大值抑制 在获得梯度的方向和大小之后,应该对整幅图像做一个扫描,去除那些非 边界上的点.对

[转载+原创]Emgu CV on C# (六) —— Emgu CV on Canny边缘检测

Canny边缘检测也是一种边缘检测方法,本文介绍了Canny边缘检测的函数及其使用方法,并利用emgucv方法将轮廓检测解算的结果与原文进行比较. 图像的边缘检测的原理是检测出图像中所有灰度值变化较大的点,而且这些点连接起来就构成了若干线条,这些线条就可以称为图像的边缘.Canny边缘检测算子是John F. Canny于 1986 年开发出来的一个多级边缘检测算法. Canny 边缘检测的数学原理和算法实现这里就不再了,有兴趣的读者可以查阅专业书籍. 一.概述(若果不想看,可以略过.转自:<C

Log和Canny边缘检测(附Matlab程序)

  一. 实验目的 (1) 通过实验分析不同尺度下LOG和Canny边缘提取算子的性能. (2) 研究这两种边缘提取方法在不同参数下的边缘提取能力. (3) 使用不同的滤波尺度和添加噪声能量(噪声水平),通过与无噪声图像对比,选择最能说明自己结论的滤波尺度和噪声水平,并做出分析说明. 二. 实验原理 边缘的含义:在数字图像中,边缘是指图像局部变化最显著的部分,边缘主要存在于目标与目标,目标与背景之间,是图像局部特性的不连续性,如灰度的突变.纹理结构的突变.颜色的突变等.尽管图像的边缘点产生的原因

Canny边缘检测算法

作为对比,先看一下Sobel的原理: Sobel的原理: 索贝尔算子(Sobeloperator)是图像处理中的算子之一,主要用作边缘检测.在技术上,它是一离散性差分算子,用来运算图像亮度函数的梯度之近似值.在图像的任何一点使用此算子,将会产生对应的梯度矢量或是其法矢量. 该算子包含两组3x3的矩阵,分别为横向及纵向,将之与图像作平面卷积,即可分别得出横向及纵向的亮度差分近似值.如果以A代表原始图像,Gx及Gy分别代表经横向及纵向边缘检测的图像 Canny边缘检测算子的matlab实现 在以上例

基于opencv下对视频的灰度变换,高斯滤波,canny边缘检测处理,同窗体显示并保存

如题:使用opencv打开摄像头或视频文件,实时显示原始视频,将视频每一帧依次做灰度转换.高斯滤波.canny边缘检测处理(原始视频和这3个中间步骤处理结果分别在一个窗口显示),最后将边缘检测结果保存为一个视频avi文件. 这里问题综合性比较大,这里进行分治. 该类问题可分为四个方面的处理: (1)打开 视频或者是摄像头,并播放视频 (2)对视频的每一帧做处理 (3)同窗体显示四个结果 (4)保存视频文件 以下分为这三个方面进行处理: (1)打开 视频或者摄像头,并播放视频 这个利用opencv

Canny边缘检测算法原理及其VC实现详解(二)

转自:http://blog.csdn.net/likezhaobin/article/details/6892629 3.  Canny算法的实现流程 由于本文主要目的在于学习和实现算法,而对于图像读取.视频获取等内容不进行阐述.因此选用OpenCV算法库作为其他功能的实现途径(关于OpenCV的使用,作者将另文表述).首先展现本文将要处理的彩色图片. 图2 待处理的图像 3.1 图像读取和灰度化 编程时采用上文所描述的第二种方法来实现图像的灰度化.其中ptr数组中保存的灰度化后的图像数据.具

算法解剖系列(1)-Canny边缘检测原理

Canny边缘检测算子 基本原理 须满足条件:抑制噪声:精确定位边缘. 从数学上表达了三个准则[信噪比准则(低错误率).定位精度准则.单边缘响应准则],并寻找表达式的最佳解. 属于先平滑后求导的方法. 算法实现步骤 1.使用高斯滤波平滑图像 令f(x,y)表示数据(输入源数据),G(x,y)表示二维高斯函数(卷积操作数),fs(x,y)为卷积平滑后的图像. G(x,y)=12πσ2e?(x2+y2)2σ2 fs(x,y)=f(x,y)?G(x,y) Guess过程 用坐标点(x,y)表示一个3×

Canny边缘检测算法原理及其VC实现详解(一)

转自:http://blog.csdn.net/likezhaobin/article/details/6892176 图象的边缘是指图象局部区域亮度变化显著的部分,该区域的灰度剖面一般可以看作是一个阶跃,既从一个灰度值在很小的缓冲区域内急剧变化到另一个灰度相差较大的灰度值.图象的边缘部分集中了图象的大部分信息,图象边缘的确定与提取对于整个图象场景的识别与理解是非常重要的,同时也是图象分割所依赖的重要特征,边缘检测主要是图象的灰度变化的度量.检测和定位,自从1959提出边缘检测以来,经过五十多年

Canny边缘检测原理及C#程序实现

http://blog.csdn.net/yjz_uestc/article/details/6664937 Canny边缘检测是被公认的检测效果最好的边缘检测方法,是由John F. Canny于1986年提出,算法目标是找出一个最优的边缘检测的方法,所谓最优即:1.好的检测:算法能够尽可能的标识出图像的边缘:2.好的定位:标识出的边缘要尽可能的与实际边缘相接近:3.最小响应:图像中的边缘只能标识一次,并且不能把噪声标识成边缘.同时我们也要满足3个准则:信噪比准则.定位精度准则.单边缘响应准则