mirror of
https://github.com/peterbraden/node-opencv.git
synced 2025-12-08 19:45:55 +00:00
Merge pull request #98 from hybridgroup/master
Added coffee script examplesand updated HighGUI blockingWaitKey.
This commit is contained in:
commit
e3b20082ab
31
examples/coffeescript/blend-images.coffee
Normal file
31
examples/coffeescript/blend-images.coffee
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
cv = require 'opencv'
|
||||||
|
|
||||||
|
# we create a function helper to display the images.
|
||||||
|
displayImage = (windowName, img) ->
|
||||||
|
namedWindow = new cv.NamedWindow(windowName)
|
||||||
|
namedWindow.show(img)
|
||||||
|
namedWindow.blockingWaitKey(0, 5000)
|
||||||
|
|
||||||
|
# Load both sources to be blended
|
||||||
|
cv.readImage("./images/windows-logo.png", (err, src1) ->
|
||||||
|
|
||||||
|
# Display Source 1
|
||||||
|
displayImage('Source 1', src1)
|
||||||
|
|
||||||
|
# Load source #2
|
||||||
|
cv.readImage("./images/linux-logo.png", (err, src2) ->
|
||||||
|
|
||||||
|
# Display Source 2
|
||||||
|
displayImage('Source 2', src2)
|
||||||
|
|
||||||
|
# We use the addWeighted opencv wrapper to blend the images.
|
||||||
|
# we need to use a different image object, since src1 and src2
|
||||||
|
# will be manipulated.
|
||||||
|
result = new cv.Matrix(src1.width(), src2.height())
|
||||||
|
result.addWeighted(src1, 0.7, src2, 0.9)
|
||||||
|
result.save("./tmp/blended.png")
|
||||||
|
|
||||||
|
# Display Blended result 2
|
||||||
|
displayImage('Blended', result)
|
||||||
|
)
|
||||||
|
)
|
||||||
56
examples/coffeescript/camera-face-detect-max-frames.coffee
Normal file
56
examples/coffeescript/camera-face-detect-max-frames.coffee
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
cv = require 'opencv'
|
||||||
|
|
||||||
|
# First we create a new VideoCapture object to get
|
||||||
|
# video from the camera (0 for default camera)
|
||||||
|
camera = new cv.VideoCapture(1)
|
||||||
|
|
||||||
|
# we create a window to display the Video frames
|
||||||
|
namedWindow = new cv.NamedWindow('Video', 0)
|
||||||
|
color = [255,0,0]
|
||||||
|
|
||||||
|
frameRead = () ->
|
||||||
|
# camera.read allows us to retrieve the current
|
||||||
|
# frame to be displayed in the video window.
|
||||||
|
camera.read((err, im) ->
|
||||||
|
# We can check for errors and even break the
|
||||||
|
# program execution if an error is detected here.
|
||||||
|
console.log "The err ==>#{ err }" if err
|
||||||
|
|
||||||
|
# There is no need to display the image width or Height
|
||||||
|
# but I leave this here in case anyone needs to check them.
|
||||||
|
#console.log("Width: #{im.width()}")
|
||||||
|
#console.log("height: #{im.height()}")
|
||||||
|
|
||||||
|
# Before working with the frame we need to check the image
|
||||||
|
# is already available and has a width and height greater than 0,
|
||||||
|
# otherwise it will fail when trying to do namedWindow.show()
|
||||||
|
# and the image has width or height equal or less than 0.
|
||||||
|
if im.width() > 0 and im.height() > 0
|
||||||
|
#console.log("Interval ID => #{ intervalId }")
|
||||||
|
#console.log(intervalId)
|
||||||
|
clearInterval(intervalId) if intervalId?
|
||||||
|
im.detectObject('./haarcascades/haarcascade_frontalface_alt.xml', {}, (err, faces) ->
|
||||||
|
for face in faces
|
||||||
|
im.rectangle([face.x, face.y], [face.x + face.width, face.y + face.height], [0,255,0], 2)
|
||||||
|
|
||||||
|
# We use the previously created namedWindow to display the
|
||||||
|
# video frame to wich we applied the blur and filter.
|
||||||
|
namedWindow.show(im)
|
||||||
|
|
||||||
|
# Finally we get the key pressed on the window to terminate
|
||||||
|
# execution of the program.
|
||||||
|
res = namedWindow.blockingWaitKey(0, 20)
|
||||||
|
|
||||||
|
# In this case we terminate the program if any key is pressed.
|
||||||
|
#if res >= 0 then do not set a new timeout to get a new frame.
|
||||||
|
setTimeout(frameRead, 5) if res < 0
|
||||||
|
)
|
||||||
|
|
||||||
|
)
|
||||||
|
|
||||||
|
# We set an interval to retrieve frames from the
|
||||||
|
# video source and we get the intervalId so we can
|
||||||
|
# stop the program by pressing any key on the video feed window.
|
||||||
|
intervalId = setInterval(() ->
|
||||||
|
frameRead(intervalId)
|
||||||
|
, 100)
|
||||||
49
examples/coffeescript/camera-face-detection.coffee
Normal file
49
examples/coffeescript/camera-face-detection.coffee
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
cv = require 'opencv'
|
||||||
|
|
||||||
|
# First we create a new VideoCapture object to get
|
||||||
|
# video from the camera (0 for default camera)
|
||||||
|
camera = new cv.VideoCapture(1)
|
||||||
|
|
||||||
|
# we create a window to display the Video frames
|
||||||
|
namedWindow = new cv.NamedWindow('Video',1)
|
||||||
|
color = [255,0,0]
|
||||||
|
|
||||||
|
# We set an interval to retrieve frames from the
|
||||||
|
# video source and we get the intervalId so we can
|
||||||
|
# stop the program from the video feed window.
|
||||||
|
intervalId = setInterval(()->
|
||||||
|
|
||||||
|
# camera.read allows us to retrieve the current
|
||||||
|
# frame to be displayed in the video window.
|
||||||
|
camera.read((err, im) ->
|
||||||
|
# We can check for errors and even break the
|
||||||
|
# program execution if an error is detected here.
|
||||||
|
console.log "The err ==>#{ err }" if err
|
||||||
|
|
||||||
|
# There is no need to display the image width or Height
|
||||||
|
# but I leave this here in case anyone needs to check them.
|
||||||
|
#console.log("Width: #{im.width()}")
|
||||||
|
#console.log("height: #{im.height()}")
|
||||||
|
|
||||||
|
# Before working with the frame we need to check the image
|
||||||
|
# is already available and has a width and height greater than 0,
|
||||||
|
# otherwise it will fail when trying to do namedWindow.show()
|
||||||
|
# and the image has width or height equal or less than 0.
|
||||||
|
if im.width() > 0 and im.height() > 0
|
||||||
|
im.detectObject('./haarcascades/haarcascade_frontalface_alt.xml', {}, (err, faces) ->
|
||||||
|
for face in faces
|
||||||
|
im.rectangle([face.x, face.y], [face.x + face.width, face.y + face.height], [0,255,0], 2)
|
||||||
|
|
||||||
|
# We use the previously created namedWindow to display the
|
||||||
|
# video frame to wich we applied the blur and filter.
|
||||||
|
namedWindow.show(im)
|
||||||
|
)
|
||||||
|
|
||||||
|
# Finally we get the key pressed on the window to terminate
|
||||||
|
# execution of the program.
|
||||||
|
res = namedWindow.blockingWaitKey(0, 50)
|
||||||
|
|
||||||
|
# In this case I terminate the program if any key is pressed.
|
||||||
|
if res >= 0 then clearInterval(intervalId)
|
||||||
|
)
|
||||||
|
, 150)
|
||||||
58
examples/coffeescript/camera-gausian-effect.coffee
Normal file
58
examples/coffeescript/camera-gausian-effect.coffee
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
cv = require 'opencv'
|
||||||
|
|
||||||
|
# First we create a new VideoCapture object to get
|
||||||
|
# video from the camera (0 for default camera)
|
||||||
|
camera = new cv.VideoCapture(1)
|
||||||
|
|
||||||
|
# we create a window to display the Video frames
|
||||||
|
namedWindow = new cv.NamedWindow('Video',1)
|
||||||
|
|
||||||
|
# We set an interval to retrieve frames from the
|
||||||
|
# video source and we get the intervalId so we can
|
||||||
|
# stop the program from the video feed window.
|
||||||
|
intervalId = setInterval(()->
|
||||||
|
|
||||||
|
# camera.read allows us to retrieve the current
|
||||||
|
# frame to be displayed in the video window.
|
||||||
|
camera.read((err, im) ->
|
||||||
|
# We can check for errors and even break the
|
||||||
|
# program execution if an error is detected here.
|
||||||
|
console.log "The err ==>#{ err }" if err
|
||||||
|
|
||||||
|
# There is no need to display the image width or Height
|
||||||
|
# but I leave this here in case anyone needs to check them.
|
||||||
|
#console.log("Width: #{im.width()}")
|
||||||
|
#console.log("height: #{im.height()}")
|
||||||
|
|
||||||
|
# Before working with the frame we need to check the image
|
||||||
|
# is already available and has a width and height greater than 0,
|
||||||
|
# otherwise it will fail when trying to do namedWindow.show()
|
||||||
|
# and the image has width or height equal or less than 0.
|
||||||
|
if im.width() > 0 and im.height() > 0
|
||||||
|
# We apply filters and effects to the frame we got from the
|
||||||
|
# camera to manipulate the video that we'll display.
|
||||||
|
# First we convert to grayscale.
|
||||||
|
im.convertGrayscale()
|
||||||
|
# We then apply GaussianBlur.
|
||||||
|
# It takes an array of type double and size 2
|
||||||
|
# that indicates how strong the gaussian blur should be.
|
||||||
|
im.gaussianBlur([7,7])
|
||||||
|
# We then apply canny edge filtering.
|
||||||
|
# Params are lower and higher threshold and aperture size,
|
||||||
|
# although nodejs-opencv overwrite the aperture size param
|
||||||
|
# with 0. An update to consider it is needed.
|
||||||
|
im.canny(0, 30, 3)
|
||||||
|
|
||||||
|
# We use the previously created namedWindow to display the
|
||||||
|
# video frame to wich we applied the blur and filter.
|
||||||
|
namedWindow.show(im)
|
||||||
|
|
||||||
|
# Finally we get the key pressed on the window to terminate
|
||||||
|
# execution of the program.
|
||||||
|
res = namedWindow.blockingWaitKey(0, 20)
|
||||||
|
console.log("KEYPRESSED => #{ res } ")
|
||||||
|
|
||||||
|
# In this case I terminate the program if any key is pressed.
|
||||||
|
if res >= 0 then clearInterval(intervalId)
|
||||||
|
)
|
||||||
|
, 50)
|
||||||
57
examples/coffeescript/camera-gaussian-effect.coffee
Normal file
57
examples/coffeescript/camera-gaussian-effect.coffee
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
cv = require 'opencv'
|
||||||
|
|
||||||
|
# First we create a new VideoCapture object to get
|
||||||
|
# video from the camera (0 for default camera)
|
||||||
|
camera = new cv.VideoCapture(1)
|
||||||
|
|
||||||
|
# we create a window to display the Video frames
|
||||||
|
namedWindow = new cv.NamedWindow('Video',1)
|
||||||
|
|
||||||
|
# We set an interval to retrieve frames from the
|
||||||
|
# video source and we get the intervalId so we can
|
||||||
|
# stop the program from the video feed window.
|
||||||
|
intervalId = setInterval(()->
|
||||||
|
|
||||||
|
# camera.read allows us to retrieve the current
|
||||||
|
# frame to be displayed in the video window.
|
||||||
|
camera.read((err, im) ->
|
||||||
|
# We can check for errors and even break the
|
||||||
|
# program execution if an error is detected here.
|
||||||
|
console.log "The err ==>#{ err }" if err
|
||||||
|
|
||||||
|
# There is no need to display the image width or Height
|
||||||
|
# but I leave this here in case anyone needs to check them.
|
||||||
|
#console.log("Width: #{im.width()}")
|
||||||
|
#console.log("height: #{im.height()}")
|
||||||
|
|
||||||
|
# Before working with the frame we need to check the image
|
||||||
|
# is already available and has a width and height greater than 0,
|
||||||
|
# otherwise it will fail when trying to do namedWindow.show()
|
||||||
|
# and the image has width or height equal or less than 0.
|
||||||
|
if im.width() > 0 and im.height() > 0
|
||||||
|
# We apply filters and effects to the frame we got from the
|
||||||
|
# camera to manipulate the video that we'll display.
|
||||||
|
# First we convert to grayscale.
|
||||||
|
im.convertGrayscale()
|
||||||
|
# We then apply GaussianBlur.
|
||||||
|
# It takes an array of type double and size 2
|
||||||
|
# that indicates how strong the gaussian blur should be.
|
||||||
|
im.gaussianBlur([7,7])
|
||||||
|
# We then apply canny edge filtering.
|
||||||
|
# Params are lower and higher threshold and aperture size,
|
||||||
|
# although nodejs-opencv overwrite the aperture size param
|
||||||
|
# with 0. An update to consider it is needed.
|
||||||
|
im.canny(0, 30, 3)
|
||||||
|
|
||||||
|
# We use the previously created namedWindow to display the
|
||||||
|
# video frame to wich we applied the blur and filter.
|
||||||
|
namedWindow.show(im)
|
||||||
|
|
||||||
|
# Finally we get the key pressed on the window to terminate
|
||||||
|
# execution of the program.
|
||||||
|
res = namedWindow.blockingWaitKey(0, 20)
|
||||||
|
|
||||||
|
# In this case I terminate the program if any key is pressed.
|
||||||
|
if res >= 0 then clearInterval(intervalId)
|
||||||
|
)
|
||||||
|
, 50)
|
||||||
41
examples/coffeescript/camera.coffee
Normal file
41
examples/coffeescript/camera.coffee
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
cv = require 'opencv'
|
||||||
|
|
||||||
|
# First we create a new VideoCapture object to get
|
||||||
|
# video from the camera (0 for default camera)
|
||||||
|
camera = new cv.VideoCapture(1)
|
||||||
|
|
||||||
|
# we create a window to display the Video frames
|
||||||
|
namedWindow = new cv.NamedWindow('Video',1)
|
||||||
|
|
||||||
|
# We set an interval to retrieve frames from the
|
||||||
|
# video source and we get the intervalId so we can
|
||||||
|
# stop the program from the video feed window.
|
||||||
|
intervalId = setInterval(()->
|
||||||
|
|
||||||
|
# camera.read allows us to retrieve the current
|
||||||
|
# frame to be displayed in the video window.
|
||||||
|
camera.read((err, im) ->
|
||||||
|
# We can check for errors and even break the
|
||||||
|
# program execution if an error is detected here.
|
||||||
|
console.log "The err ==>#{ err }" if err
|
||||||
|
|
||||||
|
# There is no need to display the image width or Height
|
||||||
|
# but I leave this here in case anyone needs to check them.
|
||||||
|
#console.log("Width: #{im.width()}")
|
||||||
|
#console.log("height: #{im.height()}")
|
||||||
|
|
||||||
|
# We use the previously created namedWindow to display the
|
||||||
|
# video feed frame, we need to check the image is already
|
||||||
|
# available and has a width and height greater than 0,
|
||||||
|
# otherwise namedWindow.show() will fail if any of those is 0.
|
||||||
|
namedWindow.show(im) if im.width() > 0 and im.height() > 0
|
||||||
|
|
||||||
|
# Finally we get the key pressed on the window to terminate
|
||||||
|
# execution of the program.
|
||||||
|
res = namedWindow.blockingWaitKey(0, 20)
|
||||||
|
console.log("KEYPRESSED => #{ res } ")
|
||||||
|
|
||||||
|
# In this case I terminate the program if any key is pressed.
|
||||||
|
if res >= 0 then clearInterval(intervalId)
|
||||||
|
)
|
||||||
|
, 500)
|
||||||
26161
examples/coffeescript/haarcascades/haarcascade_frontalface_alt.xml
Normal file
26161
examples/coffeescript/haarcascades/haarcascade_frontalface_alt.xml
Normal file
File diff suppressed because it is too large
Load Diff
29
examples/coffeescript/image-basic-shapes.coffee
Normal file
29
examples/coffeescript/image-basic-shapes.coffee
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
cv = require('opencv')
|
||||||
|
|
||||||
|
|
||||||
|
cv.readImage("./images/mona.png", (err, im) ->
|
||||||
|
# Create new NamedWindow object to hold the image
|
||||||
|
# NamedWindow takes two arguments String WindowName and String windowSize
|
||||||
|
namedWindow = new cv.NamedWindow('Display Window', '400x400')
|
||||||
|
|
||||||
|
# We add a couple of basic shapes to the image, just to show how
|
||||||
|
# to create basic shapes.
|
||||||
|
# im.rectangle([startX, StartY], [width, height], color(array of RGB)
|
||||||
|
im.rectangle([50, 50], [200, 200], [0, 255, 0], 2)
|
||||||
|
# im.ellipse(centerX, centerY, width, height, color(array of RGB)
|
||||||
|
im.ellipse(150, 150, 50, 50, [255, 255, 0], 2)
|
||||||
|
|
||||||
|
# We then tell the image to show the image we loaded.
|
||||||
|
namedWindow.show(im)
|
||||||
|
|
||||||
|
console.log("Image should be displayed inside a window.")
|
||||||
|
# Finally we tell the NamedWindow to wait for any key being pressed to close
|
||||||
|
# itself (by passing a 0 as the first param, or wait a defined amount of time
|
||||||
|
# by passing the time as a second argument (in milliseconds)
|
||||||
|
#
|
||||||
|
# If we do not tell the window to wait it will just load and show the image
|
||||||
|
# and close so fast that it will appear nothing happened.
|
||||||
|
namedWindow.blockingWaitKey(0, 5000)
|
||||||
|
|
||||||
|
console.log("And the window should close automatically or by pressing any key on it.")
|
||||||
|
)
|
||||||
22
examples/coffeescript/image-load-display.coffee
Normal file
22
examples/coffeescript/image-load-display.coffee
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
cv = require('opencv')
|
||||||
|
|
||||||
|
|
||||||
|
cv.readImage("./images/mona.png", (err, im) ->
|
||||||
|
# Create new NamedWindow object to hold the image
|
||||||
|
# NamedWindow takes two arguments String WindowName and String windowSize
|
||||||
|
namedWindow = new cv.NamedWindow('Display Window', '400x400')
|
||||||
|
|
||||||
|
# We then tell the image to show the image we loaded.
|
||||||
|
namedWindow.show(im)
|
||||||
|
|
||||||
|
console.log("Image should be displayed inside a window.")
|
||||||
|
# Finally we tell the NamedWindow to wait for any key being pressed to close
|
||||||
|
# itself (by passing a 0 as the first param, or wait a defined amount of time
|
||||||
|
# by passing the time as a second argument (in milliseconds)
|
||||||
|
#
|
||||||
|
# If we do not tell the window to wait it will just load and show the image
|
||||||
|
# and close so fast that it will appear nothing happened.
|
||||||
|
namedWindow.blockingWaitKey(0, 5000)
|
||||||
|
|
||||||
|
console.log("And the window should close automatically or by pressing any key on it.")
|
||||||
|
)
|
||||||
28
examples/coffeescript/image-load-modify-display.coffee
Normal file
28
examples/coffeescript/image-load-modify-display.coffee
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
cv = require('opencv')
|
||||||
|
|
||||||
|
|
||||||
|
cv.readImage("./images/windows-logo.png", (err, im) ->
|
||||||
|
|
||||||
|
# The nodejs-opencv has some shortcodes for image manipulation.
|
||||||
|
# We modify the image using the handy function im.convertGrayscale()
|
||||||
|
# This will overwrite the current image with a grayscaled version.
|
||||||
|
im.convertGrayscale()
|
||||||
|
|
||||||
|
# Create new NamedWindow object to hold the image
|
||||||
|
# NamedWindow takes two arguments String WindowName and String windowSize
|
||||||
|
namedWindow = new cv.NamedWindow('Display Window', '400x400')
|
||||||
|
|
||||||
|
# We then tell the image to show the image we loaded.
|
||||||
|
namedWindow.show(im)
|
||||||
|
|
||||||
|
console.log("Image should be displayed inside a window.")
|
||||||
|
# Finally we tell the NamedWindow to wait for any key being pressed to close
|
||||||
|
# itself (by passing a 0 as the first param, or wait a defined amount of time
|
||||||
|
# by passing the time as a second argument (in milliseconds)
|
||||||
|
#
|
||||||
|
# If we do not tell the window to wait it will just load and show the image
|
||||||
|
# and close so fast that it will appear nothing happened.
|
||||||
|
namedWindow.blockingWaitKey(0, 5000)
|
||||||
|
|
||||||
|
console.log("And the window should close automatically or by pressing any key on it.")
|
||||||
|
)
|
||||||
BIN
examples/coffeescript/images/linux-logo.png
Normal file
BIN
examples/coffeescript/images/linux-logo.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 17 KiB |
BIN
examples/coffeescript/images/mona.png
Executable file
BIN
examples/coffeescript/images/mona.png
Executable file
Binary file not shown.
|
After Width: | Height: | Size: 518 KiB |
BIN
examples/coffeescript/images/windows-logo.png
Normal file
BIN
examples/coffeescript/images/windows-logo.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 50 KiB |
@ -73,9 +73,16 @@ NamedWindow::BlockingWaitKey(const v8::Arguments& args){
|
|||||||
HandleScope scope;
|
HandleScope scope;
|
||||||
//SETUP_FUNCTION(NamedWindow)
|
//SETUP_FUNCTION(NamedWindow)
|
||||||
int time = 0;
|
int time = 0;
|
||||||
if (args.Length() > 0){
|
|
||||||
|
if (args.Length() > 1){
|
||||||
time = args[1]->IntegerValue();
|
time = args[1]->IntegerValue();
|
||||||
|
}else{
|
||||||
|
if (args.Length() > 0){
|
||||||
|
time = args[0]->IntegerValue();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
int res = cv::waitKey(time);
|
int res = cv::waitKey(time);
|
||||||
|
|
||||||
return scope.Close(Number::New(res));
|
return scope.Close(Number::New(res));
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user