HitMiss.cpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #include <opencv2/core.hpp>
  2. #include <opencv2/imgproc.hpp>
  3. #include <opencv2/highgui.hpp>
  4. using namespace cv;
  5. int main(){
  6. Mat input_image = (Mat_<uchar>(8, 8) <<
  7. 0, 0, 0, 0, 0, 0, 0, 0,
  8. 0, 255, 255, 255, 0, 0, 0, 255,
  9. 0, 255, 255, 255, 0, 0, 0, 0,
  10. 0, 255, 255, 255, 0, 255, 0, 0,
  11. 0, 0, 255, 0, 0, 0, 0, 0,
  12. 0, 0, 255, 0, 0, 255, 255, 0,
  13. 0, 255, 0, 255, 0, 0, 255, 0,
  14. 0, 255, 255, 255, 0, 0, 0, 0);
  15. Mat kernel = (Mat_<int>(3, 3) <<
  16. 0, 1, 0,
  17. 1, -1, 1,
  18. 0, 1, 0);
  19. Mat output_image;
  20. morphologyEx(input_image, output_image, MORPH_HITMISS, kernel);
  21. const int rate = 50;
  22. kernel = (kernel + 1) * 127;
  23. kernel.convertTo(kernel, CV_8U);
  24. resize(kernel, kernel, Size(), rate, rate, INTER_NEAREST);
  25. imshow("kernel", kernel);
  26. moveWindow("kernel", 0, 0);
  27. resize(input_image, input_image, Size(), rate, rate, INTER_NEAREST);
  28. imshow("Original", input_image);
  29. moveWindow("Original", 0, 200);
  30. resize(output_image, output_image, Size(), rate, rate, INTER_NEAREST);
  31. imshow("Hit or Miss", output_image);
  32. moveWindow("Hit or Miss", 500, 200);
  33. waitKey(0);
  34. return 0;
  35. }