将奇异的纹理转换为OpenCV图像 [英] Convert a kivy texture to opencv image
问题描述
我正在尝试将我的猕猴桃纹理转换为图像格式,以便可以使用opencv(cv2)处理它.我尝试使用read()和cv2.imread()读取纹理,但均无效.我还考虑将ubyte纹理转换为字符串,但无济于事.
I am trying to convert my kivy texture to an image format so that i can process it using opencv (cv2). I tried reading the texture using read() and using cv2.imread() but neither worked. I also looked into converting the ubyte texture into a string but got nowhere.
奇凡相机纹理->格式
类似
MyVariable = someid.texture
#do something to format of MyVariable so that it is an 'image'
Newvar = MyVariable.read()
#cv2 processing...
好,所以我以RGB阵列格式使用texture.pixel从纹理中获取了数据.然后,我用
Ok so i got the data from the texture using texture.pixel in an RGB array format. I then used
newvalue = np.frombuffer(camera.texture.pixels, np.uint8)
将数据转换为numpy字符串.从那里可以很容易地使用
to get the data into a numpy string. From there it was pretty easy to re display it on an image widget using:
finalstage = newvalue.tostring()
texture1 = Texture.create(size=variables.size, colorfmt='rgba')
texture1.blit_buffer(finalstage, colorfmt='rgba', bufferfmt='ubyte')
image.texture = texture1
我的问题是,现在我想使用Opencv在脸上绘制矩形,然后更新图像纹理.我有工作代码来绘制矩形,但是我首先需要使用
my issue is that now i want to use Opencv to draw rectangles on the faces and then update the image texture. I have working code to draw rectangles, however i first need to convert the RGB data to grey using
gray = cv2.cvtColor(newvalue, cv2.COLOR_RGBA2GRAY)
这样做会返回我的新错误:
Doing this returns my new error:
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
如果我遗漏了一些东西,请告诉我
If i left something out, let me know
推荐答案
您必须重塑"newvalue"
You must reshape "newvalue"
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)
这篇关于将奇异的纹理转换为OpenCV图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!