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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. rgb_immage.setFromPixels(webcam.getPixels());
  16. rgb_immage.blurGaussian(5);
  17. //hsv_immage = rgb_immage;
  18. //hsv_immage.convertRgbToHsv();
  19. //hsv_immage.blur();
  20. contour.setTargetColor(color); /*, ofxCv::TRACK_COLOR_HSV*/
  21. contour.setThreshold(20);
  22. contour.findContours(rgb_immage);
  23. selection = 0;
  24. if (contour.size() != 0) {
  25. for (i = 1; i < contour.size(); i++) {
  26. if (contour.getContourArea(i) > contour.getContourArea(selection)) {
  27. selection = i;
  28. }
  29. }
  30. if ((color_picked == 1) && (contour.getContourArea(selection) != 0)) {
  31. center_trapeze = contour.getCenter(selection);
  32. }
  33. }
  34. }
  35. }
  36. //--------------------------------------------------------------
  37. void ofApp::draw(){
  38. ofSetColor(255);
  39. rgb_immage.draw(0, 0);
  40. contour.draw();
  41. ofFill();
  42. ofSetColor(color);
  43. ofDrawRectangle(rgb_immage.getWidth(), 0, 128, 128);
  44. ofDrawLine(cp_top_left.x, cp_top_left.y, cp_top_right.x, cp_top_right.y);
  45. ofDrawLine(cp_top_right.x, cp_top_right.y, cp_bottom_right.x, cp_bottom_right.y);
  46. ofDrawLine(cp_bottom_right.x, cp_bottom_right.y, cp_bottom_left.x, cp_bottom_left.y);
  47. ofDrawLine(cp_bottom_left.x, cp_bottom_left.y, cp_top_left.x, cp_top_left.y);
  48. ofDrawLine(middle, cp_top_left.y, middle, cp_bottom_right.y);
  49. if (color_picked == 1 && key_pressed == 0) {
  50. scaleTrapeze();
  51. scaleRectangel();
  52. SetCursorPos(cp_cursor_position.x, cp_cursor_position.y);
  53. }
  54. }
  55. //--------------------------------------------------------------
  56. void ofApp::keyPressed(int key){
  57. key_pressed = 1;
  58. if (key == 57358) {
  59. mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
  60. }
  61. else if (key == 57356) {
  62. mouse_event(MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0);
  63. }
  64. else if (key == ' ') {
  65. select_colour = select_colour ^ 0x0001;
  66. }
  67. }
  68. //--------------------------------------------------------------
  69. void ofApp::keyReleased(int key){
  70. key_pressed = 0;
  71. if (key == 57358) {
  72. mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
  73. }
  74. else if (key == 57356) {
  75. mouse_event(MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0);
  76. }
  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. if (calibrated == 4 && select_colour == 0x0001) {
  87. color = rgb_immage.getPixels().getColor(x, y);
  88. printf("%i, %i, %i\n", color.r, color.g, color.b);
  89. printf("%f, %f, %f, %f \n", y_offset, y_scale, x_offset, x_scale);
  90. color_picked = 1;
  91. }
  92. if (calibrated != 4 && key_pressed == 0) {
  93. if (calibrated == 0) {
  94. GetCursorPos(&cp_top_left);
  95. }
  96. if (calibrated == 1) {
  97. GetCursorPos(&cp_top_right);
  98. }
  99. if (calibrated == 2) {
  100. GetCursorPos(&cp_bottom_right);
  101. }
  102. if (calibrated == 3) {
  103. GetCursorPos(&cp_bottom_left);
  104. if (cp_top_left.x > cp_bottom_left.x)
  105. {
  106. x_offset = cp_bottom_left.x;
  107. }
  108. else
  109. {
  110. x_offset = cp_top_left.x;
  111. }
  112. if (cp_top_right.x < cp_bottom_right.x)
  113. {
  114. x_scale = ofGetScreenWidth() / (cp_bottom_right.x - x_offset);
  115. }
  116. else
  117. {
  118. x_scale = ofGetScreenWidth() / (cp_top_right.x - x_offset);
  119. }
  120. if (cp_top_left.y > cp_top_right.y)
  121. {
  122. y_offset = cp_top_right.y;
  123. }
  124. else
  125. {
  126. y_offset = cp_top_left.y;
  127. }
  128. if (cp_bottom_left.y < cp_bottom_right.y)
  129. {
  130. y_scale = ofGetScreenHeight() / (cp_bottom_right.y - y_offset);
  131. }
  132. else
  133. {
  134. y_scale = ofGetScreenHeight() / (cp_bottom_left.y - y_offset);
  135. }
  136. printf("Y:%f \nX:%f\n", y_offset, x_offset);
  137. }
  138. calibrated++;
  139. }
  140. }
  141. //--------------------------------------------------------------
  142. void ofApp::mouseReleased(int x, int y, int button){
  143. }
  144. //--------------------------------------------------------------
  145. void ofApp::mouseEntered(int x, int y){
  146. }
  147. //--------------------------------------------------------------
  148. void ofApp::mouseExited(int x, int y){
  149. }
  150. //--------------------------------------------------------------
  151. void ofApp::windowResized(int w, int h){
  152. }
  153. //--------------------------------------------------------------
  154. void ofApp::gotMessage(ofMessage msg){
  155. }
  156. //--------------------------------------------------------------
  157. void ofApp::dragEvent(ofDragInfo dragInfo){
  158. }
  159. void ofApp::scaleTrapeze(void) {
  160. left_pitch = (float)(cp_bottom_left.y - cp_top_left.y) / (float)(cp_bottom_left.x - cp_top_left.x);
  161. right_pitch = (float) (cp_bottom_right.y - cp_top_right.y) / (float)(cp_bottom_right.x - cp_top_right.x);
  162. middle = (cp_top_right.x + cp_top_left.x)/2;
  163. stringstream out;
  164. out.precision(2);
  165. out << "Middle:" << middle;
  166. string message = out.str();
  167. ofDrawBitmapString(message, 100, 100);
  168. out.str("");
  169. out << "rightpitch:" << right_pitch;
  170. message = out.str();
  171. ofDrawBitmapString(message, 100, 120);
  172. out.str("");
  173. out << "leftpitch:" << left_pitch;
  174. message = out.str();
  175. ofDrawBitmapString(message, 100, 140);
  176. if (center_trapeze.x > middle) {
  177. float mid_to_dot = center_trapeze.x - middle;
  178. float distance_to_rect = ((cp_bottom_right.y - center_trapeze.y) / right_pitch);
  179. float mid_to_max = (cp_bottom_right.x - middle) - distance_to_rect;
  180. center_rect.x = center_trapeze.x + ((distance_to_rect) * ((mid_to_dot)/(mid_to_max)));
  181. }
  182. else {
  183. float mid_to_dot = middle - center_trapeze.x;
  184. float distance_to_rect = ((cp_bottom_left.y - center_trapeze.y) / left_pitch);
  185. float mid_to_max = (middle - cp_bottom_left.x) - distance_to_rect;
  186. center_rect.x = center_trapeze.x + ((distance_to_rect) * ((mid_to_dot)/(mid_to_max)));
  187. }
  188. }
  189. void ofApp::scaleRectangel(void) {
  190. cp_cursor_position.x = (center_rect.x - x_offset) * x_scale;
  191. cp_cursor_position.y = (center_trapeze.y - y_offset )* y_scale ;
  192. stringstream out;
  193. out.precision(2);
  194. out << "Cursor:" << "x" << cp_cursor_position.x << " | y"<< cp_cursor_position.y;
  195. string message = out.str();
  196. ofDrawBitmapString(message, 100, 160);
  197. out.str("");
  198. }