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

前端 未结 1 1749
离开以前
离开以前 2021-01-24 12:03

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

1条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-24 12:28

    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.

    0 讨论(0)
提交回复
热议问题