Almost working client server BOOST ASIO socket backed opencv

I have posts in the usual places… but figured now my issue is pretty much just understanding opencv…

I have a Server on a machine which will take a screenshot and send it when requested oversocket to a Client which requests it… I send first a header message with dimenions, amount of image bytes to next grab… so that I get exactly the frame needed next… eventually I want to turn it into a sort of webcam feed but for now just… hard coded sending a single image

anyway, when I send the image data over the wire everything seems fine… but when I decode the image it ends up mostly black, and a bit of gray at the top… this tells me I must be decoding the JPG incorrectly… so any help understanding would be appreciated>

I’ve tried various techniques but… nothing quite clicking yet… I’m thinking I perhaps need to take the buffer data… place itinto a shape of 1, LENGTH_OF_BUFFER and reshape it… but that also crashes

Server.cpp series of events


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

//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 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);

Client.cpp

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

thank you so much

https://www.cplusplus.com/forum/beginner/280846/

I got it working

Server

#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(1);
        }   
    }
}

cv::Mat retrieve_data(){

    std::string image_path = "/YOUR_IMAGE.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<std::uint8_t> buff;
            cv::imencode(".jpg", frame, buff, param);

            // now we send the header message
            std:: string image_dimensions = "6016Wx3384H";
            std:: string image_buff_bytes = std::to_string(buff.size());
            std::string message_header = image_dimensions + "," +  image_buff_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';

            socket.write_some(boost::asio::buffer(message_header), ignored_error);

            socket.write_some(boost::asio::buffer(buff), ignored_error);

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

    return 0; 
}

Client

#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;
            std::vector<std::uint8_t> buff(header_data.image_size_bytes);
            boost::asio::read(socket, boost::asio::buffer(buff), 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;

                cv::Mat img(cv::imdecode(buff, cv::IMREAD_ANYCOLOR));

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