python如何调用c++numpy.ndarray代码,如何将opencv Mat转换为numpy.ndarray?

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"

解决方案

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值