Friday, February 24, 2017

Opencv-3.2.0 on Windows 10 - 64 bit Using Visual Studio 2015

Using the latest version OpenCV (currently opencv-3.2.0) pre-built for 64 bit Windows with Visual Studio 2015 is discussed. It is available to download at
https://sourceforge.net/projects/opencvlibrary/files/opencv-win/.
Then, the downloaded file, opencv-3.2.0-vc14.exe is extracted at C:\opencv as a folder called opencv320. Visual Studio 2015 IDE is downloaded from https://www.visualstudio.com/downloads/ and install. The following steps are tested on Windows 10 64 bit using Visual Studio 2015 and OpenCV-3.2.0. For other versions, the precedures are similar.

At first OPENCV_DIR is declared as an enviroment variable. Run 'Command window' in administrator mode by right clicking start menu and choosing command prompt (admin). Enter

setx -m OPENCV_DIR C:\opencv\opencv320\build\x64\vc14


To include bin folder for DLLs in system path, utility such as https://www.rapidee.com/en/download can be used. Alternatively, go to 'this PC(My Computer) > Properties > Advanced System Settings > Advanced Tab > Environment variables > Edit System Variables' and edit Path by entering '%OPENCV_DIR%\bin' in system path.


Figure. Declaring the environment variable


After those installation procedures, let us create an example to use OpenCV. Open Visual Studio 2015 and choose 'New Project'. Choose 'Installed > Visual C++ > General > Empty Project'.


Figure. New Project in Visual Studio 2015


Right click 'Source Files' in Solution Explorer and choose 'Add > New Item...'. Then, in the pop-up window, choose 'Installed > Visual C++ > C++ File (.cpp)' and add a file, in this case, 'main.cpp'. On the Tool bar at the top, choose x64 instead of x86 in Solution Platforms.


Figure. Choosing x64 as platform


Thereafter, go to 'menu bar' and choose 'View > Other Windows > Property Manager'.


Figure. Property Manager


In the rigt pane, Property Manager, right click on Debug|x64 configuration, choose 'Add New Project Property Sheet...', and create a property sheet called 'OpenCV320'. It is reusable in next Projects by choosing 'Add Existing Property Sheet' instead of 'Add New'.


Figure. Adding a Property Sheet


In 'Property Manager', right click 'OpenCV320' and choose 'Properties'. In the pop-up 'Property Pages', go to 'C/C++ > General > Additional Include Directories', enter

$(OPENCV_DIR)\..\..\include


and click Apply.


Figure. Defining 'Include directory'


And under Linker, at 'General > Additional Library Directories', enter

$(OPENCV_DIR)\lib


And then at 'Input > Additional Dependencies' under Linker, enter the names of 'library modules'.


Figure. Library directory


In OpenCV 3.2.0, there are two library modules under 'C:/opencv/opencv320/build/x64/vc14/lib'. They are 'opencv_world320.lib' and 'opencv_world320d.lib'. For Debug mode, use the one ending with 'd' and the other is for Release mode. The number 320 in the module names are for opencv version 3.2.0 and it will change according to version you used. For this example, enter the library name

opencv_world320d.lib





Figure. Defining library name


In Visual Studio 2015, fopen used in OpenCV is deprecated. To suppress it, go to 'C/C++ > Preprocessor > Preprocessor Definitions' and enter

_CRT_SECURE_NO_DEPRECATE





Figure. Disabling the Deprecation of fopen


The following is an example program to test.

#include < stdio.h >
#include < opencv2/opencv.hpp >
using namespace cv;
int main(int argc, char** argv)
{
 Mat image;
 image = imread("C:/opencv/thiri.jpg",0);
 if (!image.data) {
  printf("No image data \n");
  return -1;
 }
 namedWindow("Display Image", WINDOW_AUTOSIZE);
 imshow("Display Image", image);
 
 //Create a circular mask
 int h = image.rows;
 int w = image.cols;
 Mat mask(h, w, CV_8UC1, Scalar(0, 0, 0));
 circle(mask, Point(w / 2, h / 2), h / 3, 255, -1);
 
 //Perform bitwise 'AND' to mask out
 Mat im2;
 bitwise_and(image, mask, im2);
 
 //Manipulate pixels
 for (int i=0; i < h; i++)
  for (int j=0; j < w; j++)
   if (mask.at(i, j) == 0)
    im2.at(i, j) = 128;
 namedWindow("Masked Image", WINDOW_AUTOSIZE);
 imshow("Masked Image", im2);
 
 waitKey(0);
 return 0;
}


An image file called 'thiri.jpg' is placed at 'C:/opencv/thiri.jpg'. In the program, you need to define 'full path'. Forward slash is allowed and using '/' is recommanded. If you want to use 'Backslash', escape character '\\' must be used. In this pragram, the second argument in imread is 0 to read the image in grayscale. Then, Mat is used to create a mask filled with 0. And, circle is used to draw a white circle in the mask. Using bitwise_and makes all pixels 0 except the ones in the white circle. Thereafter, for loop is used to change individual 0 pixels into gray color (128). And, imshow is used to show the result.


Figure. The output of the program.


No comments:

Post a Comment

Comments are moderated and don't be surprised if your comment does not appear promptly.