Videoverarbeitung eines Beerpongspiels. Entstehung im Rahmen einer Praktikumsaufgabe im Fach "Interaktion".
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ofApp.cpp 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #include "ofApp.h"
  2. #define VIDEO_WIDTH 360
  3. #define VIDEO_HEIGHT 640
  4. //--------------------------------------------------------------
  5. void ofApp::setup(){
  6. vidPlayer.load("video.mkv"); // in /bin/data/
  7. vidPlayer.play();
  8. vidPlayer.setLoopState(OF_LOOP_NORMAL);
  9. colorVideo.allocate(VIDEO_WIDTH, VIDEO_HEIGHT);
  10. grayVideo.allocate(VIDEO_WIDTH, VIDEO_HEIGHT);
  11. learnBackground = true;
  12. gui.setup("Parameter");
  13. gui.add(threshhold.set("Threshhold", 80, 0, 255));
  14. }
  15. //--------------------------------------------------------------
  16. void ofApp::update(){
  17. vidPlayer.update();
  18. colorVideo.setFromPixels(vidPlayer.getPixels());
  19. grayVideo = colorVideo; // Convert Color to Grayscale
  20. //cvInRange();
  21. //colorVideo.convertToGrayscalePlanarImage(red, 0);
  22. if (learnBackground){
  23. recognizer.setBackground(grayVideo);
  24. learnBackground = false;
  25. }
  26. recognizer.setThreshhold(threshhold);
  27. recognizer.update(grayVideo, VIDEO_WIDTH, VIDEO_HEIGHT);
  28. background = recognizer.background;
  29. subtracted = recognizer.subtracted;
  30. binarized = recognizer.binarized;
  31. contourFinder = recognizer.contourFinder;
  32. }
  33. //--------------------------------------------------------------
  34. void ofApp::draw(){
  35. ofSetHexColor(0xffffff);
  36. colorVideo.draw(20, 20);
  37. grayVideo.draw(40 + VIDEO_WIDTH, 20);
  38. subtracted.draw(60 + 2 * VIDEO_WIDTH, 20);
  39. //binarized.draw(80 + 3 * VIDEO_WIDTH, 20);
  40. //colorVideo.draw(20, 20);
  41. //grayVideo.draw(40 + VIDEO_WIDTH, 20);
  42. //background.draw(20, 40 + VIDEO_HEIGHT);
  43. //binarized.draw(40 + VIDEO_WIDTH, 40 + VIDEO_HEIGHT);
  44. //red.draw(40 + VIDEO_WIDTH, 20);
  45. //green.draw(20, 40 + VIDEO_HEIGHT);
  46. //blue.draw(40 + VIDEO_WIDTH, 40 + VIDEO_HEIGHT);
  47. ofFill();
  48. ofSetHexColor(0x333333);
  49. ofDrawRectangle(80 + 3 * VIDEO_WIDTH, 20, VIDEO_WIDTH, VIDEO_HEIGHT);
  50. ofSetHexColor(0xffffff);
  51. for (int i = 0; i < contourFinder.blobs.size(); i++) {
  52. ofxCvBlob blob = contourFinder.blobs[i];
  53. // draw over the centroid if the blob is a hole
  54. ofSetColor(255);
  55. // https://docs.opencv.org/master/d1/d32/tutorial_py_contour_properties.html
  56. float aspect_ratio = blob.boundingRect.getWidth() / blob.boundingRect.getHeight();
  57. stringstream sizeStr;
  58. sizeStr << std::to_string(i) << ": " << std::to_string(contourFinder.blobs[i].area);
  59. sizeStr << " | F=" << aspect_ratio;
  60. if (aspect_ratio > 0.5 && aspect_ratio < 1.5)
  61. {
  62. blob.draw(80 + 3 * VIDEO_WIDTH, 20);
  63. ofDrawBitmapString(sizeStr.str(),
  64. contourFinder.blobs[i].boundingRect.getCenter().x + 80 + 3 * VIDEO_WIDTH,
  65. contourFinder.blobs[i].boundingRect.getCenter().y + 20);
  66. }
  67. }
  68. gui.draw();
  69. }
  70. //--------------------------------------------------------------
  71. void ofApp::keyPressed(int key){
  72. if (key == '-') threshhold--;
  73. else if (key == '+') threshhold++;
  74. else if (key == ' ') learnBackground = true;
  75. else if (key == '0') vidPlayer.setPaused(!vidPlayer.isPaused());
  76. else if (key == OF_KEY_RIGHT) vidPlayer.nextFrame();
  77. else if (key == OF_KEY_LEFT) vidPlayer.previousFrame();
  78. else;
  79. }
  80. //--------------------------------------------------------------
  81. void ofApp::keyReleased(int key){
  82. }
  83. //--------------------------------------------------------------
  84. void ofApp::mouseMoved(int x, int y ){
  85. }
  86. //--------------------------------------------------------------
  87. void ofApp::mouseDragged(int x, int y, int button){
  88. }
  89. //--------------------------------------------------------------
  90. void ofApp::mousePressed(int x, int y, int button){
  91. }
  92. //--------------------------------------------------------------
  93. void ofApp::mouseReleased(int x, int y, int button){
  94. }
  95. //--------------------------------------------------------------
  96. void ofApp::mouseEntered(int x, int y){
  97. }
  98. //--------------------------------------------------------------
  99. void ofApp::mouseExited(int x, int y){
  100. }
  101. //--------------------------------------------------------------
  102. void ofApp::windowResized(int w, int h){
  103. }
  104. //--------------------------------------------------------------
  105. void ofApp::gotMessage(ofMessage msg){
  106. }
  107. //--------------------------------------------------------------
  108. void ofApp::dragEvent(ofDragInfo dragInfo){
  109. }