alpha_comp.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #include <iostream>
  2. #include "opencv2/core/opengl.hpp"
  3. #include "opencv2/highgui.hpp"
  4. #include "opencv2/cudaimgproc.hpp"
  5. using namespace std;
  6. using namespace cv;
  7. using namespace cv::cuda;
  8. int main()
  9. {
  10. cout << "This program demonstrates using alphaComp" << endl;
  11. cout << "Press SPACE to change compositing operation" << endl;
  12. cout << "Press ESC to exit" << endl;
  13. namedWindow("First Image", WINDOW_NORMAL);
  14. namedWindow("Second Image", WINDOW_NORMAL);
  15. namedWindow("Result", WINDOW_OPENGL);
  16. setGlDevice();
  17. Mat src1(640, 480, CV_8UC4, Scalar::all(0));
  18. Mat src2(640, 480, CV_8UC4, Scalar::all(0));
  19. rectangle(src1, Rect(50, 50, 200, 200), Scalar(0, 0, 255, 128), 30);
  20. rectangle(src2, Rect(100, 100, 200, 200), Scalar(255, 0, 0, 128), 30);
  21. GpuMat d_src1(src1);
  22. GpuMat d_src2(src2);
  23. GpuMat d_res;
  24. imshow("First Image", src1);
  25. imshow("Second Image", src2);
  26. int alpha_op = ALPHA_OVER;
  27. const char* op_names[] =
  28. {
  29. "ALPHA_OVER", "ALPHA_IN", "ALPHA_OUT", "ALPHA_ATOP", "ALPHA_XOR", "ALPHA_PLUS", "ALPHA_OVER_PREMUL", "ALPHA_IN_PREMUL", "ALPHA_OUT_PREMUL",
  30. "ALPHA_ATOP_PREMUL", "ALPHA_XOR_PREMUL", "ALPHA_PLUS_PREMUL", "ALPHA_PREMUL"
  31. };
  32. for(;;)
  33. {
  34. cout << op_names[alpha_op] << endl;
  35. alphaComp(d_src1, d_src2, d_res, alpha_op);
  36. imshow("Result", d_res);
  37. char key = static_cast<char>(waitKey());
  38. if (key == 27)
  39. break;
  40. if (key == 32)
  41. {
  42. ++alpha_op;
  43. if (alpha_op > ALPHA_PREMUL)
  44. alpha_op = ALPHA_OVER;
  45. }
  46. }
  47. return 0;
  48. }