【问题标题】:OpenCV - Filling an Image using JavaOpenCV - 使用 Java 填充图像
【发布时间】:2017-05-28 00:28:09
【问题描述】:

我在使用 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;
}

结果是我返回的图像与我在函数中收到的图像完全相同。我的意思是,我期望填充的图像,但我得到的是原始图像,也就是说,没有任何反应。

【问题讨论】:

  • 两个建议:显示更多 Java 代码,并显示您遇到的具体错误。
  • 知道了。已编辑。
  • 酷 - 下一个问题我关心你的预期结果。您说“结果是我返回的图像与我在函数中收到的图像完全相同。”这与您的预期有何不同? “它坏了”报告的一个很好的格式是:“我看到 A 但期待 B”。我不清楚你的问题是什么,而不是你得到什么。
  • 又搞定了。感谢您的提示,已编辑

标签: java c++ opencv image-processing


【解决方案1】:

使用此代码可以正常工作:

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;
}

【讨论】:

    猜你喜欢
    • 2011-08-15
    • 2017-02-02
    • 2015-12-02
    • 1970-01-01
    • 2014-11-18
    • 2018-10-28
    • 2011-05-19
    • 2013-04-23
    • 1970-01-01
    相关资源
    最近更新 更多