158 lines
4.4 KiB
C++
Raw Normal View History

2022-05-05 14:45:14 +02:00
#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()) {
contour.setTargetColor(color);
contour.setThreshold(30);
contour.findContours(webcam);
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 = contour.getCenter(selection);
}
}
}
}
//--------------------------------------------------------------
void ofApp::draw(){
ofSetColor(255);
webcam.draw(0, 0);
contour.draw();
ofFill();
ofSetColor(color);
ofDrawRectangle(webcam.getWidth(), 0, 128, 128);
if (color_picked == 1 && key_pressed == 0) {
2022-05-12 15:16:23 +02:00
scaleTrapeze();
scaleRectangel();
SetCursorPos(cp_cursor_position.x, cp_cursor_position.y);
2022-05-05 14:45:14 +02:00
}
}
//--------------------------------------------------------------
void ofApp::keyPressed(int key){
key_pressed = 1;
}
//--------------------------------------------------------------
void ofApp::keyReleased(int key){
key_pressed = 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) {
color = webcam.getPixels().getColor(x, y);
printf("%i, %i, %i\n", color.r, color.g, color.b);
2022-05-12 15:16:23 +02:00
printf("%f, %f, %f, %f \n", y_offset, y_scale, x_offset, x_scale);
2022-05-05 14:45:14 +02:00
color_picked = 1;
}
2022-05-12 15:16:23 +02:00
if (calibrated != 4 && key_pressed == 0) {
2022-05-05 14:45:14 +02:00
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);
x_offset = (cp_top_left.x + cp_bottom_left.x) / 2.0;
2022-05-12 15:16:23 +02:00
x_scale = ofGetScreenWidth() / (cp_bottom_right.x - x_offset);
y_offset = (cp_top_left.y + cp_top_right.y) / 2.0;
2022-05-05 14:45:14 +02:00
2022-05-12 15:16:23 +02:00
y_scale = ofGetScreenHeight() / ((cp_bottom_left.y + cp_bottom_right.y) / 2.0 - y_offset);
2022-05-05 14:45:14 +02:00
}
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){
}
2022-05-12 15:16:23 +02:00
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));
}
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));
}
}
void ofApp::scaleRectangel(void) {
cp_cursor_position.x = center.x * x_scale - x_offset;
cp_cursor_position.y = center.y * y_scale - y_offset;
}