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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #include "ofApp.h"
  2. //--------------------------------------------------------------
  3. void ofApp::setup(){
  4. webcam.setup(1280, 720);
  5. contour.setMinAreaRadius(1);
  6. contour.setMaxAreaRadius(5);
  7. color_red_laser.set(185, 160, 160);
  8. calibrated = 0;
  9. key_pressed = 0;
  10. }
  11. //--------------------------------------------------------------
  12. void ofApp::update(){
  13. webcam.update();
  14. if (webcam.isFrameNew()) {
  15. contour.setTargetColor(color);
  16. contour.setThreshold(30);
  17. contour.findContours(webcam);
  18. selection = 0;
  19. if (contour.size() != 0) {
  20. for (i = 1; i < contour.size(); i++) {
  21. if (contour.getContourArea(i) > contour.getContourArea(selection)) {
  22. selection = i;
  23. }
  24. }
  25. if ((color_picked == 1) && (contour.getContourArea(selection) != 0)) {
  26. center = contour.getCenter(selection);
  27. }
  28. }
  29. }
  30. }
  31. //--------------------------------------------------------------
  32. void ofApp::draw(){
  33. ofSetColor(255);
  34. webcam.draw(0, 0);
  35. contour.draw();
  36. ofFill();
  37. ofSetColor(color);
  38. ofDrawRectangle(webcam.getWidth(), 0, 128, 128);
  39. if (color_picked == 1 && key_pressed == 0) {
  40. SetCursorPos((center.x - x_offset) * x_scale, (center.y - y_offset) * y_scale);
  41. }
  42. }
  43. //--------------------------------------------------------------
  44. void ofApp::keyPressed(int key){
  45. key_pressed = 1;
  46. }
  47. //--------------------------------------------------------------
  48. void ofApp::keyReleased(int key){
  49. key_pressed = 0;
  50. }
  51. //--------------------------------------------------------------
  52. void ofApp::mouseMoved(int x, int y ){
  53. }
  54. //--------------------------------------------------------------
  55. void ofApp::mouseDragged(int x, int y, int button){
  56. }
  57. //--------------------------------------------------------------
  58. void ofApp::mousePressed(int x, int y, int button) {
  59. if (calibrated == 4) {
  60. color = webcam.getPixels().getColor(x, y);
  61. printf("%i, %i, %i\n", color.r, color.g, color.b);
  62. color_picked = 1;
  63. }
  64. if (calibrated != 4) {
  65. if (calibrated == 0) {
  66. GetCursorPos(&cp_top_left);
  67. }
  68. if (calibrated == 1) {
  69. GetCursorPos(&cp_top_right);
  70. }
  71. if (calibrated == 2) {
  72. GetCursorPos(&cp_bottom_right);
  73. }
  74. if (calibrated == 3) {
  75. GetCursorPos(&cp_bottom_left);
  76. y_offset = (cp_top_left.y + cp_top_right.y) / 2.0;
  77. y_scale = 1080 / ((cp_bottom_left.y + cp_bottom_right.y) / 2.0);
  78. x_offset = (cp_top_left.x + cp_bottom_left.x) / 2.0;
  79. x_scale = 1920 / ((cp_top_right.x + cp_bottom_right.x) / 2.0);
  80. }
  81. calibrated++;
  82. }
  83. }
  84. //--------------------------------------------------------------
  85. void ofApp::mouseReleased(int x, int y, int button){
  86. }
  87. //--------------------------------------------------------------
  88. void ofApp::mouseEntered(int x, int y){
  89. }
  90. //--------------------------------------------------------------
  91. void ofApp::mouseExited(int x, int y){
  92. }
  93. //--------------------------------------------------------------
  94. void ofApp::windowResized(int w, int h){
  95. }
  96. //--------------------------------------------------------------
  97. void ofApp::gotMessage(ofMessage msg){
  98. }
  99. //--------------------------------------------------------------
  100. void ofApp::dragEvent(ofDragInfo dragInfo){
  101. }