How to convert an opencv Mat into a numpy.ndarray?

我的未来我决定 提交于 2019-12-20 03:52:20

问题


I have a code written in java (android) that open the camera of the phone and show frames. The below code represents the method in which we can retrieve frames. The project used Chaquopy to interpret python code.

 @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;
    }

The python code is used to retrieve the frame (represented by "mRgba" which is a Mat in the java code) for further treatment The problem is to find a solution to convert this Mat into a type that could be interpreted by python code :

def call(frame):

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

"frame" is supposed to be the Mat retrieved by the java code and sent to the function "call"


回答1:


I found a solution by transforming the Mat into a byteArray in the java side

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

and in the python side the byteArray retrieved is transformed into a PIL image and then to a numpy array :

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]))

Hope that could help.



来源:https://stackoverflow.com/questions/57641801/how-to-convert-an-opencv-mat-into-a-numpy-ndarray

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!