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 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #include "ofApp.h"
  2. #define VIDEO_WIDTH 568
  3. #define VIDEO_HEIGHT 320
  4. //--------------------------------------------------------------
  5. void ofApp::setup(){
  6. vidPlayer.load("beer.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. if (learnBackground){
  21. background = grayVideo;
  22. learnBackground = false;
  23. }
  24. // Background subtraction
  25. binarized.absDiff(background, grayVideo);
  26. // Binarization
  27. binarized.threshold(threshhold);
  28. contourFinder.findContours(
  29. binarized,
  30. 10,
  31. VIDEO_HEIGHT * VIDEO_WIDTH / 10,
  32. 10,
  33. true);
  34. }
  35. //--------------------------------------------------------------
  36. void ofApp::draw(){
  37. ofSetHexColor(0xffffff);
  38. colorVideo.draw(20, 20);
  39. grayVideo.draw(40 + VIDEO_WIDTH, 20);
  40. background.draw(20, 40 + VIDEO_HEIGHT);
  41. binarized.draw(40 + VIDEO_WIDTH, 40 + VIDEO_HEIGHT);
  42. ofFill();
  43. ofSetHexColor(0x333333);
  44. ofDrawRectangle(40 + VIDEO_WIDTH, 60 + VIDEO_HEIGHT + VIDEO_HEIGHT, VIDEO_WIDTH, VIDEO_HEIGHT);
  45. ofSetHexColor(0xffffff);
  46. for (int i = 0; i < contourFinder.blobs.size(); i++) {
  47. ofxCvBlob blob = contourFinder.blobs[i];
  48. // draw over the centroid if the blob is a hole
  49. ofSetColor(255);
  50. // https://docs.opencv.org/master/d1/d32/tutorial_py_contour_properties.html
  51. float aspect_ratio = blob.boundingRect.getWidth() / blob.boundingRect.getHeight();
  52. stringstream sizeStr;
  53. sizeStr << std::to_string(i) << ": " << std::to_string(contourFinder.blobs[i].area);
  54. sizeStr << " | F=" << aspect_ratio;
  55. if (aspect_ratio > 0.5 && aspect_ratio < 1.5)
  56. {
  57. blob.draw(40 + VIDEO_WIDTH, 60 + VIDEO_HEIGHT + VIDEO_HEIGHT);
  58. ofDrawBitmapString(sizeStr.str(),
  59. contourFinder.blobs[i].boundingRect.getCenter().x + 40 + VIDEO_WIDTH,
  60. contourFinder.blobs[i].boundingRect.getCenter().y + 60 + VIDEO_HEIGHT + VIDEO_HEIGHT);
  61. }
  62. }
  63. gui.draw();
  64. }
  65. //--------------------------------------------------------------
  66. void ofApp::keyPressed(int key){
  67. if (key == '-') threshhold--;
  68. else if (key == '+') threshhold++;
  69. else if (key == ' ') learnBackground = true;
  70. else if (key == '0') vidPlayer.setPaused(!vidPlayer.isPaused());
  71. else if (key == OF_KEY_RIGHT) vidPlayer.nextFrame();
  72. else if (key == OF_KEY_LEFT) vidPlayer.previousFrame();
  73. else;
  74. }
  75. //--------------------------------------------------------------
  76. void ofApp::keyReleased(int key){
  77. }
  78. //--------------------------------------------------------------
  79. void ofApp::mouseMoved(int x, int y ){
  80. }
  81. //--------------------------------------------------------------
  82. void ofApp::mouseDragged(int x, int y, int button){
  83. }
  84. //--------------------------------------------------------------
  85. void ofApp::mousePressed(int x, int y, int button){
  86. }
  87. //--------------------------------------------------------------
  88. void ofApp::mouseReleased(int x, int y, int button){
  89. }
  90. //--------------------------------------------------------------
  91. void ofApp::mouseEntered(int x, int y){
  92. }
  93. //--------------------------------------------------------------
  94. void ofApp::mouseExited(int x, int y){
  95. }
  96. //--------------------------------------------------------------
  97. void ofApp::windowResized(int w, int h){
  98. }
  99. //--------------------------------------------------------------
  100. void ofApp::gotMessage(ofMessage msg){
  101. }
  102. //--------------------------------------------------------------
  103. void ofApp::dragEvent(ofDragInfo dragInfo){
  104. }