Compare commits

..

4 Commits

Author SHA1 Message Date
781d94004a Tracking mit RGB 2022-06-09 14:34:49 +02:00
ee084dd3b9 Convert to HSV (ohen tolle filterung)
Mouse Click mit Pfeiltasten
2022-05-19 15:34:56 +02:00
poepelal78331
5aefc9c0b3 trapezkorrektur implementiert 2022-05-14 19:35:21 +02:00
poepelal78331
aa7ee4d791 Wow coole verbesserungen 2022-05-14 17:48:14 +02:00
2 changed files with 112 additions and 25 deletions

View File

@ -17,9 +17,16 @@ void ofApp::update(){
webcam.update();
if (webcam.isFrameNew()) {
contour.setTargetColor(color);
contour.setThreshold(30);
contour.findContours(webcam);
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++) {
@ -28,7 +35,7 @@ void ofApp::update(){
}
}
if ((color_picked == 1) && (contour.getContourArea(selection) != 0)) {
center = contour.getCenter(selection);
center_trapeze = contour.getCenter(selection);
}
}
@ -39,13 +46,18 @@ void ofApp::update(){
//--------------------------------------------------------------
void ofApp::draw(){
ofSetColor(255);
webcam.draw(0, 0);
rgb_immage.draw(0, 0);
contour.draw();
ofFill();
ofSetColor(color);
ofDrawRectangle(webcam.getWidth(), 0, 128, 128);
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();
@ -57,11 +69,27 @@ void ofApp::draw(){
//--------------------------------------------------------------
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);
}
}
//--------------------------------------------------------------
@ -76,8 +104,8 @@ void ofApp::mouseDragged(int x, int y, int button){
//--------------------------------------------------------------
void ofApp::mousePressed(int x, int y, int button) {
if (calibrated == 4) {
color = webcam.getPixels().getColor(x, y);
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;
@ -94,15 +122,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 +196,43 @@ 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) {
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 {
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));
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.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("");
}

View File

@ -4,6 +4,9 @@
#include "ofxOpenCv.h"
#include "ofxCv.h"
#include "windows.h"
#include <string> // std::string
#include <iostream> // std::cout
#include <sstream> // std::stringstream, std::stringbu
class ofApp : public ofBaseApp{
@ -27,8 +30,11 @@ class ofApp : public ofBaseApp{
void scaleTrapeze(void);
ofVideoGrabber webcam;
ofxCvColorImage rgb_immage;
ofxCvColorImage hsv_immage;
ofxCv::ContourFinder contour;
cv::Point2f center;
cv::Point2f center_trapeze;
cv::Point2f center_rect;
ofColor color;
ofColor color_red_laser;
int color_picked;
@ -37,6 +43,7 @@ class ofApp : public ofBaseApp{
int biggest_contour;
unsigned int selection;
int calibrated;
int select_colour;
float y_scale;
float y_offset;
float x_scale;