python代码如何转java_如何在不使用numpy的情况下将Python代码转换为Java

我在Python中有一个方法,它使用OpenCV从图像中移除背景。我希望android版本的OpenCV也能使用相同的功能,但我似乎无法完全理解数组的工作原理以及如何处理它们。在

这是我到目前为止在Java中所看到的:private Bitmap GetForeground(Bitmap source){

source = scale(source,300,300);

Mat mask = Mat.zeros(source.getHeight(),source.getWidth(),CvType.CV_8U);

Mat bgModel = Mat.zeros(1,65,CvType.CV_64F);

Mat ftModel = Mat.zeros(1,65,CvType.CV_64F);

int x = (int)Math.round(source.getWidth()*0.1);

int y = (int)Math.round(source.getHeight()*0.1);

int width = (int)Math.round(source.getWidth()*0.8);

int height = (int)Math.round(source.getHeight()*0.8);

Rect rect = new Rect(x,y, width,height);

Mat sourceMat = new Mat();

Utils.bitmapToMat(source, sourceMat);

Imgproc.grabCut(sourceMat, mask, rect, bgModel, ftModel, 5, Imgproc.GC_INIT_WITH_RECT);

int frameSize=sourceMat.rows()*sourceMat.cols();

byte[] buffer= new byte[frameSize];

mask.get(0,0,buffer);

for (int i = 0; i < frameSize; i++) {

if (buffer[i] == 2 || buffer[i] == 0){

buffer[i] = 0;

}else{

buffer[i] = 1 ;

}

}

byte[][] sourceArray = getMultiChannelArray(sourceMat);

byte[][][] reshapedMask = ReshapeArray(buffer, sourceMat.rows(), sourceMat.cols());

return source;

}

private byte[][][] ReshapeArray(byte[] arr, int rows, int cols){

byte[][][] out = new byte[cols][rows][1];

int index=0;

for (int i = 0; i < rows; i++) {

for (int j = 0; j < cols; j++) {

out[i][j][0] = arr[index];

index++;

}

}

return out;

}

public static byte[][] getMultiChannelArray(Mat m) {

//first index is pixel, second index is channel

int numChannels=m.channels();//is 3 for 8UC3 (e.g. RGB)

int frameSize=m.rows()*m.cols();

byte[] byteBuffer= new byte[frameSize*numChannels];

m.get(0,0,byteBuffer);

//write to separate R,G,B arrays

byte[][] out=new byte[frameSize][numChannels];

for (int p=0,i = 0; p < frameSize; p++) {

for (int n = 0; n < numChannels; n++,i++) {

out[p][n]=byteBuffer[i];

}

}

return out;

}

我要重新创建的python代码:

^{pr2}$

我不知道如何转换python代码的最后两行。如果有一种方法可以在我自己的项目中在android设备上运行python clean,那也太棒了。在

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值