Here are the steps to compile and run C++ code using OpenCV 3.1.0 and Microsoft Visual Studio 2015.
- Download and install Microsoft Visual Studio 2015.
- Download OpenCV 3.1.0.
- Extract OpenCV to “C:\”. After extracting, you will end up having a folder called “C:\opencv”.
- On Windows:
- In Search, search for and then select: “System” (Control Panel).
- Click on the “Advanced System Settings” link.
- Click Environment Variables.
- In System Variables, click on “Path”.
- Click on “Modify”.
- Click on “New” and add the following (without quotation marks): “C:\opencv\build\x64\vc14\lib\”.
- Click on “New” again and add the following (without quotation marks): “C:\opencv\build\x64\vc14\bin\”.
- Restart Windows.
- Open Visual Studio and create a new project.
- On “Solution Explorer”:
- Right click on the project title and then go to “Properties”.
- Change Platform to “x64”.
- Go to “C/C++” -> “General” -> “Additional Include Directories” and add (without quotation marks) “C:\opencv\build\include”. Click on “Apply”.
- Go to “Linker” -> “General” -> “Additional Library Directories” and add (without quotation marks) “C:\opencv\build\x64\vc14\lib”. Click on “Apply”.
- Go to “Linker” -> “Input” -> “Additional Dependencies” and add (without quotation marks) “opencv_world310.lib;opencv_world310d.lib;”. Click on “Apply”.
- 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!