Added coffee script examples based on opencv tutorials and updated blockWaitKey function.

This commit is contained in:
Edgar O Silva 2014-01-07 20:28:06 -06:00
parent 1d9072aefb
commit c7ae7140ea
8 changed files with 127 additions and 0 deletions

View 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)
)
)

View 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 retriebe 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.lof "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 greated 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)

View 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.")
)

View 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.")
)

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 518 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 50 KiB

View File

@ -73,9 +73,14 @@ NamedWindow::BlockingWaitKey(const v8::Arguments& args){
HandleScope scope;
//SETUP_FUNCTION(NamedWindow)
int time = 0;
if (args.Length() > 0){
time = args[1]->IntegerValue();
}else{
time = args[0]->IntegerValue();
}
int res = cv::waitKey(time);
return scope.Close(Number::New(res));
}