0

Im trying to translate my Python code to C++ (for speed up).

My code receive the image by socket and just show it.

Python code:

            self._img = cv2.imdecode(np.fromstring(self._SocketData, np.uint8), 1)
            if not self._img is None:
                self._img = cv2.resize(self._img, (1280, 720))
                cv2.imshow("1", self._img)
                cv2.waitKey(1)

Unfortunately, i get problem with "np.fromstring" in C++.

How to implement that?

Im trying this:

while (ignored_error != boost::asio::error::eof) {

    boost::array<uchar, 10000> RECV_DATA;

    size_t ImageSize = image_recver.read_some(
        boost::asio::buffer(RECV_DATA), ignored_error);

    vector<uchar> Img (ImageSize);

    for (int i = 0; i < ImageSize; i++) {
        Img[i] = RECV_DATA[i];
    }

    Mat img(1280, 720, CV_64F, Img.data());

    imshow("1", img);
    waitKey(1);

}

But this is not working (I think this is due with "cv2.imdecode" and "np.fromstring").

Please, help me

P.S Generally, my main problem exactly in np.fromstring, because from socket im get a string, not a some bytes or integers and i should transform a string to array of pixels from (0-255 each)

BotMan
  • 37
  • 5
  • 10000 byte buffer is probably not enough to fit the whole image. You don't decode the image data. And for some odd reason you create the image as containing 64bit floating point values. – Dan Mašek Mar 06 '19 at 16:51
  • @DanMašek, thanks for comment. My image size is 50x50, i think, 10000 bytes is enough – BotMan Mar 06 '19 at 16:55
  • @BotMan `CV_64F` is for _double_, see [How to access pixel values of CV_32F/CV_64F Mat?](https://stackoverflow.com/questions/15130162/how-to-access-pixel-values-of-cv-32f-cv-64f-mat) – bruno Mar 06 '19 at 16:56
  • @BotMan why `1280, 720` then if 50x50 ? – bruno Mar 06 '19 at 16:56
  • @bruno, because 1280x720 looks better on the screen, then 50x50 :D. 50x50 - only for speed optimization – BotMan Mar 06 '19 at 17:03
  • @BotMan sure ^^ but I was speaking about the memory size, 1280*720 is 921600 while 10000 byte is 80000 bits and less than one bit for a pixel is ... (but may be the error is in `read_some` so before) – bruno Mar 06 '19 at 17:07
  • 1
    @bruno, hmm.. In python all works perfectly, with the same size of receive array. Anyway, im found the solution. – BotMan Mar 06 '19 at 17:12

1 Answers1

1

Thanks for all comments. I found the solution:

    while (ignored_error != boost::asio::error::eof) {

        boost::array<uchar, 10000> RECV_DATA;

        size_t ImageSize = image_recver.read_some(
            boost::asio::buffer(RECV_DATA), ignored_error);

        vector<uchar> Img (ImageSize);

        for (int i = 0; i < ImageSize; i++) {
            Img[i] = RECV_DATA[i];
        }

        Mat img_img = imdecode(Img, 1);

        imshow("1", img_img);
        waitKey(1);

    }

P.S Of course, still need to come up with for loop.

BotMan
  • 37
  • 5