cv::Mat中的地址错误
Bad address in cv::Mat
我正在制作一个使用 opencv 和 zeromq 的C++应用程序。在尝试通过 zmq tcp 套接字发送 cv::Mat 对象 (CV_8UC3( 时,我遇到了一些问题。
下面是更新的代码示例:
#include <iostream>
#include <zmq.hpp>
#include <pthread.h>
#include <opencv/cv.h>
#include <opencv/highgui.h>
using namespace std;
int main()
{
zmq::context_t ctx( 1 );
zmq::socket_t mysocket( ctx, ZMQ_PUSH );
mysocket.bind( "tcp://lo:4050" );
cv::VideoCapture capture( CV_CAP_ANY );
capture.set( CV_CAP_PROP_FRAME_WIDTH, 640 );
capture.set( CV_CAP_PROP_FRAME_HEIGHT, 480 );
cv::Mat3b frame;
capture >> frame; //First one is usually blank
capture >> frame;
capture >> frame;
cv::Mat3b clonedFrame( 480, 640, CV_8UC3 );
frame.copyTo( clonedFrame );
cout << "Original:" << endl
<< "Address of data:t" << &frame.data << endl
<< "Size:ttt" << frame.total() * frame.channels() << endl << endl;
cout << "Cloned:" << endl
<< "Address of data:t" << &clonedFrame.data << endl
<< "Size:ttt" << clonedFrame.total() * clonedFrame.channels() << endl << endl;
cout << "Gap between data:t" << &clonedFrame.data - &frame.data << endl;
unsigned int frameSize = frame.total() * frame.channels();
zmq::message_t frameMsg( frame.data, frameSize, NULL, NULL );
zmq::message_t clonedFrameMsg( clonedFrame.data, frameSize, NULL, NULL );
cv::imshow( "original", frame );
cv::imshow( "cloned", clonedFrame );
cvWaitKey( 0 );
if( frame.isContinuous() )
{
cout << "Sending original frame" << endl;
mysocket.send( frameMsg, 0 ); //This works
cout << "done..." << endl;
}
cvWaitKey( 0 );
if( clonedFrame.isContinuous() )
{
cout << "Sending cloned frame" << endl;
mysocket.send( clonedFrameMsg, 0 ); //This fails
cout << "done..." << endl;
}
return EXIT_SUCCESS;
}
后者 send(( 使 zmq 失败一些断言。输出:
Original:
Address of data: 0xbfdca480
Size: 921600
Cloned:
Address of data: 0xbfdca4b8
Size: 921600
Gap between data: 14
Sending original frame
done...
Sending cloned frame
Bad address
done...
nbytes != -1 (tcp_socket.cpp:203)
为什么 clone(( 会弄乱指针,我该如何解决这个问题?
任何帮助,不胜感激。
编辑 2012-05-25:更新了代码示例。我可以通过向消息构造函数提供以下指针之一来发送原始帧:frame.ptr((,frame.data,frame.datastart,frame.at((。它们都适用于原始版本,但不适用于构造函数。如您所见,两个数据指针之间的地址空间很小。它不应该至少是框架大小吗?
//John
好的...所以我最终制作了自己的复制功能
它看起来像这样:
struct frameData_t
{
unsigned char *data;
size_t size;
};
struct frameData_t *copyMatData( cv::Mat3b &indata )
{
struct frameData_t *ret = new struct frameData_t;
ret->size = indata.total() * indata.channels();
ret->data = new unsigned char[ret->size];
int datapos = 0;
for( int channel = 0; channel < indata.channels(); channel++ )
{
for( int row = 0; row < indata.rows; row++ )
{
const cv::Vec3b *srcrow = indata[row];
for( int col = 0; col < indata.cols; col++, datapos++ )
{
ret->data[datapos] = srcrow[col][channel];
}
}
}
return ret;
}
我像这样使用它:
struct frameData_t *clonedFrame = copyMatData( frame );
zmq::message_t frameMsg( frame.data, frameSize, NULL, NULL );
mysocket.send( clonedFrameMsg, 0 );
如果有人知道更好的方法,请告诉我。
//John
相关文章:
- 为什么我在leetcode上收到AddressSanitizer:地址0x602000000058上的堆缓冲区溢出错误
- 地址的奇怪错误
- 为什么我得到以下代码地址清理器:未知地址错误的SEGV
- 运行时错误:引用绑定到类型"int"的未对齐地址0xbebebebebebebec6,这需要 4 个字节对齐 (stl_vector.h)
- 我有一个线程 1:EXC_BAD_ACCESS(代码 = 1,地址 = 0x8)错误.我认为这是由于内存管理不好.我可以
- Assimp 库错误:获取打包成员的地址
- 运行时错误地址清理器:LEETCODE 中的致命信号
- 瓦尔格林德错误 - 地址0x0不是堆叠的 malloc'd 或自由的
- 获取未知分段错误"(地址)在 std::__cxx11::basic_string<char, std::char_traits<char>,..., std::分配器<cha
- 来自简单循环的 OpenAcc 错误:内核执行期间的非法地址
- 带有 NORM 的 ZeroMQ - 地址已在使用 错误被抛出在第二个 .bind() 上 - 为什么?
- 重载新返回错误的地址
- 线程 1:xcode 中出现EXC_BAD_ACCESS(代码 = 1,地址 = 0x0)错误
- cv::Mat中的地址错误
- 尝试使用pthreads访问共享数据数组时,出现“无法访问内存地址”错误
- Windbus示例应用程序无法启动(无法解析服务器地址错误)
- 分配OpenCL缓冲区时内存地址错误
- 未定义的引用,但(动态)库被链接.(可能是坏的重定位地址错误)
- 圆括号成员函数地址错误
- ZeroMQ:重新绑定套接字时使用地址错误