1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
// Smoother color tracking // based on Dan Shiffman & Dan O'Sullivan's examples import processing.video.*; import processing.serial.*; Serial mySerial; // Variable for capture device Capture video; // A variable for the color we are searching for. color trackColor; float thresh = 19; int avgX, avgY; //this is what we are trying to find float xPos; float yPos; PImage bridge; void setup() { size(800, 600); video = new Capture(this, width, height); video.start(); // Start off tracking for red trackColor = color(255, 0, 0); bridge =loadImage("bridge.jpg"); String portName=Serial.list()[4]; //replace number with port STARTS WITH ZERO mySerial =new Serial(this,portName,9600); mySerial.readStringUntil('\n'); } void draw() { tint( 255, 255 ); // No tint // Capture and display the video if (video.available()) { video.read(); } video.loadPixels(); image(video, 0, 0); //we are going to find the average location of changed pixels so //we will need the sum of all the x find, the sum of all the y // find and the total finds int totalFoundPixels= 0; int sumX = 0; int sumY = 0; // Begin loop to walk through every pixel for (int x = 0; x < video.width; x ++ ) { for (int y = 0; y < video.height; y ++ ) { int loc = x + y*video.width; // What is current color color currentColor = video.pixels[loc]; float r1 = red(currentColor); float g1 = green(currentColor); float b1 = blue(currentColor); // colors we are looking for float r2 = red(trackColor); float g2 = green(trackColor); float b2 = blue(trackColor); // Using euclidean distance to compare colors float difference = dist(r1, g1, b1, r2, g2, b2); // We are using the dist( ) function to compare the current // color with the color we are tracking. // If current color is more similar to tracked color than // closest color, save current location and current difference if (difference < thresh) { sumX = sumX + x; sumY= sumY + y; totalFoundPixels++; } } } // average the locations of all the pixels that were close enough. if (totalFoundPixels > 0) { avgX = sumX/totalFoundPixels; avgY = sumY/totalFoundPixels; // Draw a circle at the tracked pixel fill(trackColor); strokeWeight(4.0); stroke(0, 127); ellipse(avgX-10, (avgY-10), 20, 20); println("L"); } image(bridge,500,400); if (totalFoundPixels <=0) { tint(255,0,0,500); image(bridge,500,400); println("H"); } } void serialEvent (Serial myPort){ if(mySerial ==null)return; //if nothing inside, but fires, sidestep String input = mySerial.readStringUntil('\n'); if(input != null) { //if input inside, get input, get rid of white space, casting println("H"); input =input.trim(); // // diam =float(input); //casting transmits into value // println(diam); //prints out value }} void mousePressed() { //Save color where the mouse is clicked in trackColor variable int loc = mouseX + mouseY*video.width; trackColor = video.pixels[loc]; } void keyPressed() { //for adjusting things on the fly if (key == '-') { thresh--; println("Threshold " + thresh); } else if (key == '=') { thresh++; println("Threshold " + thresh); } } |