无法使用 OpenCv 3.4.5 从具有C++ dll 的网络摄像机 (rtsp) 中抓取帧



我正在用c ++(32位(构建一个简单的应用程序,该应用程序使用opencv从RTSP相机中抓取帧。

抓取相机帧的函数在与主程序分开的线程中运行。

我已经用mp4视频测试了这个应用程序,它工作正常。我能够抓住帧并处理它们。 但是,当我使用 rtsp 链接时,尽管我能够打开与相机的连接,但每当我尝试读取 grab(( 和 read(( 函数时都会返回 False。

首先,我认为这是rtsp链接的问题,但我做了一个简单的Python应用程序来测试它,它也可以工作。所以这不是链接。

这是我用来抓取帧的代码:

#ifndef _IMAGE_BUFFER_H_
#define _IMAGE_BUFFER_H_
#include <opencv2/core.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp>
.
.
.
VideoCapture capture_;
string address_;
atomic<bool> keep_alive_;
thread thread_;
int fps_;
mutex mutex_;
list<FramePair> frames_;
int Run()
{
capture_.open(address_, cv::CAP_ANY);
if (!capture_.isOpened()) {
printf("Could not open Camera feed! n");
return -1;
}
uint64_t period = uint64_t((float(1) / float(fps_)) * float(1000));
period = period - (period / 20);
uint64_t t0 = getCurrentTimeInMilli();
while (keep_alive_) {
uint64_t difftime = getCurrentTimeInMilli() - t0;
if (difftime < period) {
uint64_t sleep_time = period - difftime;
if (sleep_time < period) {
std::this_thread::sleep_for(std::chrono::milliseconds(sleep_time));
}
}
t0 = getCurrentTimeInMilli();
CaptureFrame();
}
return 0;
}
void CaptureFrame()
{
Mat frame;
bool ret = capture_.read(frame);
if (!ret) {
printf("Error in frame reading! n");
}
vector<uint8_t> jpeg;
cv::imencode(".jpg", frame, jpeg, vector<int>());
mutex_.lock();
frames_.push_back(FramePair(getCurrentTimeInMilli(), jpeg));
if (frames_.size() > FRAME_QUEUE_SIZE)
frames_.pop_front();
mutex_.unlock();
}

我使用的OpenCv版本是3.4.5

链接 :rtsp://<user>:<pass>@<ip>:<port>/media

我感谢在这件事上的任何帮助。


编辑1:

我尝试过的:

  • 我试过这个,但它没有用
  • 还尝试使用预构建的opencv 3.4.0版本进行64位,并且仍然相同

很抱歉回答我自己的问题。

但是经过大量的试验和错误,并阅读了有关在Windows中使用cv::VideoCapture问题的各种SO线程。

我发现了问题,

我试图在我的应用程序中静态链接 OpenCv,但由于许可证问题,ffmpeg 无法与应用程序一起静态编译。

我通过复制opencv_ffmpegxxx.dll解决了它,可以在/bin/中找到。并按照此处的建议将其粘贴到我的.exe文件夹中。

可能有一些解决方法可以将 ffmpeg dll 嵌入应用程序中,如此处的建议,但我还没有尝试过。我希望其他人可以从这个问题中受益。

感谢您的帮助。

最新更新