Here are the steps to compile and run C++ code using OpenCV 3.1.0 and Microsoft Visual Studio 2015.

  1. Download and install Microsoft Visual Studio 2015.
  2. Download OpenCV 3.1.0.
  3. Extract OpenCV to “C:\”. After extracting, you will end up having a folder called “C:\opencv”.
  4. On Windows:
    1. In Search, search for and then select: “System” (Control Panel).
    2. Click on the “Advanced System Settings” link.
    3. Click Environment Variables.
    4. In System Variables, click on “Path”.
    5. Click on “Modify”.
    6. Click on “New” and add the following (without quotation marks): “C:\opencv\build\x64\vc14\lib\”.
    7. Click on “New” again and add the following (without quotation marks): “C:\opencv\build\x64\vc14\bin\”.
  5. Restart Windows.
  6. Open Visual Studio and create a new project.
  7. On “Solution Explorer”:
    1. Right click on the project title and then go to “Properties”.
    2. Change Platform to “x64”.
    3. Go to “C/C++” -> “General” -> “Additional Include Directories” and add (without quotation marks) “C:\opencv\build\include”. Click on “Apply”.
    4. Go to “Linker” -> “General” -> “Additional Library Directories” and add (without quotation marks) “C:\opencv\build\x64\vc14\lib”. Click on “Apply”.
    5. Go to “Linker” -> “Input” -> “Additional Dependencies” and add (without quotation marks) “opencv_world310.lib;opencv_world310d.lib;”. Click on “Apply”.
  8. Copy the following code to your “.cpp” file.
// My first OpenCV code
// Source: cgcvtutorials.wordpress.com

#include <stdafx.h>
#include <opencv2/opencv.hpp>
#include <iostream>
#include <string>

using namespace std;
using namespace cv;

int main() {
    String text = "cgcvtutorials.wordpress.com";
    int fontFace = FONT_HERSHEY_SCRIPT_SIMPLEX;
    double fontScale = 2;
    int thickness = 3;

    Mat img(600, 800, CV_8UC3, Scalar::all(0));

    int baseline = 0;
    Size textSize = getTextSize(text, fontFace,
        fontScale, thickness, &baseline);
    baseline += thickness;

    // center the text
    Point textOrg((img.cols - textSize.width) / 2,
        (img.rows + textSize.height) / 2);

    // draw the box
    rectangle(img, textOrg + Point(0, baseline),
        textOrg + Point(textSize.width, -textSize.height),
        Scalar(0, 0, 255));
    // ... and the baseline first
    line(img, textOrg + Point(0, thickness),
        textOrg + Point(textSize.width, thickness),
        Scalar(0, 0, 255));

    // then put the text itself
    putText(img, text, textOrg, fontFace, fontScale,
        Scalar::all(255), thickness, 8);

    imshow("img", img);
    waitKey();

    return 0;
}

9. Have fun!