【问题标题】:Opencv Python display raw imageOpencv Python显示原始图像
【发布时间】:2013-09-12 00:47:21
【问题描述】:

我不知道如何显示包含 640x480 像素信息的原始图像,每个像素为 8 位。 (灰色图像)

我需要从 np 数组转换为 Mat 格式才能显示图像。

#!/usr/bin/python
import numpy as np
import cv2
import sys
# Load image as string from file/database    
fd = open('flight0000.raw')
img_str = fd.read()
fd.close()

img_array = np.asarray(bytearray(img_str), dtype=np.uint8)

img = ... Conversion to Mat graycolor

cv2.imshow('rawgrayimage', img)
cv2.waitKey(0)

它与 cv、cv2 如此混淆。我已经尝试了一段时间,但我找不到解决方案。

【问题讨论】:

    标签: python image opencv numpy type-conversion


    【解决方案1】:

    .RAW 文件在 OpenCV see imread 中不受支持,

    但是文件可以用Python打开,用Numpy解析

    import numpy as np
    fd = open('flight0000.raw', 'rb')
    rows = 480
    cols = 640
    f = np.fromfile(fd, dtype=np.uint8,count=rows*cols)
    im = f.reshape((rows, cols)) #notice row, column format
    fd.close()
    

    这样就形成了一个可以被 OpenCV 直接操作的 numpy 数组

    import cv2
    cv2.imshow('', im)
    cv2.waitKey()
    cv2.destroyAllWindows()
    

    【讨论】:

    • 注意:我的示例解析灰度图像 - 为彩色图像修改 count 参数并相应地重塑函数
    【解决方案2】:
    #!/usr/bin/python
    #code to display a picture in a window using cv2
    import cv2
    
    cv2.namedWindow('picture',cv2.WINDOW_AUTOSIZE)
    frame=cv2.imread("abc.jpg")
    cv2.imshow('picture',frame)
    if cv2.waitKey(0) == 27:
            cv2.destroyAllWindows()
    

    【讨论】:

      【解决方案3】:

      如果您想将“原始”图像保存为“png”文件,这只是一个示例 (每个像素32位,彩色图像):

      import numpy as np
      import matplotlib.pyplot as plt
      
      img = np.fromfile("yourImage.raw", dtype=np.uint32)
      print img.size #check your image size, say 1048576
      #shape it accordingly, that is, 1048576=1024*1024
      img.shape = (1024, 1024)
      
      plt.imshow(img)
      plt.savefig("yourNewImage.png")
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-10-17
        • 1970-01-01
        • 1970-01-01
        • 2013-02-10
        • 1970-01-01
        • 2015-12-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多