Kivy与opencv.调整图像大小



我想在kivy中插入一个opencv网络摄像头视频。不幸的是,当我这样做并最大化窗口时,图像不会调整到屏幕大小。有办法做到这一点吗?

from kivy.app import App
from kivy.uix.image import Image
from kivy.clock import Clock
from kivy.graphics.texture import Texture
import cv2
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.lang import Builder
from kivy.core.window import Window
Window.size=(1000,1000)

class KivyCamera(Image):
    def __init__(self, capture, fps, **kwargs):
        super(KivyCamera, self).__init__(**kwargs)
        self.capture = capture
        Clock.schedule_interval(self.update, 1.0 / fps)
    def update(self, dt):
        ret, frame = self.capture.read()
        if ret:
            buf1 = cv2.flip(frame, 0)
            buf = buf1.tostring()
            image_texture = Texture.create(size=(frame.shape[1], frame.shape[0]), colorfmt='rgb')#(480,640)
            image_texture.blit_buffer(buf, colorfmt='bgr', bufferfmt='ubyte')
            # display image from the texture
            self.texture = image_texture

class CamApp(App):
    def build(self):
        self.camera= cv2.VideoCapture(2)
        self.my_camera = KivyCamera(capture=self.camera, fps=10,resolution=(1280,960))
        return self.my_camera
    def on_stop(self):
        self.camera.release()

if __name__ == '__main__':
    CamApp().run()

我希望当winow尺寸增加时,框架的尺寸会增加。当我尝试在"纹理"中更改大小时,会出现错误。Like要么显示奇怪的图像,要么不打开任何窗口。如果有人能帮忙就太好了。谢谢

您可以执行以下操作:将类类型更改为BoxLayout。将图像小部件添加到size_hint=(1,1)的Boxlayout中。在init中使用大小绑定,如下所示:

self.image = Image(size_hint=(1,1))
self.add_widget(self.image)
self.binding(size=self.Updatesize,
             pos=self.Updatesize)

并在类中定义一个函数,如下所示:

def Updatesize(*args):
   self.image.size=self.size
   self.image.pos=self.pos

相关内容

  • 没有找到相关文章

最新更新