【问题标题】:OpenCV: Draw Lines from CannyOpenCV:从 Canny 画线
【发布时间】:2015-02-16 00:31:30
【问题描述】:

我是 OpenCV 的新手,所以这可能是一个愚蠢的问题。

我只是想让一些基本的东西启动并运行——我想直接在传入的图像上绘制 Canny 算法检测到的边缘。我目前有这个:

我直接显示来自 Canny 的边缘数据,但现在我想在正在处理的图像上去掉黑色,只显示白色。

我尝试过在谷歌上搜索“使用二进制图像作为 alpha 掩码”之类的内容,但是在阅读了一天的教程并尝试了我能找到的所有内容之后,我仍然不确定我是否知道发生了什么。 OpenCV 似乎非常强大,所以这可能是一件很容易做到的事情,所以我希望有人能指出我正确的方向。

这是我正在使用的代码,其中大部分是从示例中复制的:

public Mat onCameraFrame(CvCameraViewFrame inputFrame) {

    Mat rgba = inputFrame.rgba();
    org.opencv.core.Size sizeRgba = rgba.size();

    Mat rgbaInnerWindow;


    int rows = (int) sizeRgba.height;
    int cols = (int) sizeRgba.width;

    int left = cols / 8;
    int top = rows / 8;

    int width = cols * 3 / 4;
    int height = rows * 3 / 4;

    //get sub-image
    rgbaInnerWindow = rgba.submat(top, top + height, left, left + width);

    //create edgesMat from sub-image
    Imgproc.Canny(rgbaInnerWindow, edgesMat, 100, 100);

    //copy the edgesMat back into the sub-image
   Imgproc.cvtColor(edgesMat, rgbaInnerWindow, Imgproc.COLOR_GRAY2BGRA, 4);

    rgbaInnerWindow.release();

    return rgba;
}

编辑:我还在 OpenCV 论坛 here 上发布了这个问题。

【问题讨论】:

    标签: java android opencv


    【解决方案1】:

    我已经有十多年没有使用 Java 了,也根本没有将 Java 与 OpenCV 一起使用,但我将尝试说明我将如何做到这一点。我正在尽我最大的努力用这种语言编写它,但如果我写得不太对,我希望你能够进行这些小改动以使其正常工作。

    在我看来,你运行 Canny 后的操作顺序应该是这样的:

    1. 复制您的 edgesMat 并将其转换为 BGRA。 (称之为 colorEdges
    2. 使用 edgesMat 作为蒙版,将 colorEdges 中的白色替换为您选择的颜色(您希望在视频源上绘制的颜色)。
    3. 然后使用 edgesMat 作为蒙版将 colorEdges 放回 rgbaInnerWindow

    代码:

    //step 1
    Mat colorEdges;
    edgesMat.copyTo(colorEdges);
    Imgproc.cvtColor(colorEdges, colorEdges, COLOR_GRAY2BGRA);
    //step 2
    newColor = new Scalar(0,255,0);    //this will be green
    colorEdges.setTo(newColor, edgesMat);
    //step 3
    colorEdges.copyTo(rgbaInnerWindow, edgesMat);    //this replaces your current cvtColor line, placing your Canny edge lines on the original image
    

    应该这样做。 :)

    copyTo (Mat m)
    cvtColor (Mat src, Mat dst, int code)
    setTo (Mat value, Mat mask)
    copyTo (Mat m, Mat mask)

    【讨论】:

      猜你喜欢
      • 2017-07-21
      • 1970-01-01
      • 2017-07-10
      • 2020-07-22
      • 1970-01-01
      • 1970-01-01
      • 2020-09-02
      • 1970-01-01
      相关资源
      最近更新 更多