【问题标题】:How to convert an opencv Mat into a numpy.ndarray?如何将 opencv Mat 转换为 numpy.ndarray?
【发布时间】:2019-08-24 21:56:30
【问题描述】:

我有一个用 java (android) 编写的代码,可以打开手机的摄像头并显示帧。下面的代码表示我们可以检索帧的方法。该项目使用 Chaquopy 来解释 python 代码。

 @Override
    public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {

        mRgba = inputFrame.rgba();
        Python py = Python.getInstance();

        PyObject pym = (PyObject) 
        py.getModule("MyPythonClass").callAttr("call",mRgba);

        return mRgba;
    }

python代码用于检索帧(由“mRgba”表示,它是java代码中的一个Mat)以进行进一步处理 问题是要找到一个解决方案,将这个 Mat 转换成可以被 python 代码解释的类型:

def call(frame):

    im = imutils.resize(frame, width=min(300, frame.shape[1]))

“frame”应该是java代码检索到的Mat并发送到函数“call”

【问题讨论】:

    标签: python numpy-ndarray chaquopy


    【解决方案1】:

    我通过在java端将Mat转换为byteArray找到了解决方案

    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            bmp.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
            byteArray = byteArrayOutputStream .toByteArray();
    

    在 python 端,检索到的 byteArray 被转换为 PIL 图像,然后转换为 numpy 数组:

    def call(imp):
    
        pic = Image.open(io.BytesIO(bytes(imp)))
        open_cv_image = np.array(pic)
        # Convert RGB to BGR
        frame = open_cv_image[:, :, ::-1].copy()
        im = imutils.resize(frame, width=min(300, frame.shape[1]))
    

    希望能有所帮助。

    【讨论】:

    • 如何将这个numpy数组接收回android端?
    猜你喜欢
    • 2023-03-09
    • 2017-12-15
    • 2018-04-16
    • 2011-06-28
    • 1970-01-01
    • 2014-05-05
    • 2018-09-04
    • 2015-12-13
    相关资源
    最近更新 更多