gpt4 book ai didi

c++ - 将 OpenCv Mat 插入到 C++ std::vector

转载 作者:太空宇宙 更新时间:2023-11-03 21:57:09 25 4
gpt4 key购买 nike

我正在将 Mat 对象插入 std::vector。

但是,当我在那之后尝试访问元素 (Mat) 时,它没有给我之前推送的图像,这取决于 frameOrientation。

例如,在下面的情况下,我得到与“before_push_rotated3.jpg”相同的“after_push_rotated2.jpg”,这不应该。

如果我将 frameOrientation 设置为所有“2s”或“4s”,“after_push_rotated1/2/3.jpg”都是相同的并且等于“before_push_rotated3.jpg”。

这里有什么问题吗?

#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
#include <iostream>
#include <vector>

using namespace cv;
using namespace std;

vector<Mat> checkOrientationAndRotate()
{
Mat frame1 = imread("1.jpg" );
Mat frame2 = imread("2.jpg" );
Mat frame3 = imread("3.jpg" );



vector<Mat> frameList;
vector<Mat> frameList_rotated;

frameList.push_back(frame1);
frameList.push_back(frame2);
frameList.push_back(frame3);

Mat transposedFrame;
Mat rotatedFrame;

int i=0;
for (vector<Mat>::iterator iter = frameList.begin(); iter != frameList.end(); iter++)
{
//Check for orientation of that frame
int frameOrientation;
if (i==0) frameOrientation = 1;
if (i==1) frameOrientation = 2;
if (i==2) frameOrientation = 4;

switch (frameOrientation)
{
case 1: //1 - no rotate
rotatedFrame = *iter;
break;

case 2: //2 - rotate it ACW 90 deg
transpose(*iter, transposedFrame);
flip(transposedFrame, rotatedFrame, 0);
break;

case 3: //3 - rotate it 180deg
flip(*iter, rotatedFrame, -1);
break;

case 4: //4 - rotate it CW 90 deg
transpose(*iter, transposedFrame);
flip(transposedFrame, rotatedFrame, 1);
break;
default:
break;
}

//Check frame before pushing into vector
if (i==0) imwrite("before_push_rotated1.jpg",rotatedFrame);
if (i==1) imwrite("before_push_rotated2.jpg",rotatedFrame);
if (i==2) imwrite("before_push_rotated3.jpg",rotatedFrame);

frameList_rotated.push_back(rotatedFrame);


//Check frame after pushing into vector.
//Depending on the frameorientation, the frames are not the frames pushed in earlier!
int n=0;
for (vector<Mat>::iterator iter = frameList_rotated.begin(); iter != frameList_rotated.end(); iter++)
{
Mat frame = *iter;
if (n==0) imwrite("after_push_rotated1.jpg",frame);
if (n==1) imwrite("after_push_rotated2.jpg",frame);
if (n==2) imwrite("after_push_rotated3.jpg",frame);
n++;
}

i++;
} //for

return frameList_rotated;

}

void main()
{
vector<Mat> frameList_rotated = checkOrientationAndRotate();
}

最佳答案

[我最近没有使用 OpenCV,所以我可能错了,如果我错了请告诉我:)]

Mat 表示(某种)图像标题,而不是图像数据。 OpenCV 在内部维护一个引用计数机制,以避免跨不同区域复制大量图像像素数据。意思是当你说:

Mat a = b; // Only headers are copied, both a and b refer to the same image.

在上面的例子中,如果你修改b,这个改变也会反射(reflect)在a中。在这种情况下你需要使用的是:

Mat a = b.clone();

关于您的情况,我真的认为以下行应该可以解决问题(如果不能,控制流可能还有其他问题,呵呵):

尝试替换:

Mat transposedFrame;
Mat rotatedFrame;

与:

 //create a new image, not just the headers..
Mat transposedFrame(Size(image_width, image_height), image.type());
Mat rotatedFrame(Size(image_width, image_height), image.type());

关于c++ - 将 OpenCv Mat 插入到 C++ std::vector,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11332726/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com