我在matlab的数组中有一张图片。 我想把照片印成猕猴桃。
# encoding: utf-8
import time
import matlab.engine
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.graphics import Rectangle
from kivy.graphics.texture import Texture
engine = matlab.engine.start_matlab("-nodesktop -noFigureWindows")
class kivyTest(Widget):
def __init__(self):
super(kivyTest, self).__init__()
self.texture = Texture.create(size=(560, 420), colorfmt="rgb")
time_eng, data_pict = engine.test_pict_data(4.0, 5.0, nargout=2)
self.texture.blit_buffer(data_pict._data, bufferfmt="ubyte", colorfmt="rgb")
with self.canvas:
Rectangle(pos=self.pos, size=(560, 420), texture=self.texture)
def on_touch_down(self, touch):
x = touch.pos[0]
y = touch.pos[1]
time_eng, data_pict = engine.test_pict_data(x, y, nargout=2)
self.texture.blit_buffer(data_pict._data, bufferfmt="ubyte", colorfmt="rgb")
super().on_touch_down(touch)
self.canvas.ask_update()
class my_app(App):
title = 'Matlab raw data picture test'
def build(self):
return kivyTest()
if __name__ == '__main__':
my_app().run()
一维数组中的data_pict._data大小为560x420x3 uint8 但是当我在这里打印时,结果是:
您对此行为有任何想法吗?
答案 0 :(得分:0)
好,我找到了路
我实际上来自matlab的1D数据(pictureFrame.cdata._data)不能使用,因此,我们需要用numpy重新组织它,并在传递给纹理时对其进行展平。
data_reorg = np.array(data_pict._data).reshape(data_pict.size, order='F')
self.texture.blit_buffer(data_reorg.ravel(), bufferfmt="ubyte", colorfmt="rgb")
这种方式既快速又有效。