OpenCVを使用する開発環境設定

C++

ライブラリのインストールとコンパイルの設定を行います。
サンプルコード

#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
    Mat image;
    image = imread("C:\\Windows\\Web\\4K\\Wallpaper\\Windows\\img0_1920x1200.jpg", IMREAD_COLOR); // Read the file
    if (image.empty()) // 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;
}

Windows + Visutal Studio

右側の Release のリンクから、opencv-4.6.0-vc14_vc15.exe をダウンロードし、実行します。
https://github.com/opencv/opencv

こちらを参考にプロジェクトの設定を行います。
https://docs.opencv.org/4.x/dd/d6e/tutorial_windows_visual_studio_opencv.html
インクルードの設定

ライブラリの設定

PATH に DLL のあるディレクトリを追加します。

あるいは、DLL をプロジェクト配下にコピーします。

MacOS +  HomeBrew + Xcode

$ brew install opencv

$ pkg-config --cflags opencv4
-I/usr/local/opt/opencv/include/opencv4

$ pkg-config --libs opencv4
-L/usr/local/opt/opencv/lib -lopencv_gapi -lopencv_stitching -lopencv_alphamat -lopencv_aruco -lopencv_barcode -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_dnn_objdetect -lopencv_dnn_superres -lopencv_dpm -lopencv_face -lopencv_freetype -lopencv_fuzzy -lopencv_hfs -lopencv_img_hash -lopencv_intensity_transform -lopencv_line_descriptor -lopencv_mcc -lopencv_quality -lopencv_rapid -lopencv_reg -lopencv_rgbd -lopencv_saliency -lopencv_sfm -lopencv_stereo -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_superres -lopencv_optflow -lopencv_surface_matching -lopencv_tracking -lopencv_highgui -lopencv_datasets -lopencv_text -lopencv_plot -lopencv_videostab -lopencv_videoio -lopencv_viz -lopencv_wechat_qrcode -lopencv_xfeatures2d -lopencv_shape -lopencv_ml -lopencv_ximgproc -lopencv_video -lopencv_dnn -lopencv_xobjdetect -lopencv_objdetect -lopencv_calib3d -lopencv_imgcodecs -lopencv_features2d -lopencv_flann -lopencv_xphoto -lopencv_photo -lopencv_imgproc -lopencv_core

Xcode 上から
左上のプロジェクト名をクリック
Build Settings > All > Search Paths > Header Search Paths > /usr/local/opt/opencv/include/opencv4, non-recursive
Build Phases > Link Binary With Libraries
/usr/local/opt/opencv/lib/libopencv_core.dylib, /usr/local/opt/opencv/lib/libopencv_imgcodecs.dylib, … をドロップ

Ubuntu

$ sudo apt update

$ sudo apt install libopencv-dev

$ pkg-config --cflags opencv4
-I/usr/include/opencv4/opencv -I/usr/include/opencv4

$ pkg-config --libs opencv4
-lopencv_stitching -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_dnn_objdetect -lopencv_dnn_superres -lopencv_dpm -lopencv_highgui -lopencv_face -lopencv_freetype -lopencv_fuzzy -lopencv_hdf -lopencv_hfs -lopencv_img_hash -lopencv_line_descriptor -lopencv_quality -lopencv_reg -lopencv_rgbd -lopencv_saliency -lopencv_shape -lopencv_stereo -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_superres -lopencv_optflow -lopencv_surface_matching -lopencv_tracking -lopencv_datasets -lopencv_text -lopencv_dnn -lopencv_plot -lopencv_ml -lopencv_videostab -lopencv_videoio -lopencv_viz -lopencv_ximgproc -lopencv_video -lopencv_xobjdetect -lopencv_objdetect -lopencv_calib3d -lopencv_imgcodecs -lopencv_features2d -lopencv_flann -lopencv_xphoto -lopencv_photo -lopencv_imgproc -lopencv_core

$ g++ opencv_test.cpp `pkg-config --cflags --libs opencv4`

$ ./a.out

コメント

タイトルとURLをコピーしました