边缘检测方面现状:
边缘检测是数字图像处理识别物体重要的预处理方法,Sobel,Robert,Prewitt,Log等等算子实现方便,处理很快,但是很容易被噪声影响。现实场景的应用噪声都是很多的,canny算子加入了平滑滤波器对这方面问题非常有效,平滑滤波器将会直接影响 Canny 算法的结果。较小的滤波器产生的模糊效果也较少,这样就可以检测较小、变化明显的细线。较大的滤波器产生的模糊效果也较多,将较大的一块图像区域涂成一个特定点的颜色值。Canny细节保留非常完整,但这个优点有时候却成为识别的瓶颈,使识别目标不够突出。
应对这些问题,我看了些相关文献,实现了形态学的边缘检测方法。
常用的边缘检测算子:
⒈Sobel算子
2.Robert算子
3.Prewit算子
4.Laplace算子
5.Canny算子
形态学边缘检测方法:
实现代码:
1 private void morphological(Image<Gray, Byte> src, float theta) 2 { 3 int[,] mask1 = {{1,2,1},{2,8,2},{1,2,1}}; 4 int[,] mask2 = {{0,1,0},{1,1,1},{0,1,0}}; 5 int[,] mask3 = {{1,0,1},{0,1,0},{1,0,1}}; 6 7 StructuringElementEx ele1 = new StructuringElementEx(mask1, 1, 1); 8 StructuringElementEx ele2 = new StructuringElementEx(mask2, 1, 1); 9 StructuringElementEx ele3 = new StructuringElementEx(mask3, 1, 1); 10 11 Image<Gray, Byte> dst1 = new Image<Gray, Byte>(src.Size); //先腐蚀再膨胀 12 Image<Gray, Byte> dst2 = new Image<Gray, Byte>(src.Size); //先膨胀再腐蚀 13 CvInvoke.cvErode(src, dst1, ele1, 1); 14 CvInvoke.cvDilate(dst1, dst1, ele2, 1); 15 CvInvoke.cvDilate(src, dst2, ele1, 1); 16 CvInvoke.cvErode(dst2, dst2, ele2, 1); 17 18 Image<Gray, Byte> G12 = new Image<Gray, byte>(src.Size); 19 Image<Gray, Byte> G22 = new Image<Gray, byte>(src.Size); 20 Image<Gray, Byte> G32 = new Image<Gray, byte>(src.Size); 21 Image<Gray, Byte> G1 = new Image<Gray, byte>(src.Size); 22 Image<Gray, Byte> G2 = new Image<Gray, byte>(src.Size); 23 Image<Gray, Byte> G3 = new Image<Gray, byte>(src.Size); 24 25 //G1 = imdilate(imdilate(imerode(f, b1), b2),b3) - imclose(imdilate(imerode(f, b1), b2), b3); 26 CvInvoke.cvDilate(dst1, G1, ele3, 1); 27 CvInvoke.cvMorphologyEx(dst1, G12,new IntPtr(), ele3, Emgu.CV.CvEnum.CV_MORPH_OP.CV_MOP_CLOSE, 1); 28 G1 = G1 - G12; 29 30 //G2 = imopen(imerode(imdilate(f, b1), b2),b3) - imerode(imerode(imdilate(f, b1), b2),b3); 31 CvInvoke.cvMorphologyEx(dst2, G2, new IntPtr(), ele3, Emgu.CV.CvEnum.CV_MORPH_OP.CV_MOP_OPEN, 1); 32 CvInvoke.cvErode(dst2, G22, ele3, 1); 33 G2 = G2 - G22; 34 35 //G3 = imdilate(imdilate(imerode(f, b1), b2),b3) - imerode(imerode(imdilate(f, b1), b2),b3); 36 CvInvoke.cvDilate(dst1, G3, ele3, 1); 37 CvInvoke.cvErode(dst2, G32, ele3, 1); 38 G3 = G3 - G32; 39 //matsub(G3, G32, G3); 40 41 // Gmin = min(G1, G2); 42 // Gmax = max(G1, G2); 43 Image<Gray, Byte> Gmin = new Image<Gray, byte>(src.Size); 44 Image<Gray, Byte> Gmax = new Image<Gray, byte>(src.Size); 45 Gmin = G1.Min(G2); 46 Gmax = G1.Max(G2); 47 /* 48 int row = 0,col = 0; 49 for (; row < Gmin.Rows; row++) 50 { 51 for (; col < Gmin.Cols; col++) 52 { 53 Gmin.Data[row, col, 0] = Math.Min(G1.Data[row, col, 0], G2.Data[row, col, 0]); 54 Gmax.Data[row, col, 0] = Math.Max(G1.Data[row, col, 0], G2.Data[row, col, 0]); 55 } 56 }*/ 57 58 Gmax = Gmax - Gmin; 59 //matsub(Gmax, Gmin, Gmax); 60 // G = G3 + theta*deltaG; 61 src = G3 + theta * Gmax; 62 /* 63 for (row = 0; row < src.Rows; row++) 64 { 65 for (col = 0; col < src.Cols; col++) 66 { 67 src.Data[row, col, 0] = (byte)(G3.Data[row, col, 0] + theta * Gmax.Data[row, col, 0]); 68 } 69 } 70 imageBox1.Image = src;*/ 71 72 }
实现效果:
形态学边缘检测
附上参考的论文:http://pan.baidu.com/s/1sjQnGaL