I am making a C ++ application that uses opencv and zeromq. And I am experiencing some problems when trying to send a cv :: Mat object (CV_8UC3) through the zmq tcp socket.
Here is an example of the updated code:
#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;
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:\t\t\t" << frame.total() * frame.channels() << endl << endl;
cout << "Cloned:" << endl
<< "Address of data:\t" << &clonedFrame.data << endl
<< "Size:\t\t\t" << 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 );
cout << "done..." << endl;
}
cvWaitKey( 0 );
if( clonedFrame.isContinuous() )
{
cout << "Sending cloned frame" << endl;
mysocket.send( clonedFrameMsg, 0 );
cout << "done..." << endl;
}
return EXIT_SUCCESS;
}
The last send () causes zmq to issue a statement. conclusion:
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)
Why clone () messed up the pointer and how can I solve it?
Any help is appreciated.
Edit 2012-05-25: Updated sample code. I can send the original frame by pointing to one of the following pointers to the message constructor: frame.ptr (), frame.data, frame.datastart, frame.at (). They all work for the original, but not for the designer. As you can see, the address space between the two datapointers is small. Should it be at least frameSize?
//John
source
share