I want to implement the program that can communicate multiple images via ZeroMQ. I can display multiple images on C++ by using OpenCV but I cannot display on Python somehow although the compiler didn't output errors.
How can I communicate multiple images from C++ to Python?
C++ Code:
Mat img0;
Mat img1;
Mat img2;
img0 = imread("images.jpeg");
img1 = imread("images1.jpeg");
img2 = imread("image2.jpg");
if(img0.empty()) return -1;
if(img1.empty()) return -1;
if(img2.empty()) return -1;
ShowManyImages("IMAGE",3,img0,img1,img2);
// Let structure for zeroMQ
int32_t info[3];
info[0] = (int32_t)img0.rows;
info[1] = (int32_t)img0.cols;
info[2] = (int32_t)img0.type();
info[3] = (int32_t)img1.rows;
info[4] = (int32_t)img1.cols;
info[5] = (int32_t)img1.type();
info[6] = (int32_t)img2.rows;
info[7] = (int32_t)img2.cols;
info[8] = (int32_t)img2.type();
zmq::context_t context (1);
zmq::socket_t socket (context, ZMQ_REQ);
socket.connect ("tcp://localhost:5555");
for(i=0; i<9; i++ )
{
zmq::message_t msg0 ( (void*)&info[i], sizeof(int32_t), NULL );
socket.send(msg0, ZMQ_SNDMORE);
}
void* data0 = malloc(img0.total() * img0.elemSize());
memcpy(data0, img0.data, img0.total() * img0.elemSize());
zmq::message_t msg00(data0, img0.total() * img0.elemSize(), my_free, NULL);
socket.send(msg00);
void* data1 = malloc(img1.total() * img1.elemSize());
memcpy(data1, img1.data, img1.total() * img1.elemSize());
zmq::message_t msg11(data1, img1.total() * img1.elemSize(), my_free, NULL);
socket.send(msg11);
void* data2 = malloc(img2.total() * img2.elemSize());
memcpy(data2, img2.data, img2.total() * img2.elemSize());
zmq::message_t msg22(data2, img2.total() * img2.elemSize(), my_free, NULL);
socket.send(msg22);
Python Code:
import zmq
import cv2
import struct
import numpy as np
# Connection String
conn_str = "tcp://*:5555"
# Open ZMQ Connection
ctx = zmq.Context()
sock = ctx.socket(zmq.REP)
sock.bind(conn_str)
while(True):
# Receve Data from C++ Program
byte_rows, byte_cols, byte_mat_type, data= sock.recv_multipart()
# Convert byte to integer
rows = struct.unpack('i', byte_rows)
cols = struct.unpack('i', byte_cols)
mat_type = struct.unpack('i', byte_mat_type)
if mat_type[0] == 0:
# Gray Scale
image = np.frombuffer(data, dtype=np.uint8).reshape((rows[0],cols[0]))
else:
# BGR Color
image = np.frombuffer(data, dtype=np.uint8).reshape((rows[0],cols[0],3))
cv2.imshow('Python',image)
cv2.waitKey()
Sincerely,