简体   繁体   English

从一个十六进制字符串构造一个 Numpy 数组

[英]Construct a Numpy array from a hexadecimal string

I have a hexadecimal string "89-50-4E-47-0D-0A-1A-0A-00-00-00-0D-49" to be specific this will contain the data of an image.我有一个十六进制字符串"89-50-4E-47-0D-0A-1A-0A-00-00-00-0D-49" ,具体来说这将包含图像的数据。

I want to convert it to a Numpy array or possibly reconstruct an OpenCV image from the said data.我想将其转换为 Numpy 数组或可能从所述数据重建 OpenCV 图像。

The width and height will also be supplied so the dimensions of the Numpy array is known.还将提供宽度和高度,以便知道 Numpy 数组的尺寸。

How can I construct a Numpy array from the above string?如何从上述字符串构造一个 Numpy 数组?

We could use np.fromiter , and cast the individual strings to hexadecimal, using the base argument in int , and then to integer type using the dtype argument in np.fromiter :我们可以使用np.fromiter ,投个体线为十六进制,使用base的说法int使用,然后到整数类型dtype中的说法np.fromiter

s = "89-50-4E-47-0D-0A-1A-0A-00-00-00-0D-49"

np.fromiter((int(x, 16) for x in s.split('-')), dtype=np.int32)
# array([137,  80,  78,  71,  13,  10,  26,  10,   0,   0,   0,  13,  73])

You can use list comprehension and the built-in int module to convert from hexadecimal to decimal the splitted string:您可以使用列表推导和内置的int模块将拆分后的字符串从十六进制转换为十进制:

import numpy as np

hex_string = '89-50-4E-47-0D-0A-1A-0A-00-00-00-0D-49'
np.array([int(x, base=16) for x in hex_string.split('-')])

You can split the string by dash and convert individual base-16 numbers into int .您可以通过破折号split字符串并将单个 base-16 数字转换为int

>>> import numpy as np
>>> hext_str = "89-50-4E-47-0D-0A-1A-0A-00-00-00-0D-49"
>>> np.array([int(x, 16) for x in hex_str.split("-")])
array([137,  80,  78,  71,  13,  10,  26,  10,   0,   0,   0,  13,  73])

Provided that (n, m) are dimensions of your image you can use on the result the .reshape((n, m)) method of np.array .假设(n, m)是图像的尺寸可以在结果上使用.reshape((n, m))的方法np.array

import numpy as np arr = np.array([int(x, 16) for x in "89-50-4E-47-0D-0A-1A-0A-00-00-00-0D-49".split("-")]) print(arr)

If memory efficiency is of concern two digits in a hexadecimal number corresponds to an unsigned 8-bit integer (ie, numbers between 0 and 255).如果关注内存效率,则十六进制数中的两位数字对应于一个无符号的 8 位整数(即 0 到 255 之间的数字)。

To return to the original string you can use format(number, '02x') (zero padded 2-length hexadecimal number string)要返回原始字符串,您可以使用format(number, '02x') (零填充的 2 长度十六进制数字字符串)

hex_str = "89-50-4E-47-0D-0A-1A-0A-00-00-00-0D-49"
arr = np.fromiter((int(x, 16) for x in hex_str.split('-')), dtype=np.uint8)
# array([137,  80,  78,  71,  13,  10,  26,  10,   0,   0,   0,  13,  73],
      dtype=uint8)

The array would only take up 13 bytes of space, as opposed to the default array type for integers (np.int64) which would take up 104 bytes.该数组仅占用 13 个字节的空间,而整数的默认数组类型 (np.int64) 将占用 104 个字节。

Once could return it to its original string form in the following way:曾经可以通过以下方式将其恢复为原始字符串形式:

hex_str = '-'.join((format(x, '02x') for x in arr)).upper()

使用 ctypes 从 C 结构构造 numpy 数组。 值错误:' <p' is not a valid pep 3118 buffer format string< div><div id="text_translate"><p> 考虑以下代码:</p><p> by_ref.h</p><pre> typedef struct OutPutImage{ double *** output_img; int nb_images; int nb_cols; int nb_rows; }opi_; void test_output_images(struct OutPutImage * out, int nb_images, int nb_cols, int nb_rows);</pre><p> by_ref.c</p><pre> #include &lt;stdlib.h&gt; #include "by_ref.h" void test_output_images(struct OutPutImage* out, int nb_images, int nb_cols, int nb_rows){ out-&gt;nb_images = nb_images; out-&gt;nb_cols = nb_cols; out-&gt;nb_rows = nb_rows; out-&gt;output_img = (double***)malloc((nb_images) * sizeof(double**)); for(int i = 0; i &lt; nb_images; i++){ out-&gt;output_img[i] = (double**)malloc((nb_cols) * sizeof(double*)); for(int j = 0; j &lt; nb_cols; j++){ out-&gt;output_img[i][j] = (double*)malloc((nb_rows) * sizeof(double)); for(int k = 0; k &lt; nb_rows; k++){ out-&gt;output_img[i][j][k] = 0; } } } }</pre><p> 和</p><p> by_ref.py</p><pre> import ctypes import numpy.ctypeslib as npct import numpy as np class OutPutImage(ctypes.Structure): _fields_ = [('output_img', npct.ndpointer(dtype=np.double, ndim=3)), ('nb_images', ctypes.c_int), ('nb_cols', ctypes.c_int), ('nb_rows', ctypes.c_int)] _libc = ctypes.CDLL("./by_ref.so") def __init__(self, nb_images=None, nb_cols=None, nb_rows=None): self.nb_images = nb_images self.nb_cols = nb_cols self.nb_rows = nb_rows if __name__ == '__main__': libc_adm = ctypes.CDLL("./by_ref.so") libc_adm.test_output_images.restype = ctypes.c_int libc_adm.test_output_images.argtypes = [ctypes.POINTER(OutPutImage), ctypes.c_int, ctypes.c_int, ctypes.c_int] output_image = OutPutImage(1, 2, 3) libc_adm.test_output_images(ctypes.byref(output_image), 4, 5, 6) print(np.array(output_image.output_img, dtype=np.float)) # error ocures here</pre><p> 当我使用以下 Makefile 制作并运行此代码时</p><pre>by_ref: by_ref.so python by_ref.py by_ref.so: by_ref.o gcc -shared -o by_ref.so by_ref.o by_ref.o: by_ref.c gcc -c -Wall -fpic by_ref.c -o by_ref.o</pre><p> 我得到错误:</p><pre> Traceback (most recent call last): File "by_ref.py", line 46, in &lt;module&gt; print(np.array(output_image.output_img, dtype=np.float)) ValueError: '&lt;P' is not a valid PEP 3118 buffer format string make: *** [Makefile:2: by_ref] Error 1</pre><p> 我确信test_output_images正在做它应该做的事情。 但是我无法从结构中的数据构建 numpy 数组。 我该如何做到这一点? 另外,我什么时候释放 memory? 谢谢。</p><p> <strong>编辑:</strong></p><p> 如果我使用np.ctypeslib.as_array(output_image.output_img)我得到同样的错误:</p><pre> ValueError: '&lt;P' is not a valid PEP 3118 buffer format string</pre><p> <strong>更新:</strong></p><p> 如果我使用x = np.ctypeslib.as_array(( ctypes.c_double*array_length ).from_address( ctypes.addressof(output_image.output_img) )) ,其中array_length=nb_images*nb_cols*nb_rows ,那么我避免了上述错误,但是新数组 x 包含垃圾,无法重塑。</p></div></p'> - Construct a numpy array from a C structure using ctypes. ValueError: '<P' is not a valid PEP 3118 buffer format string

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从文件构造结构化的numpy数组? - Construct structured numpy array from a file? 如何从 numpy 数组构造一个 ndarray? python - How to construct a ndarray from a numpy array? python 正确地从十六进制字符串转换为字节数组 - Correctly going from a Hexadecimal string to a byte array 从不同大小的较小数组构造单个numpy数组 - Construct single numpy array from smaller arrays of different sizes 如何从一组现有数组构造一个新的numpy数组? - How to construct a new numpy array from a set of existing arrays? 如何根据ID对齐的数据从多个向量构造一个numpy数组 - How to construct a numpy array from multiple vectors with data aligned by id 如何从大量数据中有效构造一个numpy数组? - How to efficiently construct a numpy array from a large set of data? 使用 ctypes 从 C 结构构造 numpy 数组。 值错误:' <p' is not a valid pep 3118 buffer format string< div><div id="text_translate"><p> 考虑以下代码:</p><p> by_ref.h</p><pre> typedef struct OutPutImage{ double *** output_img; int nb_images; int nb_cols; int nb_rows; }opi_; void test_output_images(struct OutPutImage * out, int nb_images, int nb_cols, int nb_rows);</pre><p> by_ref.c</p><pre> #include &lt;stdlib.h&gt; #include "by_ref.h" void test_output_images(struct OutPutImage* out, int nb_images, int nb_cols, int nb_rows){ out-&gt;nb_images = nb_images; out-&gt;nb_cols = nb_cols; out-&gt;nb_rows = nb_rows; out-&gt;output_img = (double***)malloc((nb_images) * sizeof(double**)); for(int i = 0; i &lt; nb_images; i++){ out-&gt;output_img[i] = (double**)malloc((nb_cols) * sizeof(double*)); for(int j = 0; j &lt; nb_cols; j++){ out-&gt;output_img[i][j] = (double*)malloc((nb_rows) * sizeof(double)); for(int k = 0; k &lt; nb_rows; k++){ out-&gt;output_img[i][j][k] = 0; } } } }</pre><p> 和</p><p> by_ref.py</p><pre> import ctypes import numpy.ctypeslib as npct import numpy as np class OutPutImage(ctypes.Structure): _fields_ = [('output_img', npct.ndpointer(dtype=np.double, ndim=3)), ('nb_images', ctypes.c_int), ('nb_cols', ctypes.c_int), ('nb_rows', ctypes.c_int)] _libc = ctypes.CDLL("./by_ref.so") def __init__(self, nb_images=None, nb_cols=None, nb_rows=None): self.nb_images = nb_images self.nb_cols = nb_cols self.nb_rows = nb_rows if __name__ == '__main__': libc_adm = ctypes.CDLL("./by_ref.so") libc_adm.test_output_images.restype = ctypes.c_int libc_adm.test_output_images.argtypes = [ctypes.POINTER(OutPutImage), ctypes.c_int, ctypes.c_int, ctypes.c_int] output_image = OutPutImage(1, 2, 3) libc_adm.test_output_images(ctypes.byref(output_image), 4, 5, 6) print(np.array(output_image.output_img, dtype=np.float)) # error ocures here</pre><p> 当我使用以下 Makefile 制作并运行此代码时</p><pre>by_ref: by_ref.so python by_ref.py by_ref.so: by_ref.o gcc -shared -o by_ref.so by_ref.o by_ref.o: by_ref.c gcc -c -Wall -fpic by_ref.c -o by_ref.o</pre><p> 我得到错误:</p><pre> Traceback (most recent call last): File "by_ref.py", line 46, in &lt;module&gt; print(np.array(output_image.output_img, dtype=np.float)) ValueError: '&lt;P' is not a valid PEP 3118 buffer format string make: *** [Makefile:2: by_ref] Error 1</pre><p> 我确信test_output_images正在做它应该做的事情。 但是我无法从结构中的数据构建 numpy 数组。 我该如何做到这一点? 另外,我什么时候释放 memory? 谢谢。</p><p> <strong>编辑:</strong></p><p> 如果我使用np.ctypeslib.as_array(output_image.output_img)我得到同样的错误:</p><pre> ValueError: '&lt;P' is not a valid PEP 3118 buffer format string</pre><p> <strong>更新:</strong></p><p> 如果我使用x = np.ctypeslib.as_array(( ctypes.c_double*array_length ).from_address( ctypes.addressof(output_image.output_img) )) ,其中array_length=nb_images*nb_cols*nb_rows ,那么我避免了上述错误,但是新数组 x 包含垃圾,无法重塑。</p></div></p'> - Construct a numpy array from a C structure using ctypes. ValueError: '<P' is not a valid PEP 3118 buffer format string 用pytables构造巨大的numpy数组 - Construct huge numpy array with pytables 在numpy中构造这个数组的有效方法? - Efficient way to construct this array in numpy?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM