0

I am trying to read video frames in OpenCV, then copy the data into my another C++ code to perform something else. My code is as follows:

cv::Mat capturedFrame; 
int newData[600][800];
std::cout<<"Debug1 " << std::endl;
memcpy((char*)newData, (char*)capturedFrame.data, 600*800*sizeof(int) );
std::cout<<"Debug2 " << std::endl;
mycode.setData ( newData );
std::cout<<"Debug3 " << std::endl;

Then the class "setData" was defined as follows:

char data [600][800];
void mycode::setData ( char newData[600][800] )
{
  for ( int m=0; m < 600; m ++ )
  {
      for ( int n = 0; n < 800; n ++ )
      {
          data[i][j] = newData[i][j]; 
      }
  }
}

But the code stops when it comes to the line:

    mycode.setData ( newData );

What confuses me is, if I delete this code, then I can see "Debug1" to "Debug3" on the screen, which is normal. But if I use this code, the program stops even without printing out "Debug1" and "Debug2" on the screen. This is really strange. I also tried to comment out all the lines in the "setData" class to make it be an empty class, but the error still is the same. So I believe it was not about the "setData" class. I also know that the "capturedFrame.data" was correct, because I performed some other filters on it, and the result was good. Can someone explain the error here?

Edit:

I used a debugger, but there was no error message but the program just stopped responding. Also, I changed the data type into "char".

4

2 回答 2

4

这个数组:

int newData[600][800];

大于 1 MB。如果这是一个局部变量,那么您可能会破坏堆栈。

数组可能也是data如此,但由于您的代码片段的上下文很少,因此很难知道什么是静态分配的还是自动分配的。

我认为您应该考虑动态分配这些大型数组。

于 2012-09-28T07:57:17.500 回答
1

如果你注释掉

mycode.setData ( newData );

编译优化器可能知道newData没有使用,所以

memcpy((int*)newData, (int*)capturedFrame.data, 600*800*sizeof(int) );

也可能被消除,因此它可能没有被执行。

问题可能存在于memcpy方法中,甚至其他地方。

根据您提供的有限信息,很难调查真正的原因,但我建议您可以深入研究其他代码。

于 2012-09-28T07:55:37.297 回答