Opencv &PyGi:如何显示由 opencv 读取的图像 [英] Opencv & PyGi : how to display an image read by opencv

查看:104
本文介绍了Opencv &PyGi:如何显示由 opencv 读取的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 PyGi 中显示图像,图像首先被 opencv 读取.但是,它失败了.

I want to display an image in PyGi, the image is read first by opencv. But,it fails.

from gi.repository import Gtk, GdkPixbuf
import cv2
import numpy as np
window = Gtk.Window()
image = Gtk.Image()
image.show()
window.add(image)
window.show_all()
im = cv2.imread("file.bmp")
a = np.ndarray.tostring(img)
h, w, d = img.shape
p = GdkPixbuf.Pixbuf.new_from_data(a,GdkPixbuf.Colorspace.RGB, False, 8, w, h, w*3, None, None)
image.set_from_pixbuf(p)
Gtk.main()

但结果是黑色图像.此外,如果我循环一组文件(来自一个目录的多个 .bmp 文件),我会得到 coredump(怀疑 GdkPixbuf.Pixbuf.new_from_data)

But the result is a black image. Moreover, if I loop around a set of files (multiple .bmp files from a directory), I got coredump (suspecting GdkPixbuf.Pixbuf.new_from_data)

这是拥有 opencv & 的正确方法吗?PyGi 交互?我设法将 opencv 与 Tkinter 一起使用,但我无法将它与 PyGi 一起使用.

Is it the proper way to have opencv & PyGi interacting ? I managed to use opencv with Tkinter, but I fail to use it with PyGi.

推荐答案

这实际上对我来说效果很好.也许您的情况的问题是两个不同的变量 'im'(您在其中读取简历图像)和 'img'(您要转换为的变量)字符串)?

This actually worked just fine for me. Perhaps the issue in your case was the two different variables 'im' (in which you are reading the CV image) and 'img' (the one that you are converting to string)?

这是一个简化的代码,用于显示来自相机的视频帧:

Here is a simplified code that worked for me in displaying a video frame from camera:

# OpenCV image:
cap = cv2.VideoCapture(0)
ret, img = cap.read()
# Gtk Image:
img_gtk = Gtk.Image()
# Do other things such as attaching the 'img_gtk' to a window/grid...

# Convert and display:
h, w, d = img.shape
pixbuf = GdkPixbuf.Pixbuf.new_from_data  (img.tostring(), GdkPixbuf.Colorspace.RGB, False, 8, w, h, w*3, None, None)
img_gtk.set_from_pixbuf (pixbuf)
img_gtk.show()

这篇关于Opencv &PyGi:如何显示由 opencv 读取的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