diff --git a/src/ofApp.cpp b/src/ofApp.cpp index 6d23d9c..ff0d0ec 100644 --- a/src/ofApp.cpp +++ b/src/ofApp.cpp @@ -28,7 +28,7 @@ void ofApp::update(){ } } if ((color_picked == 1) && (contour.getContourArea(selection) != 0)) { - center = contour.getCenter(selection); + center_trapeze = contour.getCenter(selection); } } @@ -46,6 +46,11 @@ void ofApp::draw(){ ofFill(); ofSetColor(color); ofDrawRectangle(webcam.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(); @@ -94,15 +99,43 @@ void ofApp::mousePressed(int x, int y, int button) { } 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; + } - x_offset = (cp_top_left.x + cp_bottom_left.x) / 2.0; - - x_scale = ofGetScreenWidth() / (cp_bottom_right.x - x_offset); - - y_offset = (cp_top_left.y + cp_top_right.y) / 2.0; - - y_scale = ofGetScreenHeight() / ((cp_bottom_left.y + cp_bottom_right.y) / 2.0 - y_offset); + 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++; } @@ -140,19 +173,37 @@ void ofApp::dragEvent(ofDragInfo dragInfo){ void ofApp::scaleTrapeze(void) { - left_pitch = (cp_top_left.y - cp_bottom_left.y) / (cp_top_left.x - cp_bottom_left.x); - right_pitch = (cp_top_right.y - cp_bottom_right.y) / (cp_top_right.x - cp_bottom_right.x); - middle = cp_top_right.x - cp_top_left.x; - - if (center.x > middle) { - 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)); + 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) { + center_rect.x = center_trapeze.x - (((center_trapeze.y - cp_top_right.y) / right_pitch));// * (((cp_bottom_right.x - middle) - ((center_trapeze.y - cp_top_right.y) / right_pitch)) / (center_trapeze.x - middle))); } else { - 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)); + center_rect.x = center_trapeze.x - (((center_trapeze.y - cp_top_left.y) / left_pitch));//* (((middle - cp_bottom_left.x) - ((center_trapeze.y - cp_top_left.y) / left_pitch)) / (middle - center_trapeze.x))); } } void ofApp::scaleRectangel(void) { - cp_cursor_position.x = center.x * x_scale - x_offset; - cp_cursor_position.y = center.y * y_scale - y_offset; + 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(""); } \ No newline at end of file diff --git a/src/ofApp.h b/src/ofApp.h index 8856a9f..3e957ad 100644 --- a/src/ofApp.h +++ b/src/ofApp.h @@ -4,6 +4,9 @@ #include "ofxOpenCv.h" #include "ofxCv.h" #include "windows.h" +#include // std::string +#include // std::cout +#include // std::stringstream, std::stringbu class ofApp : public ofBaseApp{ @@ -28,7 +31,8 @@ class ofApp : public ofBaseApp{ ofVideoGrabber webcam; ofxCv::ContourFinder contour; - cv::Point2f center; + cv::Point2f center_trapeze; + cv::Point2f center_rect; ofColor color; ofColor color_red_laser; int color_picked;