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.6KB

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