how to append data from buffer into something for OpenCV?

Hi Cplusplus,

I am in the midst of experimenting with C++ sockets and opencv. I've successfully gotten some text data to travel over the wire..

I want to send a variable sized frame over the wire... so sending the text of the dimensions.. and then the number of image bytes

but how do I send the image data and display it ??


Client.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <iostream>
#include <boost/asio.hpp>
#include <boost/array.hpp>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <opencv2/objdetect/objdetect.hpp>
#include <string>
#include <vector>


using boost::asio::ip::tcp;

using namespace std;
using namespace cv;

// source https://www.programmerall.com/article/7721373696/
// source https://cppsecrets.com/users/14041151035752494957504952535764103109971051084699111109/Programming-in-C00-using-boostasio.php

struct image_metadata_t {
    int width;
    int height;
    size_t image_size_bytes;
};

image_metadata_t parse_header(boost::asio::streambuf &buffer){

    std::string data_buff_str = std::string(boost::asio::buffer_cast<const char*>(buffer.data()));
    cout << data_buff_str << endl;

    int width_pos = data_buff_str.find("W"); 
    int x_pos = data_buff_str.find("x"); 
    int height_pos = data_buff_str.find("H"); 
    int comma_pos = data_buff_str.find(","); 

    std::cout << "data_buff_str.substr(x_pos+1,height_pos) " << data_buff_str.substr(x_pos+1,height_pos) <<std::endl;

    image_metadata_t meta_data;
    meta_data.width = std::stoi(data_buff_str.substr(0,width_pos));
    meta_data.height = std::stoi(data_buff_str.substr(x_pos+1,height_pos));
    meta_data.image_size_bytes = std::stoi(data_buff_str.substr(data_buff_str.find(",") + 1));

    return meta_data;
}


cv::Mat GetImageFromMemory(uchar* image, int length, int flag) {
    std::vector<uchar> data = std::vector<uchar>(image, image + length);
    cv::Mat ImMat = imdecode(data, flag);

    return ImMat;
}

int main()
{
    try{
        boost::asio::io_service io_service;
        tcp::endpoint end_point(boost::asio::ip::address::from_string("127.0.0.1"), 3200);
        tcp::socket socket(io_service);
        socket.connect(end_point);
        boost::system::error_code ignored_error;
        boost::asio::streambuf receive_buffer;

        // Now we retrieve the message header of 64 bytes
        size_t header_size = 64;
        boost::asio::read(socket, receive_buffer, boost::asio::transfer_exactly(header_size), ignored_error);

        if ( ignored_error && ignored_error != boost::asio::error::eof ) {
            cout << "first receive failed: " << ignored_error.message() << endl;
        } else {
            image_metadata_t header_data = parse_header(receive_buffer);

            const int IMAGE_WIDTH = header_data.width;
            const int IMAGE_HEIGHT = header_data.height;

            // Now we retrieve the frame itself
            std::cout << "Now asing for image bytes of size " << std::to_string(header_data.image_size_bytes) << std::endl;
            boost::asio::streambuf second_receive_buffer;
            boost::asio::read(socket, second_receive_buffer, boost::asio::transfer_exactly(header_data.image_size_bytes), ignored_error);
            if( ignored_error && ignored_error != boost::asio::error::eof ) {
                cout << "SECOND receive failed: " << ignored_error.message() << endl;
            }
            else {
                std::cout << "[IMAGE_WIDTH]  = " << std::to_string(IMAGE_WIDTH) << std::endl;
                std::cout << "[IMAGE_HEIGHT] = " << std::to_string(IMAGE_HEIGHT) << std::endl;
                std::cout << "image bytes  = " << std::to_string(header_data.image_size_bytes) << std::endl;
                std::cout << "secondbuffer data " << std::to_string(second_receive_buffer.data().size()) << std::endl;
                
                void* matrix_ready_data = const_cast<void*>(boost::asio::buffer_cast<const void*>(second_receive_buffer.data()));
                Mat img = cv::Mat(1, header_data.image_size_bytes, CV_8UC3, matrix_ready_data);
                img = img.reshape(0,IMAGE_HEIGHT);
                //Mat img2 = cv::imdecode(img, cv::LOAD_IMAGE_UNCHANGED);

                imshow("client", img);
                waitKey(5000);
            }
        }
    } catch (std::exception& e) {
        std::cerr << e.what() << std::endl;
    }
 
    return 0;
}


