Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package lab.pkg4;
- import org.opencv.core.Core;
- import static org.opencv.core.CvType.CV_8UC1;
- import org.opencv.core.Mat;
- import org.opencv.core.Point;
- import org.opencv.core.Scalar;
- import org.opencv.core.Size;
- import org.opencv.highgui.HighGui;
- import org.opencv.imgcodecs.Imgcodecs;
- import static org.opencv.imgcodecs.Imgcodecs.CV_LOAD_IMAGE_GRAYSCALE;
- import static org.opencv.imgcodecs.Imgcodecs.imwrite;
- import org.opencv.imgproc.Imgproc;
- public class Lab4 {
- public static void main(String[] args) {
- //Zadanie1();
- //Zadanie2();
- //Zadanie3();
- //Zadanie4();
- //Zadanie5();
- //Zadanie6();
- Zadanie7();
- System.exit(0);
- }
- public static void Zadanie1() {
- String plik = "C:/Users/Mateusz/Pictures/Camera Roll/1.jpg";
- Mat matrix = Imgcodecs.imread(plik, CV_LOAD_IMAGE_GRAYSCALE);
- Mat threshold_result = new Mat();
- Mat erosion_result = new Mat();
- Imgproc.threshold(matrix, threshold_result, 50, 180, Imgproc.THRESH_BINARY);
- int erosion_size = 5;
- Mat element = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(6 * erosion_size + 1, 3 * erosion_size + 1));
- Imgproc.erode(threshold_result, erosion_result, element);
- imwrite("erozja_prostokat.jpg", erosion_result);
- element = Imgproc.getStructuringElement(Imgproc.MORPH_CROSS, new Size(5 * erosion_size + 1, 3 * erosion_size + 1));
- Imgproc.erode(threshold_result, erosion_result, element);
- imwrite("erozja_krzyz.jpg", erosion_result);
- element = Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, new Size(3 * erosion_size + 1, 2 * erosion_size + 1));
- Imgproc.erode(threshold_result, erosion_result, element);
- imwrite("erozja_elipsa.jpg", erosion_result);
- }
- public static void Zadanie2() {
- String plik = "C:/Users/Mateusz/Pictures/Camera Roll/1.jpg";
- Mat matrix = Imgcodecs.imread(plik, CV_LOAD_IMAGE_GRAYSCALE);
- Mat threshold_result = new Mat();
- Mat erosion_result = new Mat();
- Imgproc.threshold(matrix, threshold_result, 50, 180, Imgproc.THRESH_BINARY);
- int erosion_size = 5;
- Mat element = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(6 * erosion_size + 1, 3 * erosion_size + 1));
- Imgproc.erode(threshold_result, erosion_result, element, new Point(0, 0), 3);
- showImg(erosion_result, "Okno");
- }
- public static void showImg(Mat img, String name) {
- HighGui.namedWindow(name, HighGui.WINDOW_AUTOSIZE);
- HighGui.imshow(name, img);
- HighGui.waitKey(0);
- }
- public static void Zadanie3() {
- String plik = "C:/Users/Mateusz/Pictures/Camera Roll/1.jpg";
- Mat matrix = Imgcodecs.imread(plik, CV_LOAD_IMAGE_GRAYSCALE);
- Mat threshold_result = new Mat();
- Mat dilation_result = new Mat();
- Imgproc.threshold(matrix, threshold_result, 50, 180, Imgproc.THRESH_BINARY);
- Mat element = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(2 * 2 + 1, 2 * 1 + 1));
- Imgproc.dilate(threshold_result, dilation_result, element);
- imwrite("dylatacja_prostokat.jpg", dilation_result);
- element = Imgproc.getStructuringElement(Imgproc.MORPH_CROSS, new Size(2 * 50 + 1, 2 * 20 + 1));
- Imgproc.erode(threshold_result, dilation_result, element);
- imwrite("dylatacja_krzyz.jpg", dilation_result);
- element = Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, new Size(2 * 15 + 1, 2 * 5 + 1));
- Imgproc.erode(threshold_result, dilation_result, element);
- imwrite("dylatacja_elipsa.jpg", dilation_result);
- }
- public static void Zadanie4() {
- String plik = "C:/Users/Mateusz/Pictures/Camera Roll/1.jpg";
- Mat matrix = Imgcodecs.imread(plik, CV_LOAD_IMAGE_GRAYSCALE);
- Mat threshold_result = new Mat();
- Mat dilation_result = new Mat();
- Imgproc.threshold(matrix, threshold_result, 50, 180, Imgproc.THRESH_BINARY);
- int dilation_size = 5;
- Mat element = Imgproc.getStructuringElement(Imgproc.MORPH_CROSS, new Size(2 * dilation_size + 1, 2 * dilation_size + 1));
- Imgproc.dilate(threshold_result, dilation_result, element, new Point(0, 0), 3);
- showImg(dilation_result, "Okno");
- }
- public static void Zadanie5() {
- String plik = "C:/Users/Mateusz/Pictures/Camera Roll/1.jpg";
- Mat matrix = Imgcodecs.imread(plik, CV_LOAD_IMAGE_GRAYSCALE);
- Mat threshold_result = new Mat();
- Mat erosion_result = new Mat();
- Mat dilation_result = new Mat();
- Imgproc.threshold(matrix, threshold_result, 50, 180, Imgproc.THRESH_BINARY);
- int operation_size = 5;
- Mat element = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(2 * operation_size + 1, 2 * operation_size + 1));
- Imgproc.erode(threshold_result, erosion_result, element);
- Imgproc.dilate(erosion_result, dilation_result, element);
- imwrite("otwarcie.jpg", dilation_result);
- Imgproc.dilate(threshold_result, dilation_result, element);
- Imgproc.erode(dilation_result, erosion_result, element);
- imwrite("domkniecie.jpg", erosion_result);
- }
- public static void Zadanie6() {
- String plik = "C:/Users/Mateusz/Pictures/Camera Roll/1.jpg";
- Mat matrix = Imgcodecs.imread(plik, CV_LOAD_IMAGE_GRAYSCALE);
- Mat threshold_result = new Mat();
- Mat erosion_result = new Mat();
- Mat dilation_result = new Mat();
- Mat contours1 = new Mat();
- Mat contours2 = new Mat();
- Mat contours3 = new Mat();
- Imgproc.threshold(matrix, threshold_result, 50, 180, Imgproc.THRESH_BINARY);
- int operation_size = 5;
- Mat element = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(2 * operation_size + 1, 2 * operation_size + 1));
- Imgproc.dilate(threshold_result, dilation_result, element);
- Core.subtract(dilation_result, threshold_result, contours1);
- imwrite("kontury1.jpg", contours1);
- Imgproc.erode(threshold_result, erosion_result, element);
- Core.subtract(threshold_result, erosion_result, contours2);
- imwrite("kontury2.jpg", contours2);
- Imgproc.dilate(threshold_result, dilation_result, element);
- Imgproc.erode(threshold_result, erosion_result, element);
- Core.subtract(dilation_result, erosion_result, contours3);
- imwrite("kontury3.jpg", contours3);
- }
- public static void Zadanie7() {
- String plik = "D:/UTP/Obrazy_obrazy/litera.png";
- Mat matrix = Imgcodecs.imread(plik, CV_LOAD_IMAGE_GRAYSCALE);
- Imgproc.threshold(matrix, matrix, 127, 255, Imgproc.THRESH_BINARY);
- Mat skel = new Mat(matrix.size(), CV_8UC1, new Scalar(0));
- Mat temp = new Mat();
- Mat eroded = new Mat();
- Mat element = Imgproc.getStructuringElement(Imgproc.MORPH_CROSS, new Size(3, 3));
- Boolean done;
- do {
- Imgproc.erode(matrix, eroded, element);
- Imgproc.dilate(eroded, temp, element);
- Core.subtract(matrix, temp, temp);
- Core.bitwise_or(skel, temp, skel);
- eroded.copyTo(matrix);
- done = (Core.countNonZero(matrix) == 0);
- } while (!done);
- showImg(skel, "Okno");
- }
- static {
- System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement