簡體   English   中英

將 dtype (uint8) 的 Numpy ndarray 轉換為 OpenCV 可讀圖像

[英]Convert Numpy ndarray of dtype (uint8) to OpenCV readable image

長話短說 - 我正在學習如何使用這個D3DShot (Windows 桌面復制 API 的 Python 實現)庫進行簡單的屏幕捕獲,並將捕獲的數據直接傳遞給OpenCV

捕獲的數據是np.ndarraydtype uint8與值范圍(0, 255) 我已經在 Stack Overflow 和其他網站上嘗試了多種建議,但無法解決這個問題並不斷遇到錯誤。

這是我當前的代碼:

import d3dshot
import cv2

# D3D init
d = d3dshot.create(capture_output="numpy", frame_buffer_size=60)

# Choose display
for i, display in enumerate(d.displays):
    current_display = display
    print('[' + str(i+1) + '] ' + display.name + '\n')

# Set display
current_display.i = int(input("Select display by nr.: ")) - 1
d.display = d.displays[current_display.i]
print('\nYou\'ve selected [' + str(current_display.i) + '] ' + current_display.name)

# Start capturing
d.capture(target_fps=10,)

# Send frame to opencv
while True:
    #Displayed the image
    img = d.get_latest_frame()

    #Dump it like it's hot
    print(img.shape, img.dtype)
    """
    Here we will convert from np.ndarray of dtype uint8 to opencv supported img
    """
    # Pass img to Open CV
    cv2.imshow("Test Window", img)
    cv2.waitKey(0)

轉儲返回:

Traceback (most recent call last):
  File ".\capture.py", line 26, in <module>
    print(img.shape, img.dtype)
AttributeError: 'NoneType' object has no attribute 'shape'

我當然問題是如何從轉換np.ndarraydtype uint8到OpenCV的支持的圖像?

所以原因很簡單.. D3DShot 捕獲需要一些延遲(我將其設置為100毫秒)來初始化,並且數組最終不為空並成功傳遞給 OpenCV。 這是更新后的代碼:

import d3dshot
import cv2
import time

# D3D init
d = d3dshot.create(capture_output="numpy", frame_buffer_size=60)

# Choose display
for i, display in enumerate(d.displays):
    current_display = display
    print('[' + str(i+1) + '] ' + display.name + '\n')

# Set display
current_display.i = int(input("Select display by nr.: ")) - 1
d.display = d.displays[current_display.i]
print('\nYou\'ve selected [' + str(current_display.i) + '] ' + current_display.name)

# Start capturing
d.capture(target_fps=60)
time.sleep(0.1)

# Send frame to opencv
while True:
    #Displayed the image
    img = d.get_latest_frame()

    #Dump it like it's hot
    print(img.shape, img.dtype)

    # Send to Open CV
    cv2.imshow("Test Window", img)
    cv2.waitKey(1)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM