使用C ++时,我的工作正常,代码如下:
Mat floodFilled = cv::Mat::zeros(dilateGrad.rows + 2, dilateGrad.cols + 2, CV_8U);
floodFill(dilateGrad, floodFilled, cv::Point(0, 0), 0, 0, cv::Scalar(), cv::Scalar(), 4 + (255 << 8) + cv::FLOODFILL_MASK_ONLY);
floodFilled = cv::Scalar::all(255) - floodFilled;
Mat temp;
floodFilled(Rect(1, 1, dilateGrad.cols - 2, dilateGrad.rows - 2)).copyTo(temp);
floodFilled = temp;
imshow("5. Floodfill", floodFilled);
但是我希望能够在Java中做同样的事情,我不能做这样的事情:
floodFilled = cv::Scalar::all(255) - floodFilled;
在java我的代码中它就像:
private static Mat floodFill(Mat img)
{
Mat floodfilled = Mat.zeros(img.rows() + 2, img.cols()+2, CvType.CV_8U);
Imgproc.floodFill(img, floodfilled, new Point(0,0), new Scalar(0,255,0));
return floodfilled;
}
结果是我返回的图像与我在函数中收到的图像完全相同。我的意思是,我期待填充的图像,但我得到了原始图像,即没有任何反应。
答案 0 :(得分:0)
使用此代码可以正常工作:
private static Mat floodFill(Mat img)
{
Mat floodfilled = Mat.zeros(img.rows() + 2, img.cols() + 2, CvType.CV_8U);
Imgproc.floodFill(img, floodfilled, new Point(0, 0), new Scalar(255), new OpenCVForUnity.Rect(), new Scalar(0), new Scalar(0), 4 + (255 << 8) + Imgproc.FLOODFILL_MASK_ONLY);
Core.subtract(floodfilled, Scalar.all(0), floodfilled);
Rect roi = new Rect(1, 1, img.cols() - 2, img.rows() - 2);
Mat temp = new Mat();
floodfilled.submat(roi).copyTo(temp);
img = temp;
//Core.bitwise_not(img, img);
return img;
}