使用VideoCapture读取目录中的图像不起作用
Reading images in directory using VideoCapture not working
我正在尝试读取多个名为001.jpg、002.jpg、003.jpg及以上的图像文件,方法是在Linux上使用OpenCV,并在一段延迟后将其显示在窗口中。这是我的代码-
int main(int argc, char ** argv){
cv::VideoCapture cam("/home/bikz05/Desktop/dataset/%03d.jpg");
if (!cam.isOpened())
cout << "Capture is not opened";
cv::Mat image;
const string Window = "Image Number";
namedWindow(Window, CV_WINDOW_NORMAL);
while(true) {
cam >> image;
if (!image.data) {
std::cout << "Preview Ends" << std::endl;
break;
}
cv::imshow(Window, image);
waitKey(1000);
}
return 0;
}
在主功能的第一行中,我甚至尝试使用
cv::VideoCapture cam("/home/bikz05/Desktop/dataset/001.jpg");
我甚至试着把图像和二进制文件放在同一个文件夹里,即使这样也无济于事。
在这两种情况下,它都只显示第一个图像,然后程序退出。注意,它还没有结束。
附言:Stack Exchange和opencv.org上提供的答案并没有产生预期的结果。
您编写的代码有几个问题,但如果要从磁盘读取图像,请使用cv::imread()
而不是cv::VideoCapture
:
std::string filename = "/home/bikz05/Desktop/dataset/001.jpg";
const std::string Window = "Image Number";
cv::namedWindow(Window, CV_WINDOW_NORMAL);
while (true)
{
cv::Mat image = cv::imread(filename.c_str());
if (!image.data)
{
std::cout << "!!! File not found: " << filename << std::endl;
break;
}
cv::imshow(Window, image);
cv::waitKey(1000);
// And before the loop ends, update filename with the next image:
// filename = ???;
}
我还没有测试过,但我相信你已经明白了。
使用glob作为glob(folderpath, vectorOfimages)
,然后访问每个图像作为vectorOfimages[i]
。
vectorOfimages是
vector<String>
folderpath是一个字符串。
我遇到了同样的问题,videoCapture
无法解决。
相关文章:
- 我的神经网络不起作用 [XOR 问题]
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- C++为什么尽管我调用了void函数,它却不起作用
- 为什么在保护模式下继承升级不起作用
- 循环在计数器中不起作用
- 在其他文件中创建类时在 c++ 项目中不起作用
- Visual studio代码重构似乎不起作用(例如,重命名符号-f2)
- 为什么二进制搜索在我的测试中不起作用
- 我的代码中有错误吗?使用BGI图形的C++代码对我不起作用
- glTexImage2D 不起作用,但将 FragColor 设置为 vec4(1.0, 0, 0, 1.0) 会给我红色图像
- 与 alpha 通道的图像重叠在 QT 上不起作用
- SDL_2加载图像不起作用
- 使用16位签名格式打开CV不起作用..裁剪图像并重新调整图像大小
- 旋转图像不起作用
- 将opencv图像转换为gdi位图不起作用取决于图像大小
- 为什么我的 OpenCL 3D 图像查找不起作用?
- 使用VideoCapture读取目录中的图像不起作用
- 我的加载图像不起作用 - 我需要知道为什么
- 从资源加载图像不起作用(C++ Dll)