jester
jester

Reputation: 238

reading an opencv image in python through a socket

I am trying to read an opencv image in a python socket that is sent from c++.

I am able to read the image into another c++ program or VB program and build an image but with python I don't understand what's happening.

The sending code where I send the mat.data:

char *start_s = "<S><size>43434234<cols>64<rows>64<SE>";//plus I send the image size, cols, rows, which varies, not like the static char string shown
char *end_e = "<E>";
cv::Mat image_send = some_mat;
iResult = send( ConnectSocket, start_s, (int)strlen(start_s), 0 );
iResult = send( ConnectSocket, (const char *) image_send.data, i_buffer_size, 0 );
iResult = send( ConnectSocket, end_e, (int)strlen(end_e), 0 );

This is what I have tried with the python, but haven't had any success yet. The image_cols and Image_rows are filtered from the socket, not shown here, and only the image_mat.data from the c++ mat is in the socket that I am trying to put into the image:

data = conn.recv(4757560)
        if(i_Read_Image == 2) & (image_cols != 0) & (image_rows != 0):
            print ("Entering")
            #print(data)
            data2 = np.fromstring(data, dtype='uint8')
            img_np = cv2.imdecode(data2,cv2.IMREAD_COLOR )
            cv2.imshow('image',img_np)
            cv2.waitKey(0)

            #Also tried this
            #img = Image.new('RGB', (image_cols, image_rows))
            #img.putdata(data)

        #img5 = np.reshape(data2,(image_rows,image_cols))
        i_Read_Image = 0

Upvotes: 2

Views: 1467

Answers (1)

jester
jester

Reputation: 238

With the help of the comments I was able to get a working answer. The original image is in a single array RGB, this needs to be reshaped and placed into a 'RGB' image, it can be done in one line:

img = Image.fromarray(data2.reshape(image_rows,image_cols,3), 'RGB')

and when reading an opencv data array from a socket: this works:

data = conn.recv(567667)
if(i_Read_Image == 2) & (image_cols != 0) & (image_rows != 0):
    data2 = np.fromstring(data, dtype='uint8')
    img = Image.fromarray(data2.reshape(image_rows,image_cols,3), 'RGB')
    img.show()

Upvotes: 1

Related Questions