HitMiss.java 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import org.opencv.core.*;
  2. import org.opencv.highgui.HighGui;
  3. import org.opencv.imgproc.Imgproc;
  4. class HitMissRun{
  5. public void run() {
  6. Mat input_image = new Mat( 8, 8, CvType.CV_8UC1 );
  7. int row = 0, col = 0;
  8. input_image.put(row ,col,
  9. 0, 0, 0, 0, 0, 0, 0, 0,
  10. 0, 255, 255, 255, 0, 0, 0, 255,
  11. 0, 255, 255, 255, 0, 0, 0, 0,
  12. 0, 255, 255, 255, 0, 255, 0, 0,
  13. 0, 0, 255, 0, 0, 0, 0, 0,
  14. 0, 0, 255, 0, 0, 255, 255, 0,
  15. 0, 255, 0, 255, 0, 0, 255, 0,
  16. 0, 255, 255, 255, 0, 0, 0, 0);
  17. Mat kernel = new Mat( 3, 3, CvType.CV_16S );
  18. kernel.put(row ,col,
  19. 0, 1, 0,
  20. 1, -1, 1,
  21. 0, 1, 0 );
  22. Mat output_image = new Mat();
  23. Imgproc.morphologyEx(input_image, output_image, Imgproc.MORPH_HITMISS, kernel);
  24. int rate = 50;
  25. Core.add(kernel, new Scalar(1), kernel);
  26. Core.multiply(kernel, new Scalar(127), kernel);
  27. kernel.convertTo(kernel, CvType.CV_8U);
  28. Imgproc.resize(kernel, kernel, new Size(), rate, rate, Imgproc.INTER_NEAREST);
  29. HighGui.imshow("kernel", kernel);
  30. HighGui.moveWindow("kernel", 0, 0);
  31. Imgproc.resize(input_image, input_image, new Size(), rate, rate, Imgproc.INTER_NEAREST);
  32. HighGui.imshow("Original", input_image);
  33. HighGui.moveWindow("Original", 0, 200);
  34. Imgproc.resize(output_image, output_image, new Size(), rate, rate, Imgproc.INTER_NEAREST);
  35. HighGui.imshow("Hit or Miss", output_image);
  36. HighGui.moveWindow("Hit or Miss", 500, 200);
  37. HighGui.waitKey(0);
  38. System.exit(0);
  39. }
  40. }
  41. public class HitMiss
  42. {
  43. public static void main(String[] args) {
  44. // load the native OpenCV library
  45. System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
  46. new HitMissRun().run();
  47. }
  48. }