123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238 |
- #include "ofApp.h"
-
- //--------------------------------------------------------------
- void ofApp::setup(){
- webcam.setup(1280, 720);
-
- contour.setMinAreaRadius(1);
- contour.setMaxAreaRadius(5);
- color_red_laser.set(185, 160, 160);
- calibrated = 0;
- key_pressed = 0;
-
- }
-
- //--------------------------------------------------------------
- void ofApp::update(){
- webcam.update();
-
- if (webcam.isFrameNew()) {
-
- rgb_immage.setFromPixels(webcam.getPixels());
- rgb_immage.blurGaussian(5);
- //hsv_immage = rgb_immage;
- //hsv_immage.convertRgbToHsv();
- //hsv_immage.blur();
-
- contour.setTargetColor(color); /*, ofxCv::TRACK_COLOR_HSV*/
- contour.setThreshold(20);
- contour.findContours(rgb_immage);
- selection = 0;
- if (contour.size() != 0) {
- for (i = 1; i < contour.size(); i++) {
- if (contour.getContourArea(i) > contour.getContourArea(selection)) {
- selection = i;
- }
- }
- if ((color_picked == 1) && (contour.getContourArea(selection) != 0)) {
- center_trapeze = contour.getCenter(selection);
- }
- }
-
-
- }
- }
-
- //--------------------------------------------------------------
- void ofApp::draw(){
- ofSetColor(255);
- rgb_immage.draw(0, 0);
-
- contour.draw();
-
- ofFill();
- ofSetColor(color);
- ofDrawRectangle(rgb_immage.getWidth(), 0, 128, 128);
- ofDrawLine(cp_top_left.x, cp_top_left.y, cp_top_right.x, cp_top_right.y);
- ofDrawLine(cp_top_right.x, cp_top_right.y, cp_bottom_right.x, cp_bottom_right.y);
- ofDrawLine(cp_bottom_right.x, cp_bottom_right.y, cp_bottom_left.x, cp_bottom_left.y);
- ofDrawLine(cp_bottom_left.x, cp_bottom_left.y, cp_top_left.x, cp_top_left.y);
- ofDrawLine(middle, cp_top_left.y, middle, cp_bottom_right.y);
- if (color_picked == 1 && key_pressed == 0) {
- scaleTrapeze();
- scaleRectangel();
- SetCursorPos(cp_cursor_position.x, cp_cursor_position.y);
- }
-
- }
-
- //--------------------------------------------------------------
- void ofApp::keyPressed(int key){
- key_pressed = 1;
-
- if (key == 57358) {
- mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
- }
- else if (key == 57356) {
- mouse_event(MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0);
- }
- else if (key == ' ') {
- select_colour = select_colour ^ 0x0001;
- }
- }
-
- //--------------------------------------------------------------
- void ofApp::keyReleased(int key){
- key_pressed = 0;
- if (key == 57358) {
- mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
- }
- else if (key == 57356) {
- mouse_event(MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0);
- }
- }
-
- //--------------------------------------------------------------
- void ofApp::mouseMoved(int x, int y ){
-
- }
-
- //--------------------------------------------------------------
- void ofApp::mouseDragged(int x, int y, int button){
-
- }
-
- //--------------------------------------------------------------
- void ofApp::mousePressed(int x, int y, int button) {
- if (calibrated == 4 && select_colour == 0x0001) {
- color = rgb_immage.getPixels().getColor(x, y);
- printf("%i, %i, %i\n", color.r, color.g, color.b);
- printf("%f, %f, %f, %f \n", y_offset, y_scale, x_offset, x_scale);
- color_picked = 1;
- }
- if (calibrated != 4 && key_pressed == 0) {
- if (calibrated == 0) {
- GetCursorPos(&cp_top_left);
- }
- if (calibrated == 1) {
- GetCursorPos(&cp_top_right);
- }
- if (calibrated == 2) {
- GetCursorPos(&cp_bottom_right);
- }
- if (calibrated == 3) {
- GetCursorPos(&cp_bottom_left);
- if (cp_top_left.x > cp_bottom_left.x)
- {
- x_offset = cp_bottom_left.x;
- }
- else
- {
- x_offset = cp_top_left.x;
- }
-
- if (cp_top_right.x < cp_bottom_right.x)
- {
- x_scale = ofGetScreenWidth() / (cp_bottom_right.x - x_offset);
- }
- else
- {
- x_scale = ofGetScreenWidth() / (cp_top_right.x - x_offset);
- }
-
- if (cp_top_left.y > cp_top_right.y)
- {
- y_offset = cp_top_right.y;
- }
- else
- {
- y_offset = cp_top_left.y;
- }
-
- if (cp_bottom_left.y < cp_bottom_right.y)
- {
- y_scale = ofGetScreenHeight() / (cp_bottom_right.y - y_offset);
- }
- else
- {
- y_scale = ofGetScreenHeight() / (cp_bottom_left.y - y_offset);
- }
-
- printf("Y:%f \nX:%f\n", y_offset, x_offset);
- }
- calibrated++;
- }
- }
-
- //--------------------------------------------------------------
- void ofApp::mouseReleased(int x, int y, int button){
-
- }
-
- //--------------------------------------------------------------
- void ofApp::mouseEntered(int x, int y){
-
- }
-
- //--------------------------------------------------------------
- void ofApp::mouseExited(int x, int y){
-
- }
-
- //--------------------------------------------------------------
- void ofApp::windowResized(int w, int h){
-
- }
-
- //--------------------------------------------------------------
- void ofApp::gotMessage(ofMessage msg){
-
- }
-
- //--------------------------------------------------------------
- void ofApp::dragEvent(ofDragInfo dragInfo){
-
- }
-
-
- void ofApp::scaleTrapeze(void) {
- left_pitch = (float)(cp_bottom_left.y - cp_top_left.y) / (float)(cp_bottom_left.x - cp_top_left.x);
- right_pitch = (float) (cp_bottom_right.y - cp_top_right.y) / (float)(cp_bottom_right.x - cp_top_right.x);
- middle = (cp_top_right.x + cp_top_left.x)/2;
- stringstream out;
- out.precision(2);
- out << "Middle:" << middle;
- string message = out.str();
- ofDrawBitmapString(message, 100, 100);
- out.str("");
- out << "rightpitch:" << right_pitch;
- message = out.str();
- ofDrawBitmapString(message, 100, 120);
- out.str("");
- out << "leftpitch:" << left_pitch;
- message = out.str();
- ofDrawBitmapString(message, 100, 140);
- if (center_trapeze.x > middle) {
- float mid_to_dot = center_trapeze.x - middle;
- float distance_to_rect = ((cp_bottom_right.y - center_trapeze.y) / right_pitch);
- float mid_to_max = (cp_bottom_right.x - middle) - distance_to_rect;
- center_rect.x = center_trapeze.x + ((distance_to_rect) * ((mid_to_dot)/(mid_to_max)));
- }
- else {
- float mid_to_dot = middle - center_trapeze.x;
- float distance_to_rect = ((cp_bottom_left.y - center_trapeze.y) / left_pitch);
- float mid_to_max = (middle - cp_bottom_left.x) - distance_to_rect;
- center_rect.x = center_trapeze.x + ((distance_to_rect) * ((mid_to_dot)/(mid_to_max)));
- }
- }
-
- void ofApp::scaleRectangel(void) {
- cp_cursor_position.x = (center_rect.x - x_offset) * x_scale;
- cp_cursor_position.y = (center_trapeze.y - y_offset )* y_scale ;
- stringstream out;
- out.precision(2);
- out << "Cursor:" << "x" << cp_cursor_position.x << " | y"<< cp_cursor_position.y;
- string message = out.str();
- ofDrawBitmapString(message, 100, 160);
- out.str("");
- }
|