Created
August 2, 2015 14:40
-
-
Save ericoporto/7ce0bee65af15fa51edd to your computer and use it in GitHub Desktop.
This script uses 2 webcams, calculate difference per frame in each, get the position of the biggest difference and then calculate distance from the camera. Made with Processing IDE.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// First example on image processing | |
// Finds the square with bigger difference from the latter | |
// Do it for both cams | |
// Guess that it's in the same object | |
// Tell the distance from the object | |
// Prototype to be further rewritten in OpenCV | |
// This code uses snippets form this great german website: | |
// http://www.creativecoding.org/lesson/topics/video/video-in-processing | |
// Aktivität in Bildbereichen feststellen in Processing | |
import processing.video.*; | |
final int VIDEO_WIDTH = 320; | |
final int VIDEO_HEIGHT = 240; | |
final int VIDEO_COLS = 32; | |
final int VIDEO_ROWS = 24; | |
float[] activityR = new float[VIDEO_COLS * VIDEO_ROWS]; | |
float[] buffer1R = new float[VIDEO_WIDTH * VIDEO_HEIGHT]; | |
float[] buffer2R = new float[buffer1R.length]; | |
float[] buffer3R = new float[buffer1R.length]; | |
float[] activityL = new float[VIDEO_COLS * VIDEO_ROWS]; | |
float[] buffer1L = new float[VIDEO_WIDTH * VIDEO_HEIGHT]; | |
float[] buffer2L = new float[buffer1L.length]; | |
float[] buffer3L = new float[buffer1L.length]; | |
float Max = 0; | |
int maxIndex; | |
Capture camR = null; | |
Capture camL = null; | |
int POSITIONXR = 0; | |
int POSITIONXL = 0; | |
float Distance = 0; | |
int maxIndexN(float[] array) { | |
float Max = 0; | |
int maxIndex = 0; | |
for (int i = 0; i < array.length; i++) { | |
if (array[i] > Max) { | |
Max = array[i]; | |
maxIndex = i; | |
} | |
} | |
return maxIndex; | |
} | |
void setup() { | |
size(640, 240); | |
// camR = new Capture (this, VIDEO_WIDTH, VIDEO_HEIGHT,"LG 3D R Webcam", 30); | |
// camL = new Capture (this, VIDEO_WIDTH, VIDEO_HEIGHT,"LG 3D L Webcam", 30); | |
camR = new Capture(this, VIDEO_WIDTH, VIDEO_HEIGHT, "/dev/video0", 30); | |
camL = new Capture(this, VIDEO_WIDTH, VIDEO_HEIGHT, "/dev/video1", 30); | |
frameRate(15); | |
camR.start(); | |
camL.start(); | |
} | |
void draw() { | |
if (camR.available()) { | |
camR.read(); | |
int index; | |
int pxPerCol = VIDEO_WIDTH / VIDEO_COLS; | |
int pxPerRow = VIDEO_HEIGHT / VIDEO_ROWS; | |
image(camR, 0, 0); | |
for (int i = 0; i < activityR.length; i++) { | |
activityR[i] = 0; | |
} | |
for (int i = 0; i < camR.pixels.length; i++) { | |
//Calculates activity for the Right Camera | |
int x = (int)((i % camR.width) / pxPerCol); | |
int y = (int)((i / camR.width) / pxPerRow); | |
index = y * VIDEO_COLS + x; | |
color col = camR.pixels[i]; | |
float sum = red(col) + green(col) + blue(col); | |
float deltaPixel = (buffer1R[i] + buffer2R[i] + buffer3R[i]) / 3 - sum; | |
if (deltaPixel < 0) { | |
deltaPixel *= -1; | |
} | |
activityR[index] += deltaPixel; | |
buffer3R[i] = buffer2R[i]; | |
buffer2R[i] = buffer1R[i]; | |
buffer1R[i] = sum; | |
} | |
int numeroQ = maxIndexN(activityR); | |
// This simply plots the X,Y position of the maxActivity index | |
textSize(32); | |
fill(255, 255, 255); | |
text(numeroQ % VIDEO_COLS, 10, 30); | |
text(",", 50, 30); | |
text(numeroQ / VIDEO_COLS, 60, 30); | |
POSITIONXR = numeroQ % VIDEO_COLS; | |
// Set activity for the right camera | |
for (int i = 0; i < activityR.length; i++) { | |
activityR[i] /= pxPerCol * pxPerRow; | |
stroke(255, 20); | |
fill(0, 255, 230, activityR[i]); | |
rect((i % VIDEO_COLS) * pxPerCol, (i / VIDEO_COLS) * pxPerRow, pxPerCol, pxPerRow); | |
} | |
} | |
if (camL.available()) { | |
camL.read(); | |
int index; | |
int pxPerCol = VIDEO_WIDTH / VIDEO_COLS; | |
int pxPerRow = VIDEO_HEIGHT / VIDEO_ROWS; | |
image(camL, 320, 0); | |
for (int i = 0; i < activityL.length; i++) { | |
activityL[i] = 0; | |
} | |
for (int i = 0; i < camL.pixels.length; i++) { | |
// Calculate activity for the Left Camera | |
int x = (int)((i % camL.width) / pxPerCol); | |
int y = (int)((i / camL.width) / pxPerRow); | |
index = y * VIDEO_COLS + x; | |
color col = camL.pixels[i]; | |
float sum = red(col) + green(col) + blue(col); | |
float deltaPixel = (buffer1L[i] + buffer2L[i] + buffer3L[i]) / 3 - sum; | |
if (deltaPixel < 0) { | |
deltaPixel *= -1; | |
} | |
activityL[index] += deltaPixel; | |
buffer3L[i] = buffer2L[i]; | |
buffer2L[i] = buffer1L[i]; | |
buffer1L[i] = sum; | |
} | |
int maxIndexN = maxIndexN(activityL); | |
// Just here to write the maxActivity X,Y position | |
textSize(32); | |
fill(255, 255, 255); | |
text(maxIndexN % VIDEO_COLS, 330, 30); | |
text(",", 370, 30); | |
text(maxIndexN / VIDEO_COLS, 380, 30); | |
POSITIONXL = maxIndexN % VIDEO_COLS; | |
for (int i = 0; i < activityL.length; i++) { | |
//Just to ´rint out the activity | |
activityL[i] /= pxPerCol * pxPerRow; | |
stroke(255, 20); | |
fill(0, 255, 230, activityL[i]); | |
rect((i % VIDEO_COLS) * pxPerCol + 320, (i / VIDEO_COLS) * pxPerRow, pxPerCol, pxPerRow); | |
} | |
// Calculates distance, uses 50° as the viewing angle | |
Distance = 35 * VIDEO_COLS / (2 * tan(0.436) * (POSITIONXL - POSITIONXR)); | |
// Prints out the calculated distance | |
textSize(32); | |
fill(255, 255, 255); | |
text(Distance, 200, 200); | |
text("DISTANCE =", 10, 200); | |
} | |
} | |
// Conclusion here is that the most hard is how to tell that whatever you select on left camera to find | |
// the distance from camera, what's the same thing on the right camera. | |
// | |
// Copyright 2014 Érico Porto | |
// | |
// Licensed under the Apache License, Version 2.0 (the "License"); | |
// you may not use this file except in compliance with the License. | |
// You may obtain a copy of the License at | |
// | |
// http://www.apache.org/licenses/LICENSE-2.0 | |
// | |
// Unless required by applicable law or agreed to in writing, software | |
// distributed under the License is distributed on an "AS IS" BASIS, | |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
// See the License for the specific language governing permissions and | |
// limitations under the License. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment