gpt4 book ai didi

python - 在 C++ 中实现 python np.fromstring()

转载 作者:行者123 更新时间:2023-11-28 04:22:58 24 4
gpt4 key购买 nike

我正在尝试将我的 Python 代码转换为 C++(以加快速度)。

我的代码通过套接字接收图像并显示它。

Python代码:

            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)

不幸的是,我遇到了 C++ 中“np.fromstring”的问题。

如何实现?

我正在尝试这个:

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

}

但这不起作用(我认为这是由于“cv2.imdecode”和“np.fromstring”造成的)。

请帮帮我

P.S 一般来说,我的主要问题恰好在 np.fromstring,因为我从 socket 得到一个字符串,而不是一些字节或整数,我应该将一个字符串转换为像素数组(每个 0-255)

最佳答案

感谢所有评论。我找到了解决方案:

    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 当然,还是要搞出for循环。

关于python - 在 C++ 中实现 python np.fromstring(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55028198/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com