package com;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfPoint;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.highgui.HighGui;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
public class Main {
public static void main(String[] args) {
//Loading the OpenCV core library
System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
System.out.println("Версия OpenCV: " + Core.VERSION);
String file = "images\\white3.png";
Mat imageOriginal = Imgcodecs.imread(file);
Mat imageOriginalResize = new Mat();
Size minScaleSize = new Size(imageOriginal.width()/4, imageOriginal.height()/4);
Imgproc.resize(imageOriginal, imageOriginalResize, minScaleSize , 0, 0, Imgproc.INTER_AREA);
Mat imageGray = new Mat();
Imgproc.cvtColor(imageOriginalResize, imageGray, Imgproc.COLOR_BGR2GRAY);
Mat imageGaussianBlur = new Mat();
Imgproc.GaussianBlur(imageGray, imageGaussianBlur, new Size(3, 3), 3);
Mat imageCanny = new Mat();
Imgproc.Canny(imageGaussianBlur, imageCanny, 120, 250, 3, true);
MatOfPoint corners = new MatOfPoint();
System.out.println("Поиск углов");
Imgproc.goodFeaturesToTrack(imageCanny, corners, 4, 0.01, 500);
if (corners.toArray().length == 4) {
System.out.println("Найденно углов: " + corners.toArray().length);
Point[] cornerPoints = corners.toArray();
Point tl = cornerPoints[3];
Point br = cornerPoints[0];
Rect rect = new Rect(tl, new Size(br.x, br.y));
System.out.println("Будущий квадрат: " + rect);
//Mat mat = new Mat(cornerpoints[0], cornerpoints[1], cornerpoints[2], cornerpoints[3]);
// Rect rect = Imgproc.boundingRect();
for (Point points : cornerPoints) {
System.out.println(points);
int pointXI = (int) points.x;
String pointXS = Integer.toString(pointXI);
int pointYI = (int) points.y;
String pointYS = Integer.toString(pointYI);
Imgproc.circle(imageOriginalResize, points, 5, new Scalar(0,0,255), 3);
Imgproc.putText(imageOriginalResize, pointXS + " " + pointYS,
new Point(points.x-20, points.y+20) , 1, 1, new Scalar(0, 0, 255), 2);
// Imgproc.rectangle(imageOriginalResize, rect, new Scalar(255, 0, 0), 5);
}
}else {
System.out.println("Углы не найдены");
}
HighGui.namedWindow( "Image original", HighGui.WINDOW_AUTOSIZE );
HighGui.imshow( "Image original", imageOriginalResize);
HighGui.namedWindow( "Image Canny", HighGui.WINDOW_AUTOSIZE );
HighGui.imshow( "Image Canny", imageCanny);
if (HighGui.waitKey(0) == 27) {
HighGui.destroyAllWindows();
imageOriginal.release();
imageGray.release();
imageGaussianBlur.release();
imageCanny.release();
imageOriginalResize.release();
} else {
HighGui.destroyAllWindows();
imageOriginal.release();
imageGray.release();
imageGaussianBlur.release();
imageCanny.release();
imageOriginalResize.release();
}
System.exit(0);
}
}