collapse
I spent about 15 hours or so getting my homework using OpenCV. 14.5 hours were spent setting it up correctly. I watched about 7 tutorial videos, some tuning tips, and read hundreds of messages containing permissions to the same erros I received. Therefore, I understand that simply installing OpenCV is not a trivial task, and there are several steps for this. So here is a simple tutorial on setting up if you want to use openCV.
It is important to understand how everything works, how well it fits together. There are three types of files, your headers that you include, DLLs that contain functions, and libraries that contain instructions on how to call functions in a DLL. Therefore, here, and do not add only DLL files depending on the input linker, we are going to add lib files. Then we create a System Environment variable that tells the machine where to look for DLL files when they are referenced by the corresponding library files. We will create a Property Sheet so that when creating a new project, we can simply add the settings to our project by clicking "Add an existing property sheet" instead of adding a new one. So we no longer have to go through this.
FOLLOW THESE STEPS EXACTLY AND MAKE SURE THE VISUAL STUDIO IS CLOSED BEFORE CONTINUING
NOTE. When the text is enclosed in quotation marks with instructions for copying the specified text, do not include quotation marks.
- First of all, the simple part is to download OpenCV 2.4.5 from your website. http://opencv.org/ and click OpenCV for Windows. It will download OpenCV 2.4.5.exe.
Install OpenCV
After the download is complete, double-click OpenCV-2.4.5.exe to start it.
When you are asked where to extract the files, enter in the text box: "C: \"
C: \ opencv should be created upon completion. Go there to make sure.
Installation Environment Settings
WINDOWS 8 USERS:
- Right click the bottom left corner of your screen when the start icon pops up. - Click "Command Prompt (Admin)" - Type "SETX -m OPENCV_DIR C:\opencv\build" and press enter to set the opencv build directory as a System Environment Variable. Wait for the console to give you confirmation that it is set. - Right click the bottom left corner of your screen when the "Start" icon pops up. Click System -> Advanced System Settings -> Environment Variables - In the "System Variables" list box, under the "Variable" collumn, find "Path". - Highlight the "Path" row and click edit. - Click in the "Variable Value" text box and hit the "end" key on your keyboard to scroll to the end of the line and add a semicolon. - Type the following: "C:\opencv\build\x86\vc10\bin;C:\opencv\build\x86\vc10" and click "OK". This will add the openCV bin directory to the system path.
WINDOWS 7 USERS:
Follow the same steps. The only difference is how you get to the command line and system settings. Google how to set the environment variable in Windows 7, if necessary.
Setting up Visial Studio
NOTE. I highly recommend VS2012 Professional because of its enhanced syntax highlighting, which makes life easier when programming C ++. This version can be downloaded and installed for free for DreamSpark. Just make a student account and keep it in mind. However, the steps for VS2010 and VS2012 are the same.
Open Visual Studio
- Click "New Project", and in the "C ++" section, select "Win32 Console Application".
When the window opens, click "Next", mark "Empty project" and click "Finish". It is very important that you start with an EMPTY PROJECT without a precompiled header.
Find "Property Manager". By default, this should be a tab that is sometimes difficult to miss. Alternatively, you can access it by clicking on the View toolbar β Property Manager.
Right-click Debug | Win32 and select Add New Project Properties Sheet. Name it "OpenCVProps" and click "Add."
Right-click the new property sheet and select Properties.
In the left column, go to "C / C ++" β "General" and in the list on the right, select "Additional inclusion directories" and click "Change."
Add the following three directories:
"$ (OPENCV_DIR) \ include"
"$ (OPENCV_DIR) \ include \ OpenCV"
"$ (OPENCV_DIR) \ include \ opencv2"
In the left column, go to "Linker" β "General", and in the list on the right, select "Additional library directories" and click "Change."
Add the following directory:
- "$ (OPENCV_DIR) \ x86 \ VC10 \ Lib"
In the left column, go to "Linker" β "Input", and in the list on the right, select "Additional Dependenies" and click "Edit".
Add the following .lib files depending. You can do this by copying and pasting them into this edit box. I did not specifically include a marker to make it easier for you to copy this data.
opencv_core245d.lib opencv_imgproc245d.lib opencv_highgui245d.lib opencv_ml245d.lib opencv_video245d.lib opencv_features2d245d.lib opencv_calib3d245d.lib opencv_objdetect245db524 libdv5db524 libdv5db524 libdv5v lib24vdlbbdlb24 libdlbc lib5v lib24vdl5
NOTE. If you are creating for release, these steps are the same. However, when copying and pasting these files, remove the "d" from the end of each of them. "D" means that it is a release library and links to the .dll release.
- Congrats! The hard part is almost ready! Click "OK" to close the window.
Creating and building a test project
Go to our solution explorer. This can be focused from the toolbar via "View" β "Solution Explorer"
Right-click Source Files and choose Add β New Item.
Select "C ++ File (.cpp)" and name the file "main.cpp". Click Add.
Copy and paste the following program and press "F7" on your keyboard and look in the lower left corner of the screen to see if the message "Build after success" has appeared. If so, there is only one step left before compiling and running! If not, repeat the steps or comments below, and maybe I can help.
#include & ltopencv \ cv.h & gt
#include & ltopencv \ highgui.h & gt
int main (int argc, char * argv)
{// openCV .image object
cv :: Mat inputImage;
// Create a Window
cv :: namedWindow ("window", 1);
// Initialize our image.
inputImage = cv :: imread ("Lenna.png");
// Always check to make sure that image has data.
if (inputImage.empty ())
{
std :: cout & lt & lt "Image Failed to Load.";
return -1;
}
else
{
// All is well, display me.
cv :: imshow ("window", inputImage);
// Wait for user to press a key to exit.
cvWaitKey (0);
}
return 0;
}
If the assembly is successful, then all that remains is to add the image to your folder. Accommodation is very important. I copied directoy into which I put mine. Follow the same directory scheme.
- "C: \ Users \ Josh \ Documents \ Visual Studio 2012 \ Projects \ ConsoleApplication3 \ ConsoleApplication3 \ Lenna.png"
Now press "Ctrl + F5". To compile, compile and run to watch your image in the window!
* IF YOU HAVE WEBCAM *
- Copy and paste the following code to check if OpenCV works without the need to add an image. This is useful because if the above code does not work, but this code does, then you know that you have placed the image in the wrong folder.
#include
#include
int main (int argc, char * argv)
{// openCV .image object
cv :: Mat image;
// Create a Window
cv :: namedWindow ("window", 1);
// Create the capture object.
cv :: VideoCapture device;
// Open your webcam.
device.open (0);
while (1)
{
// Read data from your device and store it to the image frame.
device >> image;
// Always check to make sure that image has data.
if (image.empty ())
{
std :: cout & lt & lt "Image Failed to Load.";
return -1;
}
else
{
// All is well, display me.
cv :: imshow ("window", image);
// Wait for user to press a key to exit.
cvWaitKey (33);
}
}
return 0;
}
Happy coding !! Let me know if something didnβt work, so I can fix it!