java - 使用 OpenCV 4.1.1 和 Java 进行自动透视校正
问题描述
我已经成功地从图像中检测到最大的矩形,但是由于我的应用程序将用于扫描目的,因此我也希望具有透视更改功能。
解决方案
公共类主要{
public static void main(String[] args) {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
String default_file = "D:/folder name/image.jpg";
String filename = ((args.length > 0) ? args[0] : default_file);
// Mat imgSource = Highgui.imread(fileName, Highgui.CV_LOAD_IMAGE_UNCHANGED);
Mat imgSource = Imgcodecs.imread(filename, Imgcodecs.IMREAD_GRAYSCALE);
// Check if image is loaded fine
if( imgSource.empty() ) {
System.out.println("Error opening image!");
System.out.println("Program Arguments: [image_name -- default "
+ default_file +"] \n");
System.exit(-1);
}
Imgproc.Canny(imgSource.clone(), imgSource, 50, 50);
// apply gaussian blur to smoothen lines of dots
Imgproc.GaussianBlur(imgSource, imgSource, new org.opencv.core.Size(5, 5), 5);
// find the contours
List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
Imgproc.findContours(imgSource, contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);
double maxArea = -1;
MatOfPoint temp_contour = contours.get(0); // the largest is at the
// index 0 for starting
// point
MatOfPoint2f approxCurve = new MatOfPoint2f();
for (int idx = 0; idx < contours.size(); idx++) {
temp_contour = contours.get(idx);
double contourarea = Imgproc.contourArea(temp_contour);
// compare this contour to the previous largest contour found
if (contourarea > maxArea) {
// check if this contour is a square
MatOfPoint2f new_mat = new MatOfPoint2f(temp_contour.toArray());
int contourSize = (int) temp_contour.total();
MatOfPoint2f approxCurve_temp = new MatOfPoint2f();
Imgproc.approxPolyDP(new_mat, approxCurve_temp, contourSize * 0.05, true);
if (approxCurve_temp.total() == 4) {
maxArea = contourarea;
approxCurve = approxCurve_temp;
}
}
}
Imgproc.cvtColor(imgSource, imgSource, Imgproc.COLOR_BayerBG2RGB);
Mat sourceImage = Imgcodecs.imread(filename, Imgcodecs.IMREAD_GRAYSCALE);
double[] temp_double;
temp_double = approxCurve.get(0, 0);
Point p1 = new Point(temp_double[0], temp_double[1]);
temp_double = approxCurve.get(1, 0);
Point p2 = new Point(temp_double[0], temp_double[1]);
temp_double = approxCurve.get(2, 0);
Point p3 = new Point(temp_double[0], temp_double[1]);
temp_double = approxCurve.get(3, 0);
Point p4 = new Point(temp_double[0], temp_double[1]);
List<Point> source = new ArrayList<Point>();
source.add(p1);
source.add(p2);
source.add(p3);
source.add(p4);
Mat startM = Converters.vector_Point2f_to_Mat(source);
Mat result = warp(sourceImage, startM);
HighGui.imshow("Source", result);
// Wait and Exit
HighGui.waitKey();
System.exit(0);
// System.out.println(p1);
}
public static Mat warp(Mat inputMat, Mat startM) {
int resultWidth = 1550;
int resultHeight = 700;
Point ocvPOut4 = new Point(0, 0);
Point ocvPOut1 = new Point(0, resultHeight);
Point ocvPOut2 = new Point(resultWidth, resultHeight);
Point ocvPOut3 = new Point(resultWidth, 0);
if (inputMat.height() > inputMat.width()) {
// int temp = resultWidth;
// resultWidth = resultHeight;
// resultHeight = temp;
ocvPOut3 = new Point(0, 0);
ocvPOut4 = new Point(0, resultHeight);
ocvPOut1 = new Point(resultWidth, resultHeight);
ocvPOut2 = new Point(resultWidth, 0);
}
Mat outputMat = new Mat(resultWidth, resultHeight, CvType.CV_8UC4);
List<Point> dest = new ArrayList<Point>();
dest.add(ocvPOut1);
dest.add(ocvPOut2);
dest.add(ocvPOut3);
dest.add(ocvPOut4);
Mat endM = Converters.vector_Point2f_to_Mat(dest);
Mat perspectiveTransform = Imgproc.getPerspectiveTransform(startM, endM);
// System.out.println(dest);
Imgproc.warpPerspective(inputMat, outputMat, perspectiveTransform, new Size(resultWidth, resultHeight), Imgproc.INTER_CUBIC);
return outputMat;
}
}
推荐阅读
- javascript - 如何为 jQuery .addClass 和 .removeClass 添加延迟?
- c# - 聊天气泡功能的 Xamarin 递增循环
- javascript - 用户点击推送通知后如何更改视图 - Firebase - 本机脚本
- c++ - 链接器错误:在 VS2019 中使用 CMake 项目的函数“int __cdecl invoke_main(void)”中引用了未解析的外部符号 _main
- reactjs - 如何在 React Native 中存储和更新数据
- python-3.x - Python - 检查应用程序是否是 Web 浏览器
- r - R:从多列创建新的基于列的值列表
- kubernetes - Kubernetes - Nodeport 与负载均衡器
- ios - 从另一个应用程序使用 iOS Microsoft Word 本机应用程序打开 .docx 文件(托管在 OneDrive 上)
- swift - 如何删除领域父对象及其所有子对象?