Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8011893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:09:19+00:00 2026-06-04T19:09:19+00:00

I am making a C++ application that uses opencv and zeromq. And I am

  • 0

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 ) over a zmq tcp socket.

Here is the updated code sample:

#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:\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 ); //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;
}

The latter send() makes zmq fail some assertion.
output:

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 does clone() mess up the pointer, and how can I solve this?

Any help is appreciated.

Edit 2012-05-25:
Updated the code sample.
I can send the original frame by giving one of the following pointers to the message constructor: frame.ptr(), frame.data, frame.datastart, frame.at(). They all work for the original, but none for the constructor.
As you can see, the address space between the two datapointers is small. Shouldn’t it bee at least frameSize?

//John

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T19:09:21+00:00Added an answer on June 4, 2026 at 7:09 pm

    Ok… So I ended up making my own copy function

    It looks like this:

    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;
    }
    

    And i use it like this:

    struct frameData_t *clonedFrame = copyMatData( frame );
    zmq::message_t frameMsg( frame.data, frameSize, NULL, NULL );
    mysocket.send( clonedFrameMsg, 0 );
    

    If anyone knows of a better way, please let me know.

    //John

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making an application in Win32 API that uses the modern Ribbon style UI.
I am making an application that uses Drools planner. The @ValueRangeFromSolutionProperty is supposed to
Alright, I'm in the process of making a C# application that uses an API
I'm making an Android application that reads an XML Internet. This application uses SAX
I'm making a web application that uses hash tags for page navigation like this
I am making an android application that uses these views: (Root) - LinearLayout (Child
I am making an android application that uses a listview. I want to get
I’m making a sample application that uses MigLayout in a very cool way. Unfortunately
Im making an application that uses of API-threads in C, The program takes N-files
I am making a application that uses the GPS receiver. The application will work

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.