Advertisement
Guest User

Untitled

a guest
Dec 12th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.39 KB | None | 0 0
  1. package lab.pkg4;
  2.  
  3. import org.opencv.core.Core;
  4. import static org.opencv.core.CvType.CV_8UC1;
  5. import org.opencv.core.Mat;
  6. import org.opencv.core.Point;
  7. import org.opencv.core.Scalar;
  8. import org.opencv.core.Size;
  9. import org.opencv.highgui.HighGui;
  10. import org.opencv.imgcodecs.Imgcodecs;
  11. import static org.opencv.imgcodecs.Imgcodecs.CV_LOAD_IMAGE_GRAYSCALE;
  12. import static org.opencv.imgcodecs.Imgcodecs.imwrite;
  13. import org.opencv.imgproc.Imgproc;
  14.  
  15. public class Lab4 {
  16.  
  17.     public static void main(String[] args) {
  18.         //Zadanie1();
  19.         //Zadanie2();
  20.         //Zadanie3();
  21.         //Zadanie4();
  22.         //Zadanie5();
  23.         //Zadanie6();
  24.         Zadanie7();
  25.         System.exit(0);
  26.     }
  27.  
  28.     public static void Zadanie1() {
  29.         String plik = "C:/Users/Mateusz/Pictures/Camera Roll/1.jpg";
  30.         Mat matrix = Imgcodecs.imread(plik, CV_LOAD_IMAGE_GRAYSCALE);
  31.         Mat threshold_result = new Mat();
  32.         Mat erosion_result = new Mat();
  33.         Imgproc.threshold(matrix, threshold_result, 50, 180, Imgproc.THRESH_BINARY);
  34.         int erosion_size = 5;
  35.         Mat element = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(6 * erosion_size + 1, 3 * erosion_size + 1));
  36.         Imgproc.erode(threshold_result, erosion_result, element);
  37.         imwrite("erozja_prostokat.jpg", erosion_result);
  38.         element = Imgproc.getStructuringElement(Imgproc.MORPH_CROSS, new Size(5 * erosion_size + 1, 3 * erosion_size + 1));
  39.         Imgproc.erode(threshold_result, erosion_result, element);
  40.         imwrite("erozja_krzyz.jpg", erosion_result);
  41.         element = Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, new Size(3 * erosion_size + 1, 2 * erosion_size + 1));
  42.         Imgproc.erode(threshold_result, erosion_result, element);
  43.         imwrite("erozja_elipsa.jpg", erosion_result);
  44.     }
  45.  
  46.     public static void Zadanie2() {
  47.         String plik = "C:/Users/Mateusz/Pictures/Camera Roll/1.jpg";
  48.         Mat matrix = Imgcodecs.imread(plik, CV_LOAD_IMAGE_GRAYSCALE);
  49.         Mat threshold_result = new Mat();
  50.         Mat erosion_result = new Mat();
  51.         Imgproc.threshold(matrix, threshold_result, 50, 180, Imgproc.THRESH_BINARY);
  52.         int erosion_size = 5;
  53.         Mat element = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(6 * erosion_size + 1, 3 * erosion_size + 1));
  54.         Imgproc.erode(threshold_result, erosion_result, element, new Point(0, 0), 3);
  55.         showImg(erosion_result, "Okno");
  56.     }
  57.  
  58.     public static void showImg(Mat img, String name) {
  59.         HighGui.namedWindow(name, HighGui.WINDOW_AUTOSIZE);
  60.         HighGui.imshow(name, img);
  61.         HighGui.waitKey(0);
  62.     }
  63.  
  64.     public static void Zadanie3() {
  65.         String plik = "C:/Users/Mateusz/Pictures/Camera Roll/1.jpg";
  66.         Mat matrix = Imgcodecs.imread(plik, CV_LOAD_IMAGE_GRAYSCALE);
  67.         Mat threshold_result = new Mat();
  68.         Mat dilation_result = new Mat();
  69.         Imgproc.threshold(matrix, threshold_result, 50, 180, Imgproc.THRESH_BINARY);
  70.         Mat element = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(2 * 2 + 1, 2 * 1 + 1));
  71.         Imgproc.dilate(threshold_result, dilation_result, element);
  72.         imwrite("dylatacja_prostokat.jpg", dilation_result);
  73.         element = Imgproc.getStructuringElement(Imgproc.MORPH_CROSS, new Size(2 * 50 + 1, 2 * 20 + 1));
  74.         Imgproc.erode(threshold_result, dilation_result, element);
  75.         imwrite("dylatacja_krzyz.jpg", dilation_result);
  76.         element = Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, new Size(2 * 15 + 1, 2 * 5 + 1));
  77.         Imgproc.erode(threshold_result, dilation_result, element);
  78.         imwrite("dylatacja_elipsa.jpg", dilation_result);
  79.     }
  80.  
  81.     public static void Zadanie4() {
  82.         String plik = "C:/Users/Mateusz/Pictures/Camera Roll/1.jpg";
  83.         Mat matrix = Imgcodecs.imread(plik, CV_LOAD_IMAGE_GRAYSCALE);
  84.         Mat threshold_result = new Mat();
  85.         Mat dilation_result = new Mat();
  86.         Imgproc.threshold(matrix, threshold_result, 50, 180, Imgproc.THRESH_BINARY);
  87.         int dilation_size = 5;
  88.         Mat element = Imgproc.getStructuringElement(Imgproc.MORPH_CROSS, new Size(2 * dilation_size + 1, 2 * dilation_size + 1));
  89.         Imgproc.dilate(threshold_result, dilation_result, element, new Point(0, 0), 3);
  90.         showImg(dilation_result, "Okno");
  91.     }
  92.  
  93.     public static void Zadanie5() {
  94.         String plik = "C:/Users/Mateusz/Pictures/Camera Roll/1.jpg";
  95.         Mat matrix = Imgcodecs.imread(plik, CV_LOAD_IMAGE_GRAYSCALE);
  96.         Mat threshold_result = new Mat();
  97.         Mat erosion_result = new Mat();
  98.         Mat dilation_result = new Mat();
  99.         Imgproc.threshold(matrix, threshold_result, 50, 180, Imgproc.THRESH_BINARY);
  100.         int operation_size = 5;
  101.         Mat element = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(2 * operation_size + 1, 2 * operation_size + 1));
  102.         Imgproc.erode(threshold_result, erosion_result, element);
  103.         Imgproc.dilate(erosion_result, dilation_result, element);
  104.         imwrite("otwarcie.jpg", dilation_result);
  105.         Imgproc.dilate(threshold_result, dilation_result, element);
  106.         Imgproc.erode(dilation_result, erosion_result, element);
  107.         imwrite("domkniecie.jpg", erosion_result);
  108.     }
  109.  
  110.     public static void Zadanie6() {
  111.         String plik = "C:/Users/Mateusz/Pictures/Camera Roll/1.jpg";
  112.         Mat matrix = Imgcodecs.imread(plik, CV_LOAD_IMAGE_GRAYSCALE);
  113.         Mat threshold_result = new Mat();
  114.         Mat erosion_result = new Mat();
  115.         Mat dilation_result = new Mat();
  116.         Mat contours1 = new Mat();
  117.         Mat contours2 = new Mat();
  118.         Mat contours3 = new Mat();
  119.         Imgproc.threshold(matrix, threshold_result, 50, 180, Imgproc.THRESH_BINARY);
  120.         int operation_size = 5;
  121.         Mat element = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(2 * operation_size + 1, 2 * operation_size + 1));
  122.         Imgproc.dilate(threshold_result, dilation_result, element);
  123.         Core.subtract(dilation_result, threshold_result, contours1);
  124.         imwrite("kontury1.jpg", contours1);
  125.         Imgproc.erode(threshold_result, erosion_result, element);
  126.         Core.subtract(threshold_result, erosion_result, contours2);
  127.         imwrite("kontury2.jpg", contours2);
  128.         Imgproc.dilate(threshold_result, dilation_result, element);
  129.         Imgproc.erode(threshold_result, erosion_result, element);
  130.         Core.subtract(dilation_result, erosion_result, contours3);
  131.         imwrite("kontury3.jpg", contours3);
  132.     }
  133.  
  134.     public static void Zadanie7() {
  135.         String plik = "D:/UTP/Obrazy_obrazy/litera.png";
  136.         Mat matrix = Imgcodecs.imread(plik, CV_LOAD_IMAGE_GRAYSCALE);
  137.         Imgproc.threshold(matrix, matrix, 127, 255, Imgproc.THRESH_BINARY);
  138.         Mat skel = new Mat(matrix.size(), CV_8UC1, new Scalar(0));
  139.         Mat temp = new Mat();
  140.         Mat eroded = new Mat();
  141.         Mat element = Imgproc.getStructuringElement(Imgproc.MORPH_CROSS, new Size(3, 3));
  142.         Boolean done;
  143.         do {
  144.             Imgproc.erode(matrix, eroded, element);
  145.             Imgproc.dilate(eroded, temp, element);
  146.             Core.subtract(matrix, temp, temp);
  147.             Core.bitwise_or(skel, temp, skel);
  148.             eroded.copyTo(matrix);
  149.             done = (Core.countNonZero(matrix) == 0);
  150.         } while (!done);
  151.         showImg(skel, "Okno");
  152.     }
  153.  
  154.     static {
  155.         System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
  156.     }
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement