ubuntu+opencv顯示圖片
阿新 • • 發佈:2019-02-17
首先建立原始碼檔案showPicture.cpp:
#include "opencv2/core.hpp" #include "opencv2/imgproc.hpp" #include "opencv2/highgui.hpp" #include <iostream> using namespace cv; using namespace std; int main() { cout << "Hello OpenCV " << CV_VERSION << endl; // 載入影象 Mat myMat = imread("天.jpg", 1); // 建立一個視窗 namedWindow("Opencv Image", WINDOW_AUTOSIZE); // 顯示影象 imshow("Opencv Image", myMat); // 等待按鍵延時 ms waitKey(5000); return 0; }
然後建立編譯檔案CMakeLists.txt
# cmake needs this line 要求的最低版本 cmake_minimum_required(VERSION 2.8) # Define project name 定義工程名 project(example_project) # Find OpenCV, you may need to set OpenCV_DIR variable # to the absolute path to the directory containing OpenCVConfig.cmake file # via the command line or GUI 自動查詢庫 find_package(OpenCV REQUIRED) # Declare the executable target built from your sources 宣告可執行目標檔案及原始檔 add_executable(example showPicture.cpp) # 目標檔案,原始檔0,原始檔1,... # Link your application with OpenCV libraries 將目標檔案與庫連結 target_link_libraries(example ${OpenCV_LIBS}) # 目標檔案,庫路徑
$cmeke . 後出現問題:
原因:我裝opencv的時候沒有選擇預設路徑,所以要手動指定一下By not providing "FindOpenCV.cmake" in CMAKE_MODULE_PATH this project has asked CMake to find a package configuration file provided by "OpenCV", but CMake did not find one. Could not find a package configuration file provided by "OpenCV" with any of the following names: OpenCVConfig.cmake opencv-config.cmake
OpenCVConfig.cmake
這個檔案。
編譯檔案這樣寫:
# cmake needs this line 要求的最低版本
cmake_minimum_required(VERSION 2.8)
# Define project name 定義工程名
project(example_project)
# Find OpenCV, you may need to set OpenCV_DIR variable
set(OpenCV_DIR /usr/opencv-3.2.0/build) 手動設定OpenCVConfig.cmake的路徑!
# to the absolute path to the directory containing OpenCVConfig.cmake file
# via the command line or GUI 自動查詢庫
find_package(OpenCV REQUIRED)
# Declare the executable target built from your sources 宣告可執行目標檔案及原始檔
add_executable(example showPicture.cpp) # 目標檔案,原始檔0,原始檔1,...
# Link your application with OpenCV libraries 將目標檔案與庫連結
target_link_libraries(example ${OpenCV_LIBS}) # 目標檔案,庫路徑
接著
$cmake .
$make .
..
成功!