Server.Cpp - This sends the image.. eventually I will reverse the logic so it in fact props up the server but for now it technically connects to the client.cpp code...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <iostream>
#include <boost/array.hpp>
#include <boost/asio.hpp>
#include <boost/thread/thread.hpp>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <opencv2/core/core.hpp> 
#include <string>
#include <vector>

using boost::asio::ip::tcp;
using namespace std;
using namespace cv;
 
// source https://www.programmerall.com/article/7721373696/
// source https://cppsecrets.com/users/14041151035752494957504952535764103109971051084699111109/Programming-in-C00-using-boostasio.php
bool flag = false;    

void servershow()
{
    while (true)
    {
        if (flag)
        {
            //imshow("server",img);
            waitKey(20);
        }   
    }
}

cv::Mat retrieve_data(){

    std::string image_path = "/0_MONITORNEW_.jpg";
    cv::Mat image;
    image = imread(image_path, cv::IMREAD_COLOR);
    if(! image.data ) {
        std::cout << "Could not open or find the image" << std::endl;
    }
    return image;
}


int main()
{
    boost::thread thrd(&servershow);
    try
    {
        boost::asio::io_service io_service;
        tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), 3200));
        
        for (;;) {
            tcp::socket socket(io_service);
            acceptor.accept(socket);
            boost::system::error_code ignored_error;

            //retrieve the frame to be sent
            cv::Mat frame = retrieve_data();
            std::vector<uchar> buff;
            std::vector<int> param(2);
            param[0] = cv::IMWRITE_JPEG_QUALITY;
            param[1] = 80;
            cv::imencode(".jpg", frame, buff, param);

            // now we send the header message
            std:: string image_dimensions = "6016Wx3384H";
            std:: string image_bytes = std::to_string(buff.size());
            std::string message_header = image_dimensions + "," +  image_bytes;
            std::cout << "sending measage header of " << std::to_string(message_header.length()) << " bytes...." << std::endl;
            message_header.append(63 - message_header.length(), ' ');
            message_header = message_header + '\0';
            std::cout << "sending measage header of " << std::to_string(message_header.length()) << " bytes...." << std::endl;
            socket.write_some(boost::asio::buffer(message_header), ignored_error);

            // now we send the frame
            std::string message(reinterpret_cast<char*>(buff.data()), buff.size()); 
            std::cout << "sending image message of " << std::to_string(message.length()) << " bytes...." << std::endl;
            socket.write_some(boost::asio::buffer(message), ignored_error);

            std::cout << "send image finished" << std::endl;
            flag = true;
        }
    }
    catch (std::exception& e)
    {
        std::cerr << e.what() << std::endl;
    }
    thrd.join();

    return 0; 
}
Last edited on
this is the output so far

1
2
3
4
% ./build/bin/sockets-server   
2441673
sending....
send image finished


1
2
./build/bin/sockets-client
6016wx3384h 2441673^IMG


so just looking for how to inject the image data and stream more frames eventually

Last edited on
I'm very very close now...

I can display "something" on the client side.. but its a black image with a load of gray data.. telling me I've not translated properly..somehow

I will update the post above with latest

but the crux is now how to get the buffer properly decoded..


1
2
3
4
5
6
7
8
9
10
11
12
std::cout << "[IMAGE_WIDTH]  = " << std::to_string(IMAGE_WIDTH) << std::endl;
std::cout << "[IMAGE_HEIGHT] = " << std::to_string(IMAGE_HEIGHT) << std::endl;
std::cout << "image bytes  = " << std::to_string(header_data.image_size_bytes) << std::endl;
std::cout << "secondbuffer data " << std::to_string(second_receive_buffer.data().size()) << std::endl;
                
void* matrix_ready_data = const_cast<void*>(boost::asio::buffer_cast<const void*>(second_receive_buffer.data()));
Mat img = cv::Mat(IMAGE_HEIGHT, IMAGE_WIDTH, CV_8UC3, matrix_ready_data);
//img = img.reshape(0,IMAGE_HEIGHT);
//Mat img2 = cv::imdecode(img, cv::LOAD_IMAGE_UNCHANGED);

imshow("client", img);
waitKey(5000);


this will just show black data
Last edited on
make sure that you are not serializing a pointer, but sending actual data across. I don't see anything wrong, but I don't know this library either.
Topic archived. No new replies allowed.