如何在python opencv中从数组中读取原始png?
我正在通过tcp将png图像从iPhone传输到MacBook。MacBook代码来自。如何将图像转换为与OpenCV一起使用?之所以选择png是因为它们很有效,但也可以使用其他格式 我编写了一个测试程序,从文件中读取原始图像,但不确定如何转换:如何在python opencv中从数组中读取原始png?,python,opencv,png,Python,Opencv,Png,我正在通过tcp将png图像从iPhone传输到MacBook。MacBook代码来自。如何将图像转换为与OpenCV一起使用?之所以选择png是因为它们很有效,但也可以使用其他格式 我编写了一个测试程序,从文件中读取原始图像,但不确定如何转换: # Read rawImage from a file, but in reality will have it from TCPServer f = open('frame.png', "rb") rawImage = f.read() f.clos
# Read rawImage from a file, but in reality will have it from TCPServer
f = open('frame.png', "rb")
rawImage = f.read()
f.close()
# Not sure how to convert rawImage
npImage = np.array(rawImage)
matImage = cv2.imdecode(rawImage, 1)
#show it
cv.NamedWindow('display')
cv.MoveWindow('display', 10, 10)
cv.ShowImage('display', matImage)
cv. WaitKey(0)
(您的问题似乎被标记为objective-c,但您需要Python,您的示例也是如此,所以我将使用它。)
我的第一篇关于堆栈溢出的帖子
cv.LoadImageM方法似乎就是您想要的
示例用法:
LoadImage(文件名,iscolor=CV\u LOAD\u IMAGE\u COLOR)→ 没有
函数cvLoadImage从指定的文件和
返回指向加载图像的指针。目前,您正在创建以下文件
支持以下格式:
Windows bitmaps - BMP, DIB
JPEG files - JPEG, JPG, JPE
Portable Network Graphics - PNG
Portable image format - PBM, PGM, PPM
Sun rasters - SR, RAS
TIFF files - TIFF, TIF
注意,在当前的实现中,alpha通道(如果有)是
从输出图像中剥离,例如,将显示4通道RGBA图像
加载为RGB
我想出来了:
# Read rawImage from a file, but in reality will have it from TCPServer
f = open('frame.png', "rb")
rawImage = f.read()
f.close()
# Convert rawImage to Mat
pilImage = Image.open(StringIO(rawImage));
npImage = np.array(pilImage)
matImage = cv.fromarray(npImage)
#show it
cv.NamedWindow('display')
cv.MoveWindow('display', 10, 10)
cv.ShowImage('display', matImage)
cv. WaitKey(0)
另一方面,
在读取实际文件的情况下,这也适用于unicode路径(在windows上测试)
@Andy Rosenblum的作品,如果使用过时的cv python API(与cv2相比),这可能是最好的解决方案 然而,因为这个问题对于最新版本的用户来说同样有趣,所以我建议以下解决方案。以下示例代码可能优于公认的解决方案,因为:
import cv2
import numpy as np
#read the data from the file
with open(somefile, 'rb') as infile:
buf = infile.read()
#use numpy to construct an array from the bytes
x = np.fromstring(buf, dtype='uint8')
#decode the array into an image
img = cv2.imdecode(x, cv2.IMREAD_UNCHANGED)
#show it
cv2.imshow("some window", img)
cv2.waitKey(0)
请注意,在opencv 3.0中,各种常量/标志的命名约定已更改,因此如果使用opencv 2.x,则需要将标志cv2.IMREAD_更改为未更改。此代码示例还假设您正在加载标准的8位图像,但如果不是,则可以使用np.fromstring中的dtype='…'标志。当您必须从文件加载时,此简单解决方案可以完成此任务(使用
opencv-python-3.2.0.6
进行测试):
这对我来说很有用(现在):
导入cv2
将numpy作为np导入
数据=打开('016e263c726a.raw')。读取()
x=np.frombuffer(数据,dtype='uint8')。重塑(20482448)
cv2.imshow('x',x);cv2.waitKey();cv2.destroyAllWindows()
但是它会读取未经任何特定格式保存的原始图像。您发布的代码是否有问题?看起来一切正常。问题是png已经从tcp加载到内存中。LoadImageM要求保存到文件,然后加载该文件。这似乎是个好主意。很抱歉与标签混淆。所有这些链接都已断开:(可能是因为我使用的OpenCV版本比用于测试上述代码段的版本更新,但我必须将
cv2.CV\u LOAD\u IMAGE\u UNCHANGED
标志更改为cv2.IMREAD\u UNCHANGED
。这个答案应该是镀金的。在使用二进制缓冲区时,最好使用np.frombuffer
。实际上,请将数据类型更改为f numpy数组应始终为uint8
,因为您只是在创建openCV可以处理的字节对象。实际图像的数据类型的更改由cv2.imdecode处理。
with open(image_full_path, 'rb') as img_stream:
file_bytes = numpy.asarray(bytearray(img_stream.read()), dtype=numpy.uint8)
img_data_ndarray = cv2.imdecode(file_bytes, cv2.CV_LOAD_IMAGE_UNCHANGED)
img_data_cvmat = cv.fromarray(img_data_ndarray) # convert to old cvmat if needed
import cv2
import numpy as np
#read the data from the file
with open(somefile, 'rb') as infile:
buf = infile.read()
#use numpy to construct an array from the bytes
x = np.fromstring(buf, dtype='uint8')
#decode the array into an image
img = cv2.imdecode(x, cv2.IMREAD_UNCHANGED)
#show it
cv2.imshow("some window", img)
cv2.waitKey(0)
import cv2
img = cv2.imread(somefile)