簡體   English   中英

將 OpenCV imread 和 imwrite 與 Python Path 對象一起使用會給出 SystemError:<built-in function imread> 返回 NULL 沒有設置錯誤</built-in>

[英]Using OpenCV imread and imwrite with Python Path objects gives SystemError: <built-in function imread> returned NULL without setting an error

將 Python 路徑 object 傳遞給 OpenCV 的imreadimwrite會導致未定義的錯誤:

from pathlib import Path
import cv2

img_path = Path("test.png")
img = cv2.imread(img_path)

結果是:

Traceback (most recent call last):
  File ".\secondary_image_generation.py", line 36, in <module>
    img = cv2.imread(img_path)
SystemError: <built-in function imread> returned NULL without setting an error

為什么會這樣,我該如何避免?

OpenCV 庫源代碼是在 C++ 中編寫的,而 Python 綁定主要是自動生成的,除了包裝 C++ 函數外沒有做更多的事情。 C++ 函數需要字符串類型的文件名,因此這也是您必須提供給 Python 函數的內容。

執行以下操作可通過解析完整路徑並將其轉換為字符串來解決問題:

img_path = Path("test.png")
img = cv2.imread(str(img_path.resolve()))

這仍然是當前版本 OpenCV 的開放功能請求

暫無
暫無

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

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