Java OpenCV 图像处理27.4 FAST 角点检测
FAST特征检测器FastFeatureDetector FAST特征检测的特点是简单、快速、有效。作者为了在实时帧速率情况下进行高速特征检测,提出FAST特征检测。 相比SIFT、DoG、Harris、SUSAN等比较耗时的特征检测方法,FAST只利用周围的像素进行比较,速度大大加快(FAST只是一种特征点检测算法,并不涉及特征点的特征描述)。
package com.xu.opencv;
import java.io.File;
import org.opencv.core.Mat;
import org.opencv.core.MatOfKeyPoint;
import org.opencv.core.Scalar;
import org.opencv.features2d.FastFeatureDetector;
import org.opencv.features2d.Features2d;
import org.opencv.highgui.HighGui;
import org.opencv.imgcodecs.Imgcodecs;
public class Test {
static {
String os = System.getProperty("os.name");
String type = System.getProperty("sun.arch.data.model");
if (os.toUpperCase().contains("WINDOWS")) {
File lib;
if (type.endsWith("64")) {
lib = new File("D:\\Learn\\OpenCV\\OpenCV-4.5.5\\build\\java\\x64\\" + System.mapLibraryName("opencv_java455"));
} else {
lib = new File("D:\\Learn\\OpenCV\\OpenCV-4.5.5\\build\\java\\x86\\" + System.mapLibraryName("opencv_java455"));
}
System.load(lib.getAbsolutePath());
}
}
public static void main(String[] args) {
Mat src = Imgcodecs.imread("D:\\OneDrive\\桌面\\5.jpeg");
FastFeatureDetector fd = FastFeatureDetector.create(FastFeatureDetector.THRESHOLD);
MatOfKeyPoint regions = new MatOfKeyPoint();
fd.detect(src, regions);
Features2d.drawKeypoints(src, regions, src, new Scalar(0, 0, 255), Features2d.DrawMatchesFlags_DRAW_RICH_KEYPOINTS);
HighGui.imshow("", src);
HighGui.waitKey(0);
}
}
序号 |
名称 |
类 |
1 |
FAST 角点检测 |
FastFeatureDetector.create(FastFeatureDetector.THRESHOLD) |
2 |
ORB 角点检测 |
ORB.create(800, 1.2f, 8, 31, 0, 2, ORB.HARRIS_SCORE, 3, 3) |
3 |
SIFT 角点检测 |
SIFT.create(0, 3, 0.04, 10, 1.6) |
4 |
Harris 角点检测 |
Imgproc.cornerHarris(gray, dst, 3, 15, 0.04) |
5 |
Shi-Tomasi 角点检测 |
Imgproc.goodFeaturesToTrack(gray, corners, 200, 0.01, 10, new Mat(), 3, 5, false, 0.04) |
package com.xu.image;
import java.io.File;
import org.opencv.core.Mat;
import org.opencv.core.MatOfKeyPoint;
import org.opencv.core.MatOfPoint;
import org.opencv.core.Point;
import org.opencv.core.Scalar;
import org.opencv.features2d.FastFeatureDetector;
import org.opencv.features2d.Features2d;
import org.opencv.features2d.ORB;
import org.opencv.features2d.SIFT;
import org.opencv.highgui.HighGui;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
public class CornerPoint {
static {
String os = System.getProperty("os.name");
String type = System.getProperty("sun.arch.data.model");
if (os.toUpperCase().contains("WINDOWS")) {
File lib;
if (type.endsWith("64")) {
lib = new File("D:\\Learn\\OpenCV\\OpenCV-4.5.5\\build\\java\\x64\\" + System.mapLibraryName("opencv_java455"));
} else {
lib = new File("D:\\Learn\\OpenCV\\OpenCV-4.5.5\\build\\java\\x86\\" + System.mapLibraryName("opencv_java455"));
}
System.load(lib.getAbsolutePath());
}
}
public static void main(String[] args) {
Mat src = Imgcodecs.imread("D:\\OneDrive\\桌面\\5.jpeg");
HighGui.imshow("原图", src);
shi_tomasi();
}
public static void fast() {
Mat src = Imgcodecs.imread("D:\\OneDrive\\桌面\\5.jpeg");
FastFeatureDetector fd = FastFeatureDetector.create(FastFeatureDetector.THRESHOLD);
MatOfKeyPoint regions = new MatOfKeyPoint();
fd.detect(src, regions);
Features2d.drawKeypoints(src, regions, src, new Scalar(0, 0, 255), Features2d.DrawMatchesFlags_DRAW_RICH_KEYPOINTS);
HighGui.imshow("FAST 角点检测", src);
HighGui.waitKey(0);
}
public static void orb() {
Mat src = Imgcodecs.imread("D:\\OneDrive\\桌面\\5.jpeg");
Mat gray = new Mat();
Imgproc.cvtColor(src, gray, Imgproc.COLOR_BGR2GRAY);
ORB orb = ORB.create(800, 1.2f, 8, 31, 0, 2, ORB.HARRIS_SCORE, 3, 3);
MatOfKeyPoint point = new MatOfKeyPoint();
orb.detect(gray, point);
Features2d.drawKeypoints(src, point, src, new Scalar(0, 0, 255), Features2d.DrawMatchesFlags_DRAW_RICH_KEYPOINTS);
HighGui.imshow("ORB 角点检测", src);
HighGui.waitKey(0);
}
public static void sift() {
Mat src = Imgcodecs.imread("D:\\OneDrive\\桌面\\5.jpeg");
Mat gray = new Mat();
Imgproc.cvtColor(src, gray, Imgproc.COLOR_BGR2GRAY);
SIFT sift = SIFT.create(8000);
MatOfKeyPoint point = new MatOfKeyPoint();
sift.detect(gray, point);
Features2d.drawKeypoints(src, point, src, new Scalar(0, 0, 255), Features2d.DrawMatchesFlags_DRAW_RICH_KEYPOINTS);
HighGui.imshow("SIFT 角点检测", src);
HighGui.waitKey(0);
}
public static void harris() {
Mat src = Imgcodecs.imread("D:\\OneDrive\\桌面\\5.jpeg");
Mat gray = new Mat();
Imgproc.cvtColor(src, gray, Imgproc.COLOR_BGR2GRAY);
Mat dst = new Mat();
Imgproc.cornerHarris(gray, dst, 3, 15, 0.04);
for (int i = 0, row = dst.rows(); i < row; i++) {
for (int j = 0, col = dst.cols(); j < col; j++) {
if (dst.get(i, j)[0] > 130) {
Imgproc.circle(src, new Point(i - 3, j + 2), 1, new Scalar(0, 0, 255), 1, Imgproc.LINE_AA);
}
}
}
HighGui.imshow("Harris 角点检测", src);
HighGui.waitKey(0);
}
public static void shi_tomasi() {
Mat src = Imgcodecs.imread("D:\\OneDrive\\桌面\\5.jpeg");
Mat gray = new Mat();
Imgproc.cvtColor(src, gray, Imgproc.COLOR_BGR2GRAY);
MatOfPoint corners = new MatOfPoint();
Imgproc.goodFeaturesToTrack(gray, corners, 200, 0.01, 10, new Mat(), 3, 5, false, 0.04);
int[] cornersData = new int[(int) (corners.total() * corners.channels())];
corners.get(0, 0, cornersData);
for (int i = 0; i < corners.rows(); i++) {
Imgproc.circle(src, new Point(cornersData[i * 2], cornersData[i * 2 + 1]), 3, new Scalar(0, 0, 255), Imgproc.FILLED);
}
HighGui.imshow("Shi-Tomasi 角点检测", src);
HighGui.waitKey(0);
}
}