socketdata to image

asked 2016-07-12 14:27:42 -0500

kauv gravatar image

updated 2016-07-12 14:30:18 -0500

LBerger gravatar image

Hi

I've implemented a client server app which receives an image on the c++ server. The data is fetched and has to be saved as an image but the problem the server keeps on throwing segmentation fault 11.

uint32_t len;
int lengthbytes = 0;
lengthbytes = recv(newsockfd, (char *)&len, sizeof(len), 0);
printf("%d",len);
int bytes=0;
uchar sockData[len];
for (int i = 0; i < len; i += bytes)
{
    if ((bytes = recv(newsockfd, sockData +i, len  - i, 0)) == -1)
    {
        error("recv failed");
    }

}
Mat img(690,250,CV_8UC3,sockData);
imwrite("/Users/koushikv/Desktop/openCVSampleProject/datasent.png", img);

I think the main problem occurs in the Mat img (690,250,CV_8uC3,sockData) since i've given an arbitary rows, cols and type CV_8uc3. I don't know how to create a image from the socketdata without these parameters .Can someone please help me with this issue? To be clear, i need to convert the uchar socketdata to mat and save the image.

Thank you

edit retag flag offensive close merge delete

Comments

1

send first rows and cols in your socket

LBerger gravatar imageLBerger ( 2016-07-12 14:32:26 -0500 )edit

btw, imho you want Mat img(250,690,CV_8UC3,sockData); (rows,cols,type,data)

berak gravatar imageberak ( 2016-07-13 00:37:18 -0500 )edit

yep i tried sending rows and cols through the socket and also hardcoding the rows and cols value on the server side.It didn't work .I also tried with different types such as CV_8uc1,CV_8uc2,cv_8uc4.It still results in segmentation fault 11

kauv gravatar imagekauv ( 2016-07-13 03:33:38 -0500 )edit