在 Kivy 布局上显示 OpenCV 图像结果和绘图
Displaying an OpenCV Imageresult and Plots on Kivy Layouts
我是 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 布局 id 上显示它们,我将不胜感激。
感谢您的帮助!
文件 .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.BytesIO
读取到 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()
结果:
我是 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 布局 id 上显示它们,我将不胜感激。
感谢您的帮助!
文件 .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.BytesIO
读取到 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()
结果: