Capturing Video in OpenCV

Capturing video in OpenCV 4.0.1 is quiet simple. In just about 20 lines code you can capture the video from the camera (webcam) of your Computer and manipulate it as you wish. The code is below.

#include "opencv2/opencv.hpp"

using namespace cv;

int main(int, char**)
	VideoCapture cap(0); // open the default camera
	if (!cap.isOpened())  // check if we succeeded
		return -1;

	for (;;)
		Mat frame;
		cap >> frame; // get a new frame from camera
		flip(frame, frame, +1); //flip image horizontally
		imshow("OPENCV Video", frame);

		if (waitKey(30) >= 0) break;
	// the camera will be deinitialized automatically in VideoCapture destructor
	return 0;

Insert the code above in your IDE, build and run ( I use Visual Studio 2015 ); the result is shown below:

For comparison, I ran the built-in windows camera app. The result is shown below:

From here on you can perform any manipulations you wish. Like this cool color inversion.

Just by adding :

frame= ~frame;

before imshow  function.

Hope this was fun and informative. Don’t stop here, keep experimenting and have fun along the way.

Simple intro to OpenCV

I am presently experimenting with OpenCV (if didn’t know before OpenCV is a Computer Vision Library for Academic and Commercial use). This article is about OpenCV on windows 10 using Visual Studio 2015. I have tried my hand at various building and installation methods and this is the simplest way I have come by.

1. Get the latest release of the OpenCV at the website. URL link: and Download for windows (Win Pack).

2. After Downloading run the file and extract. I extracted to same place I ran the program from.

“opencv” folder gotten from extraction

3. Now you create a system environment variable named “OPENCV_DIR” with value “C:\<the location you extracted to>\opencv\build\x64\vc14″. My example is below.

4. Open your Visual Studio 2015 IDE. Create a new project, a Win32 Console Application (chose whatever name suits you). Ensure Radio Button and Check box are selected as below.

5. Add an Empty new .cpp file to the project. Then, go to View > Other Windows  and select Property Manager.

6. Make sure solution Configuration and Platform is set to Debug and x64 respectively.

7. Right Click on “Debug | x64” in the Property Manager and click on properties. a) Under the ” C/C++ > Additional Include Directories” add
“$(OPENCV_DIR)\..\..\include;” b) Under “Linker >
Additional Library Directories”
add “$(OPENCV_DIR)\ lib;” and c) Add “opencv_world401d.lib” under Linker > Input > Additional Dependencies. Click OK.

Note: The 401 in opencv_world401.lib represents the version number, make sure it match the library files you downloaded.


8. Copy and Paste the code below into your .cpp file.

#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
#include <string>
using namespace cv;
using namespace std;
int main( int argc, char** argv )
    String imageName( "data/vulkan.png" ); // by default
    if( argc > 1)
        imageName = argv[1];
    Mat image;
    image = imread( imageName, IMREAD_COLOR ); // Read the file
    if( image.empty() )                      // Check for invalid input
        cout <<  "Could not open or find the image" << std::endl ;
        return -1;
    namedWindow( "Display window", WINDOW_AUTOSIZE ); // Create a window for display.
    imshow( "Display window", image );                // Show our image inside it.
    waitKey(0); // Wait for a keystroke in the window
    return 0;

9. Before you run the code. Download and save picture at C:\Users\<Your User Name>\Documents\Visual Studio 2015\Projects\Intro OpenCV\Intro OpenCV\data. Link to picture here.

10. Now run and have fun.

Communicating with Arduino using Java

When I got a new 16×2 LCD, one of the projects that came to my mind was sending info to it by serial communication. In this project I will be using java because of the ease of making a GUI (Graphical User Interface).  I set out to get a suitable library for serial communication in java for windows. My search was fruitful as I came across for various choices, from which I eventually selected “jssc (Java Simple Serial Connector)” by Alexey Sokolov. The steps I followed and the results I got are:


  1. Download the jssc library from I used the jSSC-2.7.0-Release.
  2. Unzip your download. You should get the files shown below.

3. Fire up your Netbeans IDE. Start a new Project, I named mine “NetBeans Serial Test”. Right click on project and select properties. A ‘Project Properties’ video pops up, click “Add JAR/Folder” and select jssc.jar you downloaded.

4. After this insert the code below in your main method, compile.

// TODO code application logic here

String lcd_text ="A simple serial App";

SerialPort serialPort = new SerialPort("COM4"); // Input port for Arduino

try {

serialPort.openPort(); //Open port

//set parameters below

serialPort.setParams(SerialPort.BAUDRATE_9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1, SerialPort.PARITY_NONE,false,false);

//code to indicate maximum characters that screen can take

if(lcd_text.length()>32){ lcd_text ="String must be  less than 32"; }

//writing to arduino


//close of port





catch (SerialPortException ex){



5. Note if your Arduino is not plug it will throw an error. Also nothing will happen until you do the next part. Which is programming the Arduino.


  1. Run the Arduino IDE, make sure your Arduino is connected.
  2. Follow the connection below. Adjust potentiometer to change contrast.

3. Copy the code below and upload.

Edited by Lawani Oluwasheun ; 06/Jan/19
LiquidCrystal Library - Serial Input
Demonstrates the use a 16x2 LCD display.  The LiquidCrystal
library works with all LCD displays that are compatible with the
Hitachi HD44780 driver. There are many of them out there, and you
can usually tell them by the 16-pin interface.
This sketch displays text sent over the serial port
(e.g. from the Serial Monitor) on an attached LCD.

The circuit:
* LCD RS pin to digital pin 12
* LCD Enable pin to digital pin 11
* LCD D4 pin to digital pin 5
* LCD D5 pin to digital pin 4
* LCD D6 pin to digital pin 3
* LCD D7 pin to digital pin 2
* LCD R/W pin to ground
* 10K resistor:
* ends to +5V and ground
* wiper to LCD VO pin (pin 3)

Library originally added 18 Apr 2008
by David A. Mellis
library modified 5 Jul 2009
by Limor Fried (
example added 9 Jul 2009
by Tom Igoe
modified 22 Nov 2010
by Tom Igoe
modified 7 Nov 2016
by Arturo Guadalupi
This example code is in the public domain.


// include the library code:
#include <LiquidCrystal.h>

// initialize the library by associating any needed LCD interface pin

// with the arduino pin number it is connected to

const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;

LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

//character counter

int char_count=0;

void setup() {

// set up the LCD's number of columns and rows:

lcd.begin(16, 2);

// initialize the serial communications:




void loop() {

// when characters arrive over the serial port...

if (Serial.available()) {

// wait a bit for the entire message to arrive


// clear the screen


// read all the available characters

while (Serial.available() > 0) {

// display each character to the LCD


//increase character count



lcd.setCursor(0,1); //move to next line

//reset character counter




//reset character counter


// lcd.print(char_count); **debug




  1. After completing the Arduino part run the Java program. The result is as seen below.