工具准备
1.clion官网链接:clion
2.cmake官网链接:cmake
下载红框标记的压缩包,免安装。
3.mingw官网链接:mingw
安装红框标记下载免安装版本,解压可用。
4.opencv源码地址:opencv
安装clion+mingw+cmake
1.mingw安装:解压mingw压缩文件,并配置bin路径到环境变量(路径中不要有空格)
2.cmake安装:解压cmake压缩文件,并配置bin路径到环境变量(路径中不要有空格)
3.clion安装:clion安装教程。
编译opencv库
用clion打开opencv源码目录并对clion进行如下设置:
配置完成后,clion会自己更新工程的cmake配置,期间会报一些python的错误,无需理会,待cmake的配置刷新完后,点击build就可开始编译了,编译过程会报很多warning,无需理会。
重要:编译完后一定要install一次,不然后面会找不库。
编译中遇到的错误:
1.报" file too big”->在opencv的CmakeList.txt中添加:set(CMAKE_CXX_FLAGS “${CMAKE_CXX_FLAGS} -Wunused-parameter -Wextra -Wreorder -Wa,-mbig-obj -O2”)
安装完成后,添加路径“xxx\cmake-build-debug\install\x64\mingw\bin”到环境变量(这里最好重启电脑一下,否则环境变量可能不生效)。
测试程序
1.修改测试程序CmakeList.txt如下:
cmake_minimum_required(VERSION 3.23)
project(OpencvTest)
set(CMAKE_CXX_STANDARD 14)
set(OpenCV_DIR xxx\cmake-build-debug\install)
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
add_executable(OpencvTestmain.cpp)
target_link_libraries(OpencvTest${OpenCV_LIBS})
2.main.cpp—官网实例,打开笔记本电脑自带摄像头,并按下空格抓怕。
#include <opencv2\opencv.hpp>
#include <iostream>
#include <opencv2/highgui/highgui_c.h>
#include <string>
using namespace cv;
using namespace std;
int main(){
Mat src;// use default camera as video source
VideoCapture cap(0);// check if we succeededif(!cap.isOpened()){
cerr <<"ERROR! Unable to open camera\n";return-1;}// get one frame from camera to know frame size and type
cap >> src;// check if we succeededif(src.empty()){
cerr <<"ERROR! blank frame grabbed\n";return-1;}
bool isColor =(src.type()==CV_8UC3);//--- INITIALIZE VIDEOWRITER
VideoWriter writer;
int codec = VideoWriter::fourcc('M','J','P','G');// select desired codec (must be available at runtime)
double fps =25.0;// framerate of the created video stream
string filename ="./live.avi";// name of the output video file
writer.open(filename, codec, fps, src.size(), isColor);// check if we succeededif(!writer.isOpened()){
cerr <<"Could not open the output video file for write\n";return-1;}//--- GRAB AND WRITE LOOP
cout <<"Writing videofile: "<< filename << endl
<<"Press any key to terminate"<< endl;
unsigned int index =0;for(;;){// check if we succeededif(!cap.read(src)){
cerr <<"ERROR! blank frame grabbed\n";break;}// encode the frame into the videofile stream
writer.write(src);// show live and wait for a key with timeout long enough to show imagesimshow("Live", src);//if (waitKey(5) >= 0)// break;if(waitKey(10)==32){
string fileName ="./"+to_string(index)+".jpg";
index++;imwrite(fileName, src);
cout << fileName <<"save success !"<< endl;}}// the videofile will be closed and released automatically in VideoWriter destructorreturn0;}
博主花了1.5天的样子配置这个东西,网上的文章大都是用camke-gui.exe这个可视化的cmake工具在做编译,我一直没搞成功过,后面发现用clion编译也可以,就试着编译了一下,结果成功了,文章有点糙,若在参考过程中遇到问题的朋友可以留言,看到后能帮忙解答的我会回复。
版权归原作者 Sheldon1990 所有, 如有侵权,请联系我们删除。