有了第一篇的安装环境,下面执行第一个程序,也就是显示一张图片。。
其实CMakeLists.txt都不用自己写,找到opencv-3.4.14/samples/cpp/example_cmake目录下的CMakeList.txt,然后拷贝到和测试程序的同个目录下:
然后将CMakeList里面的文件名改一下。。还有就是Cpp文件名改一下。。
下面的CMakeList的imgshow 和first.cpp就是我的文件名和文件夹名。。
# cmake needs this line
cmake_minimum_required(VERSION 2.8)
# Define project name
project(imgshow)
# 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)
# If the package has been found, several variables will
# be set, you can find the full list with descriptions
# in the OpenCVConfig.cmake file.
# Print some message showing some of them
message(STATUS "OpenCV library status:")
message(STATUS " version: ${OpenCV_VERSION}")
message(STATUS " libraries: ${OpenCV_LIBS}")
message(STATUS " include path: ${OpenCV_INCLUDE_DIRS}")
if(CMAKE_VERSION VERSION_LESS "2.8.11")
# Add OpenCV headers location to your include paths
include_directories(${OpenCV_INCLUDE_DIRS})
endif()
# Declare the executable target built from your sources
add_executable(imgshow first.cpp)
# Link your application with OpenCV libraries
target_link_libraries(imgshow PRIVATE ${OpenCV_LIBS})
#include <iostream>
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int main(int argc, char **argv)
{
Mat srcImage = imread("/home/zion/CPP-study/12.opencv/imgshow/opencv-logo.png"); //图片名称最好是绝对路径,不然肯可能会报错
imshow("L7256",srcImage);
//waitKey()是OpenCV中的内置函数,语句waitKey(0);表示“暂停程序,等待一个按键输入”!也就是说,当程序执行到waitKey(0);时,程序被暂停运行,只有当你输入一个按键时,它才会继续运行。
waitKey(0);
return 0;
}
- sudo cmake .
- sudo make
- ./imgshow
执行上述步骤即可看到显示一张图片。。。