Browse Source

Wow coole verbesserungen

Coole_verbesserung
poepelal78331 2 years ago
parent
commit
aa7ee4d791
2 changed files with 73 additions and 18 deletions
  1. 68
    17
      src/ofApp.cpp
  2. 5
    1
      src/ofApp.h

+ 68
- 17
src/ofApp.cpp View File

} }
} }
if ((color_picked == 1) && (contour.getContourArea(selection) != 0)) { if ((color_picked == 1) && (contour.getContourArea(selection) != 0)) {
center = contour.getCenter(selection);
center_trapeze = contour.getCenter(selection);
} }
} }
ofFill(); ofFill();
ofSetColor(color); ofSetColor(color);
ofDrawRectangle(webcam.getWidth(), 0, 128, 128); 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) { if (color_picked == 1 && key_pressed == 0) {
scaleTrapeze(); scaleTrapeze();
scaleRectangel(); scaleRectangel();
} }
if (calibrated == 3) { if (calibrated == 3) {
GetCursorPos(&cp_bottom_left); 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++; calibrated++;
} }




void ofApp::scaleTrapeze(void) { 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 { 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) { 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("");
} }

+ 5
- 1
src/ofApp.h View File

#include "ofxOpenCv.h" #include "ofxOpenCv.h"
#include "ofxCv.h" #include "ofxCv.h"
#include "windows.h" #include "windows.h"
#include <string> // std::string
#include <iostream> // std::cout
#include <sstream> // std::stringstream, std::stringbu


class ofApp : public ofBaseApp{ class ofApp : public ofBaseApp{




ofVideoGrabber webcam; ofVideoGrabber webcam;
ofxCv::ContourFinder contour; ofxCv::ContourFinder contour;
cv::Point2f center;
cv::Point2f center_trapeze;
cv::Point2f center_rect;
ofColor color; ofColor color;
ofColor color_red_laser; ofColor color_red_laser;
int color_picked; int color_picked;

Loading…
Cancel
Save