вторник, 25 октября 2011 г.

Вывод изображения на экран OpenCV 2.3

Самый простой вывод изображения на экран:
#include <stdio.h>
#include <cv.h>
#include <highgui.h>
#include "opencv2/core/core_c.h"
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
using namespace cv;
 
int main()
{
 namedWindow("hello", CV_WINDOW_AUTOSIZE);
 Mat im = imread("C:\\1.bmp",1);
 if(im.empty())
 {
  return -1;
 }
 
    imshow("display", im);
 waitKey(0);        
 
    return 0;
}
Могут возникнуть проблемы, что imread возвращает матрицу нулевого размера, и изображение не выводится на экран. Дело в том, что Linker > Input > Additional Dependencies: прописываются отдельно для Debug и Release, например, opencv_core231d.lib и opencv_core231.lib соответственно. Необходимо положить в MyProject/Debug соответствующую библиотеку с "d", и все заработает!

пятница, 7 октября 2011 г.

Как подключить OpenCV 2.3 в Visual Studio 2010

Необходимо использовать советы из 1 и 2
1.
OpenCV 2.3.1 directory structure is different from other versions. I downloaded from OpenCV-2.3.1-win-superpack.exe. This assumes x86, 32-bit architecture, so change accordingly if you are on 64-bit Windows. Note this also includes libs for static linking (instead of requiring DLLs) in OpenCV2.3\build\[architecture]\[compiler]\staticlib. Project settings (this is kind of a deluxe set that was necessary to build the opencv_stitching example):
C/C++ > Include Directories:
X:\OpenCV2.3\build\include
X:\OpenCV2.3\modules\imgproc\src
Linker > Additional Library Directories:
X:\OpenCV2.3\build\x86\vc10\lib
Linker > Input > Additional Dependencies: (this will vary from one project to the next, but you probably want the first three)
opencv_core231.lib
opencv_highgui231.lib
opencv_imgproc231.lib
opencv_features2d231.lib
opencv_flann231.lib
opencv_gpu231.lib
opencv_haartraining_engine.lib
opencv_legacy231.lib
opencv_ml231.lib
opencv_objdetect231.lib
opencv_ts231.lib
opencv_video231.lib
opencv_calib3d231.lib
opencv_contrib231.lib
Add to PATH environment variable (or copy DLLs into output directory):
X:\OpenCV2.3\build\x86\vc10\bin
X:\OpenCV2.3\build\common\tbb\ia32\vc10 (optional, for Intel Threading Building Blocks parallel processing support)

Взято отсюда:

2.Download OpenCV-2.3.0-win-superpack.exe and execute it to extract all files to a folder named OpenCV2.3. Inside this folder there are 2 directories: build and opencv. All the setup on VS2010 will refer to the build directory. For practical purposes I moved the folder OpenCV2.3 to my C:\ drive, so pay attention to the paths I suggest on this guide as yours might be different.

On Visual Studio, create a new Win32 Console Application project and name it whatever you like. After that, a new window will show up. Click on the tab Application Settings and make sure the option Empty Project gets selected:

Add a new file main.cpp to the folder Source Files, then add this code to main.cpp:
#include <stdio.h>
#include <cv.h>
#include <highgui.h>
 
int main(int argc, char* argv[])
{
if (argc < 2)
{
    printf("Usage: ./opencv_hello <file.png>\n");
    return -1;
}
 
    IplImage* img = cvLoadImage(argv[1], CV_LOAD_IMAGE_UNCHANGED);
if (!img)
{
    return -1;
}
 
cvNamedWindow("display", CV_WINDOW_AUTOSIZE);
    cvShowImage("display", img );
 
    cvWaitKey(0);        
 
    return 0;
}

At this point, we need to configure the project so it can locate OpenCV headers and libraries. Go to the Project Properties (ALT+F7), and once the new window shows up do the following:

On the Configuration box, select All Configurations

Open Configuration Properties > C/C++ > General, and edit the field Additional Include Directories to add these 3 paths (for the headers):

C:\OpenCV2.3\build\include\opencv

C:\OpenCV2.3\build\include\opencv2

C:\OpenCV2.3\build\include

Note that include\opencv is for the C interface of OpenCV and include\opencv2 if for the C++ interface. We are also adding the folder include to prevent our build from being broken by some headers of the C interface that refer to C++ headers as opencv2\core.

Then, add the path of the libraries on Configuration Properties > Linker > General, and on the Additional Library Directories field, add this: C:\OpenCV2.3\build\x86\vc9\lib:

Finally, for this simple test we are going to add the libraries opencv_core230.lib and opencv_highgui230.lib. So go to Configuration Properties > Linker > Input and add them:

When writing more complex applications you'll probably need to add other OpenCV libs that I did not mentioned on this little project of ours.

Press F7 to Build Solution and you should see:

========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
To be able to execute the application you'll need to modify the PATH environment variable of your system to add the location of OpenCV's DLLs. Add this to end of PATH:

; C:\OpenCV2.3\build\x86\vc9\bin

Взято отсюда: http://stackoverflow.com/questions/7011238/opencv-2-3-c-visual-studio-2010

После всего этого компилим проект, положив в папку debug файлы opencv_core230.lib and opencv_highgui230.lib.
Запускаем F:\Debug\1.exe Картинка.png в командной строке
И картинка выводится на экран.