Java中的OpenCV遮罩会传回空白像素值

时间:2018-08-15 04:44:16

标签: java android opencv

我已经在Android中创建了一个程序,该程序使用CvCameraViewListener2从相机流式传输。我已经完成了想要从源图像和具有特定形状的二进制图像中仅获取蒙版图像的图像处理。图像掩蔽成功,但是我注意到当您与相机一起移动(相机面向我)时,似乎有些区域没有更新。正在更新的唯一像素是被遮罩的图像,未被遮罩覆盖的图像的其他像素部分似乎是空的。真的是空的吗?因为在Python中,对于3通道图像,遮罩图像之外的区域将是黑色(0,0,0)值。

  public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {

    mRgba = inputFrame.rgba();

    // Rotate mRgba 90 degrees
    Core.transpose(mRgba, mRgbaT);
    Imgproc.resize(mRgbaT, mRgbaF, mRgbaF.size(), 0,0, 0);
    Core.flip(mRgbaF, mRgba, 1 );

    // Read the current frames.
    // All the rest of the code is patterned just like OpenCV in Python.
    Imgproc.cvtColor(mRgba, imgOrig, Imgproc.COLOR_RGBA2RGB);
    Imgproc.cvtColor(imgOrig, imgOrigBGR, Imgproc.COLOR_RGBA2BGR);
    Imgproc.cvtColor(imgOrigBGR, imgLAB, Imgproc.COLOR_BGR2Lab);
    Imgproc.cvtColor(imgOrigBGR, gray, Imgproc.COLOR_BGR2GRAY);
    Imgproc.GaussianBlur(gray, blurred,new Size(5,5),0);
    Imgproc.threshold(blurred,binarized,threshold,255,Imgproc.THRESH_BINARY_INV);
    //Imgproc.adaptiveThreshold(blurred, binarized,255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C,Imgproc.THRESH_BINARY_INV,5,1);

    // Mask the lab space image using the binary image that is adjustable from the seekbar.
    // **Based from the output, the masked image contains empty values from Mat and only contains the pixels of the masked
    // image itself.
    Core.bitwise_and(imgLAB,imgLAB,masked,binarized);

1 个答案:

答案 0 :(得分:0)

Mat对象不会删除其先前的内容,这就是为什么当您为Mat分配新值时,保留不受新图像数据影响的值的原因,因为使用mask(按位和)将仅返回这些值在面具里。为了解决这个问题,您需要先做一个Mat对象的新实例,然后再做一些遮罩以删除其先前的内容,这与Python OpenCV不同,它会自动使不受遮罩影响的像素变为黑色。