用如下方法编译opencv:
git clone "https://github.com/opencv/opencv.git"
mkdir opencv_debug
cd opencv
mkdir build
cd build
cmake ..
#查看编译选项
cmake -LA | awk '{if(f)print} /-- Cache values/{f=1}'
#我的编译选项(其实反复了很多次)
/usr/local/bin/cmake .. -DCMAKE_BUILD_TYPE=Debug -DCMAKE_INSTALL_PREFIX=/data/ahfuzhang/install_packages/opencv_debug/ -DBUILD_SHARED_LIBS=OFF -DBUILD_WITH_DYNAMIC_IPP=OFF -DWITH_IPP=OFF -DBUILD_IPP_IW=OFF -DBUILD_ITT=OFF -DBUILD_JAVA=OFF -DBUILD_PROTOBUF=OFF -DBUILD_TESTS=OFF -DBUILD_opencv_apps=OFF -DBUILD_opencv_python_bindings_generator=OFF -DWITH_CUDA=OFF -DWITH_ITT=OFF -DWITH_OPENCL=OFF -DWITH_OPENCLAMDBLAS=OFF -DWITH_OPENCLAMDFFT=OFF -DWITH_PROTOBUF=OFF -DWITH_OPENEXR=OFF -DWITH_JASPER=OFF -DWITH_PNG=OFF -DWITH_WEBP=OFF
make -j20 #我核多,土豪
make install
写了如下一个计算图像直方图的程序:
//image_hist.cpp #include <iostream> #include <opencv2/imgproc.hpp> #include <opencv2/highgui.hpp> using namespace cv; using namespace std; int main( int argc, char** argv ) { if (argc<2){ printf("usage:%s <img> ", argv[0]); return -1; } Mat img = imread(argv[1], IMREAD_UNCHANGED); if (img.data==NULL){ printf("read file fail "); return -1; } int channels[] = {0}; int histSize[] = {256, 1}; float _ranges[] = { 0, 256 }; const float* ranges[] = {_ranges}; Mat hist; calcHist(&img, 1, channels, Mat(), hist, 2, histSize, ranges, true, false); for (int i=0; i<256; ++i){ int count = (int)hist.at<float>(i, 0); printf("%d ", count); } return 0; }
编译:
g++ -o image_hist.o -c image_hist.cpp -g -I"/data/ahfuzhang/install_packages/opencv_debug/include/opencv4/" -std=c++11
链接:
g++ -o image_hist image_hist.o -L"/data/home/ahfuzhang/install_packages/opencv_build/lib64/" -lopencv_calib3d -lopencv_features2d -lopencv_flann -lopencv_gapi -lopencv_highgui -lopencv_imgcodecs -lopencv_imgproc -lopencv_ml -lopencv_objdetect -lopencv_photo -lopencv_stitching -lopencv_video -lopencv_videoio -lopencv_core -lrt -lpthread -ldl -lz "/usr/lib64/liblapack.so.3.4.2" -lopenblas "/usr/lib64/libjpeg.so.62.1.0" "/usr/lib64/libtiff.so.5.2.0"
运行:
./image_hist 1.jpg
read file fail
DEMO并未运行成功,网上搜了是库的问题,等下继续解决!