这个是测试代码

#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
int main(int argc, char** argv){
    string fv=string(argv[1]);
    cv::VideoCapture cap(fv);
    cv::Mat img;
    cap>>img;
    cout<<img.size()<<endl;
    if(!cap.isOpened()){
        cout<<"why not open??"<<endl;
        exit(0);
    }
    return 0;
}

总是输出

[0 x 0]
why not open??

解决过程

这个问题之前遇到过,估计是lib库没链接好,但是这次怎么都弄不对,才想到 opencv 读取视频这里依赖ffmpeg,于是开始排查 opencv 编译这里,最终写上了依赖 FFMPEG的版本

cmake -D CMAKE_BUILD_TYPE=Release \
    -D CMAKE_INSTALL_PREFIX=$prefix\
    -D FFMPEG=ON \
    -D WITH_OPENCL=ON \
    -D USE_O3=ON \
    -D ENABLE_CXX11=ON \
    -D WITH_TBB=ON \
    -D WITH_IPP=ON \
    -D WITH_OPENMP=ON \
    -D WITH_WEBP=OFF \
    -D BUILD_TIFF=ON \
    -D ENABLE_FAST_MATH=ON \
    -D BUILD_EXAMPLES=OFF \
    -D BUILD_DOCS=OFF \
    -D BUILD_PERF_TESTS=OFF \
    -D WITH_CUDA=OFF \
    -D BUILD_TESTS=OFF ..

但是又遇到了问题,就是FFMEPG 这里总是 NO
在这里插入图片描述
经过资料搜索,发现需要配置 confg,改成

export PKG_CONFIG_PATH="bin/ffmpeg_opt/lib/pkgconfig:bin/x264_opt/lib/pkgconfig:$PKG_CONFIG_PATH"

cmake ... (之前的哪些)

但是仍然出现 NO,于是打算重新编译 FFMPEG

export PKG_CONFIG_PATH="bin/x264_opt/lib/pkgconfig/:$PKG_CONFIG_PATH"
../configure --prefix=$DIR/ffmpeg_opt --enable-shared --enable-static \
  --extra-cflags=' -pipe -O3 -fPIC' \
  --extra-cxxflags=' -pipe -O3 -fPIC'\
  --enable-pthreads --enable-zlib --enable-pic --enable-pthreads \
  --enable-gpl --enable-version3 --enable-hardcoded-tables --enable-libfreetype \
  --enable-optimizations --disable-doc  \
  --enable-libx264 --enable-avresample

make -j
make install

这里疑惑点是可能 --enable-avresample 是必须的

随后编译 opencv,这下config没问题了,但是make的时候报了错
在这里插入图片描述
这个其实很简单,make的时候设置 LD_LIBRARY_PATH就行

GitHub 加速计划 / opencv31 / opencv
238
21
下载
OpenCV: 开源计算机视觉库
最近提交(Master分支:4 个月前 )
6950bedb Use Mat::total() in Darknet IO 3 天前
105a7747 doc: update image codec configuration reference (add AVIF, fix JPEG XL) #28393 This PR updates the build configuration documentation to: - Add AVIF to the supported format list (it was missing). - Clarify that some codecs (AVIF, JPEG XL) do not support BUILD_* options because OpenCV does not bundle their source code. - Update the description to include "write" capabilities for these formats. ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [x] The PR is proposed to the proper branch - [ ] There is a reference to the original bug report and related work - [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [x] The feature is well documented and sample code can be built with the project CMake 4 天前
Logo

AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。

更多推荐