OpenCV将相同的图像放置到容器中
OpenCV placing the same image into the container
我正在尝试编写一个程序,从视频中捕获n帧,并将它们放入容器中进行进一步的工作(之后我将制作拼贴)。但我遇到了一个问题,当容器中的所有图像都相同时(它只填充了最后捕获的图像)。我已经检查了图像是否被正确捕获,因为我也在保存它们,并且可以清楚地看到它们是不同的。
这是我的代码:
std::vector<cv::Mat> MY_framelist; //container for captured frames
cv::VideoCapture myvid;
cv::Mat MY_frame; //will capture frames here
myvid.open(pass_filename); //open video file(char* pass_filename=12.mp4)
if (!myvid.isOpened()) {
printf("Capture not open n");
}
double x_length = myvid.get(CV_CAP_PROP_FRAME_COUNT); //get maxlength of the video
uint each_frame = uint(x_length) / 16; //capture every 16 frames
for (uint j = 0, current_frame = 1; (current_frame < x_length) && (j < 16); current_frame += each_frame, j++)
{
myvid.set(CV_CAP_PROP_POS_FRAMES, current_frame); //set frame
myvid.read(MY_frame); // then capture the next one
MY_framelist.push_back(MY_frame); //place it into the container
std::stringstream frameNum; //generating name for saved images
frameNum << j + 1;
if (j + 1 <= 9)
my_filename += "0";
my_filename += frameNum.str();
my_filename += ".jpg";
cv::imwrite(my_filename.c_str(), MY_frame); //saving images to prove that they are captured correctly
my_filename = "test";
printf(" and Image # ");
printf("%d", j + 1);
printf(" saved n");
}
结果,MY_framelist将包含上次捕获的16个相同的图像。我在这里做错了什么?我在这里看到了一些解决方法,但我并不急于这样做,因为这会导致不那么准确的结果。提前感谢!
OpenCVMat
副本是浅副本,即只复制标头,不复制数据。所以这里:
MY_framelist.push_back(MY_frame); //place it into the container
您将得到一个始终具有相同图像的容器。
您还需要进行深度复制复制数据:
MY_framelist.push_back(MY_frame.clone()); //place it into the container
相关文章:
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 如何使用OpenCV将RBG图像转换为HSV,并将H、S和V值保存为C++中的3个独立图像
- OpenCV EqualizeHist()从彩色图像创建黑白图像
- 如何使用 OpenCV 解码在两个 UWP 应用之间发送的图像字节?
- 如果不在窗口 10 中声明名为 openCV 的 openCV namedWindow,QT 图像显示将无法正常工作
- 在本地网络中通过OpenCV(C++)实时流式传输图像
- 如何在 OpenCV 中将图像与另一个图像相关联
- 如何在 OpenCV c++ 中压缩 TIFF 格式的图像?
- OpenCV - 如何在灰度图像上应用 Kmeans?
- 阅读和处理图像(OpenCV/C )
- cv::findContours 正在修改源图像 OpenCV 2.3
- 将图像替换为更大尺寸的图像 (OpenCV)
- 显示像素值已更改的图像openCV
- 具有积分图像的快速模糊图像(OpenCV)
- 扫描图像OpenCV上的文档检测
- 使用cv::Mat图像(opencv)如何检测对象
- 写作和展示图像OpenCV
- 从文件夹中读取图像(OpenCV)
- 无法保存从网络摄像头捕获的图像(OpenCV 2.3 的 imwrite 编译错误)
- 旋转不变的多个模板出现在图像Opencv