【问题标题】:Convert a kivy texture to opencv image将 kivy 纹理转换为 opencv 图像
【发布时间】:2020-03-04 06:48:03
【问题描述】:

我正在尝试将我的 kivy 纹理转换为图像格式,以便我可以使用 opencv (cv2) 对其进行处理。我尝试使用 read() 和 cv2.imread() 读取纹理,但都没有奏效。我还研究了将 ubyte 纹理转换为字符串,但没有成功。

kivy 相机纹理 -> 我可以使用 cv2 处理的格式

类似

MyVariable = someid.texture
#do something to format of MyVariable so that it is an 'image'
Newvar = MyVariable.read()
#cv2 processing...

编辑:

好的,所以我使用 RGB 数组格式的 texture.pixel 从纹理中获取了数据。然后我用了

newvalue = np.frombuffer(camera.texture.pixels, np.uint8) 

将数据转换为 numpy 字符串。从那里可以很容易地使用以下方法在图像小部件上重新显示它:

finalstage = newvalue.tostring()
texture1 = Texture.create(size=variables.size, colorfmt='rgba')
texture1.blit_buffer(finalstage, colorfmt='rgba', bufferfmt='ubyte')
image.texture = texture1

我的问题是现在我想使用 Opencv 在面上绘制矩形,然后更新图像纹理。我有绘制矩形的工作代码,但是我首先需要使用

将 RGB 数据转换为灰色
gray = cv2.cvtColor(newvalue, cv2.COLOR_RGBA2GRAY)

这样做会返回我的新错误:

11-08 17:22:51.451 25259 25582 E cv::error(): OpenCV(4.0.1) Error: Unspecified error (> Invalid number of channels in input image:
11-08 17:22:51.451 25259 25582 E cv::error(): >     'VScn::contains(scn)'
11-08 17:22:51.451 25259 25582 E cv::error(): > where
11-08 17:22:51.451 25259 25582 E cv::error(): >     'scn' is 1
11-08 17:22:51.451 25259 25582 E cv::error(): ) in cv::CvtHelper<cv::Set<3, 4, -1>, cv::Set<1, -1, -1>, cv::Set<0, 2, 5>, cv::SizePolicy::NONE>::CvtHelper(cv::InputArray, cv::OutputArray, int) [VScn = cv::Set<3, 4, -1>, VDcn = cv::Set<1, -1, -1>, VDepth = cv::Set<0, 2, 5>, sizePolicy = cv::SizePolicy::NONE], file /home/.../color.hpp, line 259

如果我遗漏了什么,请告诉我

【问题讨论】:

  • 纹理有一个save 方法,你可以用它来写入文件,然后用其他东西读取。
  • 他们还有一个pixels 属性来获取像素为rgb数据
  • 您好,谢谢您的提示!我完全错过了像素部分。我试试看,谢谢

标签: python numpy opencv opengl kivy


【解决方案1】:

你必须重塑“新价值”

height, width = camera.texture.height, camera.texture.width

newvalue = np.frombuffer(camera.texture.pixels, np.uint8)
newvalue = newvalue.reshape(height, width, 4)
gray = cv2.cvtColor(newvalue, cv2.COLOR_RGBA2GRAY)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-05
    • 2020-09-08
    • 2011-11-29
    相关资源
    最近更新 更多