我是 OpenCV 的初學者,是一個試圖創建 Android 應用程式的 kivy。
我的應用程式應該顯示通過一些 OpenCV 命令的影像。
目前我無法在我的 Kivy 布局中顯示這些影像。
我知道你可以在 Kivy 上顯示影像:
Image:
source: 'Images/Cats.jpg'
但是當我得到一個像這個閾值影像這樣的“編輯”影像時,我需要另一種方式:
threshold, thresh_inverse = cv.threshold(gray, 150, 255, cv.THRESH_BINARY_INV)
cv.imshow('Easy Threshold INV', thresh_inverse)
與我想在 Kivy 布局中顯示的直方圖的圖相同。
img = cv.imread('C:\\Users\\julia\\Desktop\\Images_HHN\\Cats3.jpg')
cv.imshow('Bsp. HHN', img)
blank = np.zeros(img.shape[:2],dtype='uint8')
Circle = cv.circle(blank, (img.shape[1] // 2, img.shape[0] // 2), 100, 255, -1)
cv.imshow('Circle', Circle)
mask= cv.bitwise_and(img,img, mask= Circle )
cv.imshow('MaskPic', mask)
plt.xlim([0, 256])
plt.show()
colors = ('b', 'g', 'r')
plt.figure()
plt.title('Color Histogramm')
plt.xlabel('Bins')
plt.ylabel('n pixels')
for i, col in enumerate(colors):
hist = cv.calcHist([img], [i], None, [256], [0,256 ])
plt.plot(hist,color=col)
plt.xlim([0,256])
plt.show()
如果有人能給我提示在我的 Kivy Layout id 上顯示它們,我將非常感激。
感謝您的幫助!
uj5u.com熱心網友回復:
檔案.kv僅在開始時加載,稍后您必須使用 Python 代碼來更新image.source或image.texture.
cv保持影像,numpy array所以我曾經numpy生成隨機陣列,轉換為kivy Texture并分配給現有的Image.
我曾經Clock每 0.25 秒重復一次,但您可以使用Button它來運行它。
from kivy.app import App
from kivy.uix.image import Image
from kivy.graphics.texture import Texture
from kivy.clock import Clock
import numpy as np
import cv2
# --- functions ---
def generate_texture():
"""Generate random numpy array `500x500` as iamge, use cv2 to change image, and convert to Texture."""
# numpy array
img = np.random.randint(0, 256, size=(500, 500, 3), dtype=np.uint8)
cv2.circle(img, (img.shape[1]//2, img.shape[0]//2), 100, 255, -1)
data = img.tobytes()
# texture
texture = Texture.create(size=(500, 500), colorfmt="rgb")
texture.blit_buffer(data, bufferfmt="ubyte", colorfmt="rgb")
return texture
def update_image(dt):
"""Replace texture in existing image."""
image.texture = generate_texture()
# --- main ---
# empty image at start
image = Image()
class MyPaintApp(App):
def build(self):
return image
# run function every 0.25 s
Clock.schedule_interval(update_image, 0.25)
if __name__ == '__main__':
MyPaintApp().run()
結果:

plt可能需要不同的方法。它可能需要將繪圖保存在io.BytesIO(模擬記憶體中的檔案)并將其從io.BytesIOto讀取CoreImage并復制CoreImage.texture到Image.texture.
from kivy.app import App
from kivy.uix.image import Image, CoreImage
from kivy.graphics.texture import Texture
from kivy.clock import Clock
import io
import numpy as np
import matplotlib.pyplot as plt
# --- functions ---
def generate_texture():
"""Generate random numpy array, plot it, save it, and convert to Texture."""
# numpy array
arr = np.random.randint(0, 100, size=10, dtype=np.uint8)
# plot
plt.clf() # remove previous plot
plt.plot(arr)
# save in memory
data = io.BytesIO()
plt.savefig(data)
data.seek(0) # move to the beginning of file
return CoreImage(data, ext='png').texture
def update_image(dt):
"""Replace texture in existing image."""
image.texture = generate_texture()
# --- main ---
# empty image at start
image = Image()
class MyPaintApp(App):
def build(self):
return image
# run function every 0.25 s
Clock.schedule_interval(update_image, 0.25)
if __name__ == '__main__':
MyPaintApp().run()
結果:

轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/317475.html
