Compare commits

...
2 changed files with 134 additions and 19 deletions

No files matched your search

+120 -17
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,15 +46,22 @@ 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) {
SetCursorPos((center.x - x_offset) * x_scale, (center.y - y_offset) * y_scale);
scaleTrapeze();
scaleRectangel();
SetCursorPos(cp_cursor_position.x, cp_cursor_position.y);
}
}
@@ -55,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);
}
}
//--------------------------------------------------------------
@@ -74,12 +104,13 @@ 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;
}
if (calibrated != 4) {
if (calibrated != 4 && key_pressed == 0) {
if (calibrated == 0) {
GetCursorPos(&cp_top_left);
}
@@ -91,14 +122,43 @@ void ofApp::mousePressed(int x, int y, int button) {
}
if (calibrated == 3) {
GetCursorPos(&cp_bottom_left);
y_offset = (cp_top_left.y + cp_top_right.y) / 2.0;
y_scale = 1080 / ((cp_bottom_left.y + cp_bottom_right.y) / 2.0);
x_offset = (cp_top_left.x + cp_bottom_left.x) / 2.0;
x_scale = 1920 / ((cp_top_right.x + cp_bottom_right.x) / 2.0);
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++;
}
@@ -133,3 +193,46 @@ 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("");
}
+14 -2
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{
@@ -23,10 +26,15 @@ class ofApp : public ofBaseApp{
void windowResized(int w, int h);
void dragEvent(ofDragInfo dragInfo);
void gotMessage(ofMessage msg);
void scaleRectangel(void);
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;
@@ -35,12 +43,16 @@ class ofApp : public ofBaseApp{
int biggest_contour;
unsigned int selection;
int calibrated;
int select_colour;
float y_scale;
float y_offset;
float x_scale;
float x_offset;
float left_pitch;
float right_pitch;
float middle;
POINT cp_top_left, cp_top_right, cp_bottom_left, cp_bottom_right;
POINT cp_top_left, cp_top_right, cp_bottom_left, cp_bottom_right, cp_cursor_position;
};