本文介紹了在 Kivy 中顯示 numpy/opencv/matplotlib 圖像的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
如何在 Kivy 中以標準 numpy/opencv/matplotlib 格式顯示圖像?Kivy 使用不同的圖像內存布局,我不知道是哪一個.
How to display image in standard numpy/opencv/matplotlib format in Kivy? Kivy uses different image memory layout and I can't figure out which one.
以下代碼完全正常.使用 cv2 VideoCapture 捕獲圖像.我認為圖像是 BGR,數組維度是 (360, 480, 3):
The following code works totally fine. Image was captured using cv2 VideoCapture. I think image is BGR, array dimensions are (360, 480, 3):
ret, image = video_capture.read()
cv2.imshow('image', image)
cv2.waitKey()
嘗試使用以下代碼顯示它會產生混亂的結果:
Trying to display it with the following code produce messy results:
video_texture = Texture.create(size=image.shape[:2])
video_texture.blit_buffer(image.tostring(), colorfmt='rgb', bufferfmt='ubyte')
# ...
video_panel = self.ids['video_panel']
with video_panel.canvas:
Rectangle(texture=video_texture, pos=video_panel.pos, size=video_panel.size)
推薦答案
找到了正確的轉換.可能不是最理想的:
Found the right transformation. Probably suboptimal:
ret, image = video_capture.read()
image = np.rot90(np.swapaxes(image, 0, 1))
video_texture = Texture.create(size=(image.shape[1], image.shape[0]), colorfmt='rgb')
video_texture.blit_buffer(image.tostring(), colorfmt='bgr', bufferfmt='ubyte')
這篇關于在 Kivy 中顯示 numpy/opencv/matplotlib 圖像的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!