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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. scaleTrapeze();
  41. scaleRectangel();
  42. SetCursorPos(cp_cursor_position.x, cp_cursor_position.y);
  43. }
  44. }
  45. //--------------------------------------------------------------
  46. void ofApp::keyPressed(int key){
  47. key_pressed = 1;
  48. }
  49. //--------------------------------------------------------------
  50. void ofApp::keyReleased(int key){
  51. key_pressed = 0;
  52. }
  53. //--------------------------------------------------------------
  54. void ofApp::mouseMoved(int x, int y ){
  55. }
  56. //--------------------------------------------------------------
  57. void ofApp::mouseDragged(int x, int y, int button){
  58. }
  59. //--------------------------------------------------------------
  60. void ofApp::mousePressed(int x, int y, int button) {
  61. if (calibrated == 4) {
  62. color = webcam.getPixels().getColor(x, y);
  63. printf("%i, %i, %i\n", color.r, color.g, color.b);
  64. printf("%f, %f, %f, %f \n", y_offset, y_scale, x_offset, x_scale);
  65. color_picked = 1;
  66. }
  67. if (calibrated != 4 && key_pressed == 0) {
  68. if (calibrated == 0) {
  69. GetCursorPos(&cp_top_left);
  70. }
  71. if (calibrated == 1) {
  72. GetCursorPos(&cp_top_right);
  73. }
  74. if (calibrated == 2) {
  75. GetCursorPos(&cp_bottom_right);
  76. }
  77. if (calibrated == 3) {
  78. GetCursorPos(&cp_bottom_left);
  79. x_offset = (cp_top_left.x + cp_bottom_left.x) / 2.0;
  80. x_scale = ofGetScreenWidth() / (cp_bottom_right.x - x_offset);
  81. y_offset = (cp_top_left.y + cp_top_right.y) / 2.0;
  82. y_scale = ofGetScreenHeight() / ((cp_bottom_left.y + cp_bottom_right.y) / 2.0 - y_offset);
  83. }
  84. calibrated++;
  85. }
  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. }
  105. void ofApp::scaleTrapeze(void) {
  106. left_pitch = (cp_top_left.y - cp_bottom_left.y) / (cp_top_left.x - cp_bottom_left.x);
  107. right_pitch = (cp_top_right.y - cp_bottom_right.y) / (cp_top_right.x - cp_bottom_right.x);
  108. middle = cp_top_right.x - cp_top_left.x;
  109. if (center.x > middle) {
  110. center.x = center.x + (((center.y - cp_top_right.y) / right_pitch) * ((cp_bottom_right.x - middle) - ((center.y - cp_top_right.y) / right_pitch)) / (center.x - middle));
  111. }
  112. else {
  113. center.x = center.x + (((center.y - cp_top_left.y) / left_pitch) * ((middle - cp_bottom_left.x) - ((center.y - cp_top_left.y) / left_pitch)) / (middle - center.x));
  114. }
  115. }
  116. void ofApp::scaleRectangel(void) {
  117. cp_cursor_position.x = center.x * x_scale - x_offset;
  118. cp_cursor_position.y = center.y * y_scale - y_offset;
  119. }