#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) { SetCursorPos((center.x - x_offset) * x_scale, (center.y - y_offset) * y_scale); } } //-------------------------------------------------------------- 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); color_picked = 1; } if (calibrated != 4) { 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); 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); } 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){ }