threshold.cpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /**
  2. * OpenCV Threshold Example
  3. *
  4. * Copyright 2015 by Satya Mallick <spmallick@gmail.com>
  5. *
  6. **/
  7. #include "opencv2/opencv.hpp"
  8. using namespace cv;
  9. using namespace std;
  10. int main( int argc, char** argv )
  11. {
  12. // Read image
  13. Mat src = imread("threshold.png", IMREAD_GRAYSCALE);
  14. Mat dst;
  15. // Basic threhold example
  16. threshold(src,dst,0, 255, THRESH_BINARY);
  17. imwrite("opencv-threshold-example.jpg", dst);
  18. // Thresholding with maxval set to 128
  19. threshold(src, dst, 0, 128, THRESH_BINARY);
  20. imwrite("opencv-thresh-binary-maxval.jpg", dst);
  21. // Thresholding with threshold value set 127
  22. threshold(src,dst,127,255, THRESH_BINARY);
  23. imwrite("opencv-thresh-binary.jpg", dst);
  24. // Thresholding using THRESH_BINARY_INV
  25. threshold(src,dst,127,255, THRESH_BINARY_INV);
  26. imwrite("opencv-thresh-binary-inv.jpg", dst);
  27. // Thresholding using THRESH_TRUNC
  28. threshold(src,dst,127,255, THRESH_TRUNC);
  29. imwrite("opencv-thresh-trunc.jpg", dst);
  30. // Thresholding using THRESH_TOZERO
  31. threshold(src,dst,127,255, THRESH_TOZERO);
  32. imwrite("opencv-thresh-tozero.jpg", dst);
  33. // Thresholding using THRESH_TOZERO_INV
  34. threshold(src,dst,127,255, THRESH_TOZERO_INV);
  35. imwrite("opencv-thresh-to-zero-inv.jpg", dst);
  36. }