Saturday, July 19, 2014

OpenCV: IplImage, cvMat, CV::Mat

IplImage

From the beginning of OpenCV, which had started off using Intel Image Processing Library from 2001, this class primarily revolves around C. Compared to cvMat and cv::Mat, it has no memory allocation services, a common attribute to C programs. Many past reference material and tutorials will use this class. This class was relevant in OpenCV 1.x.

cvMat

cvMat is also a class also in OpenCV 1.x which answered problems as stated in IplImage as a matrix class.

cv::mat

cv::mat is a more object oriented, C++, version of cvMat, which is also a matrix class for image processing.

Why Post this?

The reason I brought this up was reading the second chapter in O'Reilly Learning OpenCV by Gary Bradski and Adrian Kaehler, recommended to me by an advisor/professor, uses IplImage, which differs from the online tutorials OpenCV offers for basic image processing. Online, the tutorial uses Mat as the object and imread to intake the image file while IplImage is singularly used as an object and to take in the image. Even so, it seems that the book chose to use IplImage because the book is based on OpenCV version 2.0. This may be because IplImage is most simple to start out with or it was still a habit of the 1.x versions.

Tuesday, July 15, 2014

OpenCV: Trouble in First OpenCV Code

Understanding Arguments in OpenCV

One long problem figuring out for the first time in OpenCV that I was not aware of in the visual studios tutorial is in the following code from the same link:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main( int argc, char** argv )
{
    if( argc != 2)
    {
     cout <<" Usage: display_image ImageToLoadAndDisplay" << endl;
     return -1;
    }

    Mat image;
    image = imread(argv[1], CV_LOAD_IMAGE_COLOR);   // Read the file

    if(! image.data )                              // 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;
}
On line 8, notice that the main takes in parameters, meaning this int main needs arguments, which I suspect mean argc = argumentCount, argv = argumentValue, and therefore need the image file name input. This can be done in command line or going to the properties of your project → Configuration Properties → Debugging → Command Arguments and inputting the name of the image file.

Useful Debugging Tool: Image Watch

For reference sake, even though the extension is mentioned in the opencv tutorials, Image Watch is the name of the extension that allows viewing the images analyzed.

Running Multiple Projects in Visual Studio

Just to make my life easier (or not), I'm running all my OpenCV projects under one solution in Microsoft Visual Studios 12 2013. The way to choose which projects you are going to run with or without debugger is to right click the solution → Set StartUp Projects and choose which project(s) you want to run.

OpenCV: Downloading OpenCV

StartOpenCV.html

Getting OpenCV onto Windows 7 and Visual Studios 2013

Downloading OpenCV


To begin, I downloaded the OpenCV package from the OpenCV download page . This package includes everything you need to use unless you had plans for unique third party libraries in an .exe file for windows. Generally, you follow the steps listed in the tutorial Installation to Windows which indicate that you just need to download the file, move the file to a drive (like C:\) directly for typing purposes later, and link the path to your respective file. Mine was C:\OpenCV\Build\x86\vc12. To clarify, I used an x86 build because this is the version of Visual Studios I had, not the version of Windows OS. If you want, for some reason, to change your Visual Studios type, here is the link. In Windows 7 command line, a confirmation response "SUCCESS: Specified value was saved" was displayed.

Working with Microsoft Visual Studios 12 2013 and OpenCV 2.8X

Once downloaded, you can follow the next tutorial How to build applications with OpenCV inside the Microsoft Visual Studio for further instructions. One part I had done differently was that under both debug and release property pages, in Linker → General, I had to also change "Use Library Dependency Inputs" from No to Yes thanks to Stack Overflow . This is all you need to do for OpenCV version 2.8X.

Working with Microsoft Visual Studios 12 2013 and OpenCV 3.0X OpenCV 3.00

Here is the OpenCV 3.0 dev documentation. I will say explicitly that this is a windows 7 64 bit machine and I downloaded the OpenCV 3.0 file updated 2015-06-04. Problems and methods that I used here may not apply to your own problems. I highly suggest googling help links in stackoverflow for opencv. There are additional steps are required to download OpenCV 3.0 compared to the steps above.

Firstly, the files that were in the lib folder in OpenCV 2.8X are now in the static library folder (staticlib). Thanks to MadeofAir on the OpenCV forums for answering the question, you must link not to lib, but to staticlib folder. Additionally, the linked libraries are different. Instead of the list mentioned in this linked tutorial , using the following list:
IlmImf.lib
ippicvmt.lib
libjasper.lib
libjpeg.lib
libpng.lib
libtiff.lib
libwebp.lib
opencv_calib3d300.lib
opencv_core300.lib
opencv_features2d300.lib
opencv_flann300.lib
opencv_hal300.lib
opencv_highgui300.lib
opencv_imgcodecs300.lib
opencv_imgproc300.lib
opencv_ml300.lib
opencv_objdetect300.lib
opencv_photo300.lib
opencv_shape300.lib
opencv_stitching300.lib
opencv_superres300.lib
opencv_video300.lib
opencv_videoio300.lib
opencv_videostab300.lib
zlib.lib
    
