var cv = require('./node_modules/opencv/lib/opencv'); | |
var sleep = require('sleep'); | |
var camera = new cv.VideoCapture(0); //open camera | |
//set the video size to 512x288 | |
camera.setWidth(512); | |
camera.setHeight(288); | |
var window = new cv.NamedWindow('Camera'); | |
var firstFrame, frameDelta, gray, thresh; | |
sleep.sleep(3); | |
camera.read(function(err, frame){ | |
firstFrame = frame; | |
//convert to grayscale | |
firstFrame.cvtColor('CV_BGR2GRAY'); | |
firstFrame.gaussianBlur([21, 21]); | |
}); | |
interval = setInterval(function() { | |
camera.read(function(err, frame) { | |
gray = frame.copy(); | |
gray.cvtColor('CV_BGR2GRAY'); | |
gray.gaussianBlur([21, 21]); | |
frameDelta = new cv.Matrix(); | |
//compute difference between first frame and current frame | |
frameDelta.absDiff(firstFrame, gray); | |
thresh = frameDelta.threshold(25,255); | |
thresh.dilate(2); | |
var cnts = thresh.findContours(); | |
for(i = 0; i < cnts.size(); i++) { | |
if(cnts.area(i) < 500) { | |
continue; | |
} | |
frame.putText("Motion Detected", 10, 20, cv.FONT_HERSHEY_SIMPLEX, [0, 0, 255], 0.75, 2); | |
} | |
window.show(frame); | |
keyPressed = window.blockingWaitKey(0, 50); | |
if(keyPressed == 27) { | |
//exit if ESC is pressed | |
clearInterval(interval); | |
} | |
}); | |
}, 20); |
require 'opencv' | |
include OpenCV | |
camera = CvCapture.open #open camera | |
#set the video size to 512x288 | |
camera.width = 512 | |
camera.height = 288 | |
window = GUI::Window.new 'Camera' | |
sleep 3 | |
#convert to grayscale | |
firstFrame = camera.query.BGR2GRAY | |
firstFrame = firstFrame.smooth CV_GAUSSIAN, 21, 21, 0 | |
while not GUI::wait_key 1 | |
frame = camera.query | |
gray = frame.BGR2GRAY | |
gray = gray.smooth CV_GAUSSIAN, 21, 21, 0 | |
frameDelta = firstFrame.abs_diff gray | |
thresh = frameDelta.threshold 25, 255, CV_THRESH_BINARY | |
thresh = thresh.dilate nil, 2 | |
cnts = thresh.find_contours(:mode => CV_RETR_EXTERNAL, :method => CV_CHAIN_APPROX_SIMPLE) | |
while cnts | |
if cnts.contour_area < 500 | |
cnts = cnts.h_next | |
next | |
end | |
frame.put_text!("Motion Detected", CvPoint.new(10, 20), CvFont.new(:simplex), CvColor::Red) | |
cnts = cnts.h_next | |
end | |
window.show frame | |
end |
package com.ferdinandsilva; | |
import java.util.ArrayList; | |
import java.util.List; | |
import org.opencv.core.Core; | |
import org.opencv.core.Mat; | |
import org.opencv.core.MatOfPoint; | |
import org.opencv.core.Point; | |
import org.opencv.core.Size; | |
import org.opencv.imgproc.Imgproc; | |
import org.opencv.videoio.VideoCapture; | |
public class OpenCVTest { | |
public static void main(String args[]) { | |
//load library | |
System.load("/usr/local/Cellar/opencv/3.3.1_1/share/OpenCV/java/libopencv_java331.dylib"); | |
Mat frame = new Mat(); | |
Mat firstFrame = new Mat(); | |
Mat gray = new Mat(); | |
Mat frameDelta = new Mat(); | |
Mat thresh = new Mat(); | |
List<MatOfPoint> cnts = new ArrayList<MatOfPoint>(); | |
VideoCapture camera = new VideoCapture(); | |
camera.open(0); //open camera | |
//set the video size to 512x288 | |
camera.set(3, 512); | |
camera.set(4, 288); | |
try { | |
Thread.sleep(3000); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
camera.read(frame); | |
//convert to grayscale and set the first frame | |
Imgproc.cvtColor(frame, firstFrame, Imgproc.COLOR_BGR2GRAY); | |
Imgproc.GaussianBlur(firstFrame, firstFrame, new Size(21, 21), 0); | |
while(camera.read(frame)) { | |
//convert to grayscale | |
Imgproc.cvtColor(frame, gray, Imgproc.COLOR_BGR2GRAY); | |
Imgproc.GaussianBlur(gray, gray, new Size(21, 21), 0); | |
//compute difference between first frame and current frame | |
Core.absdiff(firstFrame, gray, frameDelta); | |
Imgproc.threshold(frameDelta, thresh, 25, 255, Imgproc.THRESH_BINARY); | |
Imgproc.dilate(thresh, thresh, new Mat(), new Point(-1, -1), 2); | |
Imgproc.findContours(thresh, cnts, new Mat(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE); | |
for(int i=0; i < cnts.size(); i++) { | |
if(Imgproc.contourArea(cnts.get(i)) < 500) { | |
continue; | |
} | |
System.out.println("Motion detected!!!"); | |
System.exit(0); | |
} | |
} | |
} | |
} |
/* | |
* To compile: g++ opencv_test2.cpp -o opencv_test2 $(pkg-config --cflags --libs opencv) | |
*/ | |
#include <opencv2/opencv.hpp> | |
#include <opencv2/tracking.hpp> | |
#include <opencv2/core/ocl.hpp> | |
#include <unistd.h> | |
using namespace cv; | |
using namespace std; | |
int main(int argc, char **argv) { | |
Mat frame, gray, frameDelta, thresh, firstFrame; | |
vector<vector<Point> > cnts; | |
VideoCapture camera(0); //open camera | |
//set the video size to 512x288 to process faster | |
camera.set(3, 512); | |
camera.set(4, 288); | |
sleep(3); | |
camera.read(frame); | |
//convert to grayscale and set the first frame | |
cvtColor(frame, firstFrame, COLOR_BGR2GRAY); | |
GaussianBlur(firstFrame, firstFrame, Size(21, 21), 0); | |
while(camera.read(frame)) { | |
//convert to grayscale | |
cvtColor(frame, gray, COLOR_BGR2GRAY); | |
GaussianBlur(gray, gray, Size(21, 21), 0); | |
//compute difference between first frame and current frame | |
absdiff(firstFrame, gray, frameDelta); | |
threshold(frameDelta, thresh, 25, 255, THRESH_BINARY); | |
dilate(thresh, thresh, Mat(), Point(-1,-1), 2); | |
findContours(thresh, cnts, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE); | |
for(int i = 0; i< cnts.size(); i++) { | |
if(contourArea(cnts[i]) < 500) { | |
continue; | |
} | |
putText(frame, "Motion Detected", Point(10, 20), FONT_HERSHEY_SIMPLEX, 0.75, Scalar(0,0,255),2); | |
} | |
imshow("Camera", frame); | |
if(waitKey(1) == 27){ | |
//exit if ESC is pressed | |
break; | |
} | |
} | |
return 0; | |
} |
/* | |
* To compile: g++ opencv_test.cpp -o opencv_test $(pkg-config --cflags --libs opencv) | |
*/ | |
#include <opencv2/opencv.hpp> | |
#include <opencv2/tracking.hpp> | |
#include <opencv2/core/ocl.hpp> | |
#include <unistd.h> | |
#define WINDOW_NAME "Camera" | |
using namespace cv; | |
using namespace std; | |
int main(int argc, char **argv) { | |
Ptr<Tracker> objectTracker; | |
Scalar color = Scalar(0, 165, 255); //Orange | |
objectTracker = TrackerKCF::create(); //KCF tracker | |
VideoCapture camera(0); //open camera | |
//set the video size to 640x480 to process faster | |
camera.set(3, 640); | |
camera.set(4, 480); | |
sleep(3); | |
Mat frame; | |
camera.read(frame); | |
Rect2d rect; | |
rect = selectROI(WINDOW_NAME, frame, false); | |
rectangle(frame, rect, color, 1); | |
imshow(WINDOW_NAME, frame); | |
objectTracker->init(frame, rect); | |
while(camera.read(frame)) { | |
if (objectTracker->update(frame, rect)) { | |
rectangle(frame, rect, color, 1); | |
} | |
imshow(WINDOW_NAME, frame); | |
if(waitKey(1) == 27){ | |
//exit if ESC is pressed | |
break; | |
} | |
} | |
return 0; | |
} |