1. 用OpenCV验证常用边缘检测方法,
1public class EdgeDetection {
2
3 private final static String path=System.getProperty("user.dir")+"\\catton.jpg";
4 static{
5 platformUtils.loadLibraries();
6 }
7
8 public static void main(String[] args) {
9 Mat srcimage;
10 //读取一张灰度图片
11 srcimage= Imgcodecs.imread(path,Imgcodecs.IMREAD_COLOR);
12 if (srcimage.empty()) {
13 System.err.println("加载图片出错,请检查图片路径!");
14 return;
15 }
16 System.out.println(srcimage);
17 // 索贝尔水平检测 边缘检测
18 // Imgproc.Sobel(srcimage,dstimg, CvType.CV_8UC1,1, 0);
19
20 //索贝尔垂直检测
21 Mat Sobel_dstimg=new Mat();
22 // Imgproc.Sobel(srcimage, dstimg, CvType.CV_8UC1,0, 1);
23
24 // Canny 边缘检测
25 Mat canny_dstimg=new Mat();
26 Imgproc.Canny(srcimage,canny_dstimg,50,240);
27
28 //拉普拉斯边检测
29 Mat laplacian_dstimg=new Mat();
30 Imgproc.Laplacian(srcimage,laplacian_dstimg,CvType.CV_8UC1);
31
32 HighGui.imshow("边缘检测 原图像",srcimage);
33 HighGui.imshow("边缘检测 Canny输出图像",canny_dstimg);
34 HighGui.imshow("边缘检测 Laplacian输出图像",laplacian_dstimg);
35 // 无限等待按键按下
36 HighGui.waitKey(0);
37 }
38}

2. 用OpenCV验证Hough变换检测图像中的直线或圆
1package com.gitee.dgw.lesson9;
2
3import com.gitee.dgw.lesson1.platformUtils;
4import org.opencv.core.Mat;
5import org.opencv.core.Point;
6import org.opencv.core.Scalar;
7import org.opencv.highgui.HighGui;
8import org.opencv.imgcodecs.Imgcodecs;
9import org.opencv.imgproc.Imgproc;
10
11
12
131415161718
19public class HoughDetect {
20
21 private final static String path=System.getProperty("user.dir")+"\\line.png";
22 static{
23 platformUtils.loadLibraries();
24 }
25 public static void main(String[] args) {
26
27 Mat srcimage;
28 //读取一张灰度图片
29 srcimage= Imgcodecs.imread(path,Imgcodecs.IMREAD_COLOR);
30 if (srcimage.empty()) {
31 System.err.println("加载图片出错,请检查图片路径!");
32 return;
33 }
34 System.out.println(srcimage);
35 Mat dst_img=srcimage.clone();
36 Imgproc.Canny(srcimage, dst_img, 200, 500, 5, false);
37
38 Imgproc.cvtColor(srcimage,dst_img,Imgproc.COLOR_RGB2GRAY);
39 //dectCircle(srcimage, dst_img);
40 Mat storage = new Mat();
41 Imgproc.HoughLinesP(dst_img, storage, 1, Math.PI / 180, 50, 0, 0);
42 for (int x = 0; x < storage.rows(); x++)
43 {
44 double[] vec = storage.get(x, 0);
45 double x1 = vec[0], y1 = vec[1], x2 = vec[2], y2 = vec[3];
46 Point start = new Point(x1, y1);
47 Point end = new Point(x2, y2);
48 Imgproc.line(srcimage, start, end, new Scalar(0, 0, 255, 255), 1, Imgproc.LINE_4, 0);
49 }
50 HighGui.imshow("边缘检测 原图像",dst_img);
51 HighGui.imshow("边缘检测 Canny输出图像",srcimage);
52 // 无限等待按键按下
53 HighGui.waitKey(0);
54 }
55
56 private static void dectCircle(Mat srcimage, Mat dst_img) {
57 Mat circles = new Mat();
58 Imgproc.HoughCircles(dst_img, circles, Imgproc.HOUGH_GRADIENT, 1, 100, 440, 50, 0, 345);
59 //概率霍夫变换)
60 for (int i = 0; i < circles.cols(); i++)
61 {
62 double[] vCircle = circles.get(0, i);
63 Point center = new Point(vCircle[0], vCircle[1]);
64 int radius = (int) Math.round(vCircle[2]);
65 // circle center
66 Imgproc.circle(srcimage, center, 3, new Scalar(0, 255, 0), -1, 8, 0);
67 // circle outline
68 Imgproc.circle(srcimage, center, radius, new Scalar(0, 0, 255), 3, 8, 0);
69 }
70 }
71}