For the Debug Project Sheets, after the version number, there is a letter 'd' for debug. Note that ippicvmt.lib does not have a "ippicvmtd.lib" file. In the debug file, simply use the "ippicvmt.lib" name file. This solved my LNK2019 LNK2001 LNK1120 errors.

Secondly, thanks to yzt, user1043413, and other links or forum articles, you should quickly realize that following the tutorials in the official OpenCV website is currently incorrect because the libraries are set up to be static libraries instead of dynamic libraries in the previous verison. This should solve your LNK2038 errors with "mismatch detected for 'RuntimeLibrary':value 'MTd_StaticDebug' doesn't match value 'MD_DynamicRelease' in Run.obj" and associated issues.

For me this was the last of my errors, and I was able to successfully build the example code in the tutorials. If your compile does not work, I saw common issues were people using the wrong bit and not including all the libraries. If you do want to have a dynamic linked library (DLL), then you need to follow the Installation by Making Your Own Libraries from the Source Files processes. Since I was able to download the pre-made libraries in sourceforge correctly, I did not go through the process of downloading the custom libraries.

Working with Microsoft Visual Studios 14 (2015) and OpenCV 3.01 with CMake

At this time, February 12, 2016, downloading OpenCV through the usual .exe file from the OpenCV website seemed to have some unanswerable problems. Instead, I tried downloading through using CMake, which makes the OpenCV package from the source files. My information largely comes from this stackoverflow question answered by globalex and Vlada Kucera , the steps are largely listed.
1. Download CMake
2. Download Source Code of OpenCV and extract the zip file (results in a folder ".../opencv-master"
3. Open CMake, choose the opencv-master file as your souce code and underneath, choose where you want to build the binaries (usually in C:/OpenCV")
4. Configure CMake by choosing the compiler for you (in this tutorial, Visual Studio 14 2015) with "Use default native compilers" checked. When you hit finish, CMake will generate the binaries (otherwise hit generate). You should be online for CMake to download some library files.
5. In the newly generated binary files, there is a file called "OpenCV.sln." Open the file using Visual Studios 2015.
6. First, in the toolbar, hit Local Windows Debugger with Debug and Win32 in the boxes next to the Debugger icon. VS should take a while to run through all the OpenCV files in your binaries.
7. After, click the drop down menu for Debug, and select Release. Run the Local Windows Debugger again.
8. Next, go to project INSTALL under folder "CMakeTargets" in VS and Build/Local Windows Debugger when the build is in Release. After, VS will ask you to rebuild ALL_BUILD again. I chose to rebuild all, though I have no knowledge if the rebuild option is any better or worse.
9. Following closely with what the OpenCV tutorial, I set the environmental variable in command line with administrator purposes "setx -m OPENCV_DIR C:\OpenCV\Install\x86\vc14" and set in my system PATH "%OPENCV_DIR%\bin" for the library files to find the Dynamica-Linked Libraries (DLL). Follow the docs.opencv.org tutorials to change the appropriate sections in the Property Sheets. The files in the linked library are:
opencv_calib3d310.lib
opencv_core310.lib
opencv_features2d310.lib
opencv_flann310.lib
opencv_highgui310.lib
opencv_imgcodecs310.lib
opencv_imgproc310.lib
opencv_ml310.lib
opencv_objdetect310.lib
opencv_photo310.lib
opencv_shape310.lib
opencv_stitching310.lib
opencv_superres310.lib
opencv_ts310.lib
opencv_video310.lib
opencv_videoio310.lib
opencv_videostab310.lib
    


Issues:
Step 7:
If you get error like "Unable to start program C:\OpenCV\ALL_BUILD Cannot find the specified file," according to this question on answers.opencv.org, answered by Cristina, "ALL_BUILD is not a program, it's just a target that perform build of all other targets." In your Solution Explorer window/pane, right click on a project you want to start (in the forum, opencv_test_core was the example), right click the project, and select "Set as StartUp Project."
Also, if you ever get a "The program can't start because opencv_core310.dll is missing," it is because you did not set the PATH (%OPENCV_DIR%\bin) correctly, and VS cannot find the .dll files because it has no way of knowing where they are (Thanks to a stackoverflow answer by udith).
Step 9
If you made a project before you changed the system variables, according to a multitude of forums (1, 2, 3, 4), VS does heavy caching and the project most likely has the value of your environmental variable incorrect (at C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\...). I did not bother to find where the environmental variables were stored and manually change it, and instead deleted and recreated the project since it I had done nothing significant with it.

What's Next

I plan to go through the OpenCV Tutorials and post any small tutorial projects.Learning OpenCV code is tandem to my project of robot vision in autonomous vehicles. Executing the sample code listed above will be on the next blog post here.