【问题标题】:Opencv javacpp-presets findContours gives error: Unrecognized or unsupported array type in cvGetMatOpencv javacpp-presets findContours 给出错误:cvGetMat 中的数组类型无法识别或不受支持
【发布时间】:2015-08-12 06:52:40
【问题描述】:

我正在尝试使用 opencv javacpp-presets(版本 3.0.0-1.0)并使用下面的代码片段从图像(二进制化)中提取文本。该片段是从代码的this python version 翻译而来的。

输入图像来自文件并通过imread 加载,但代码在findContours 处失败并显示以下错误消息:

OpenCV Error: Bad flag (parameter or structure field) (Unrecognized or unsupported array type) in cvGetMat, file src\array.cpp, line 2494

here 建议的解决方案对我不起作用。非常感谢任何帮助!

    // Load the image
    Mat image_orig = imread(inputFile);
    if ( image_orig.empty() ) { LOGGER.error("Empty image!");}

    this.image = new Mat();
    //Add a border to the image for processing sake
    copyMakeBorder(image_orig, this.image, 50, 50, 50, 50, BORDER_CONSTANT);

    //# Calculate the width and height of the image

    this.img_y = this.image.arrayHeight();
    this.img_x = this.image.arrayWidth();

    if (DEBUG)
        LOGGER.info("Image is " + this.img_x + "x" + this.img_x);

    //Split out each channel
    Mat red = new Mat();
    Mat green = new Mat();
    Mat blue = new Mat();

    MatVector v = new MatVector(blue, green, red);
    split(image, v);

    //Run canny edge detection on each channel
    Mat blue_edges = new Mat();
    Canny(blue, blue_edges, 200, 250);

    Mat green_edges = new Mat();
    Canny(green, green_edges, 200, 250);

    Mat red_edges = new Mat();
    Canny(red, red_edges, 200, 250);

    //Join edges back into image
    Mat edges = new Mat();
    MatVector vEdges = new MatVector(red_edges, green_edges, blue_edges);
    merge(vEdges, edges);

    //Find the contours
    Mat edgesCopy = new Mat();
    edges.copyTo(edgesCopy);

    Mat hierarchy = new Mat();
    MatVector contours = new MatVector();
    findContours(edgesCopy, contours, hierarchy, RETR_TREE, CHAIN_APPROX_NONE);

【问题讨论】:

  • 在调用 findContours 之前尝试将边缘复制垫转换为灰度。它要求输入是单通道的 8 位图像。
  • 感谢您的回复。我尝试了您的建议://convert to grayscale Mat gray = new Mat(); cvtColor(edges, gray, COLOR_BGR2GRAY); Mat blur = new Mat(); GaussianBlur(gray, blur, new Size(5,5), 0); Mat edgesCopy = new Mat(); adaptiveThreshold(blur, edgesCopy, 255,1,1,11,2); 但我在findContours 收到相同的错误消息
  • 您确定图像被正确读取了吗?尝试在读入后检查类型,然后将其传递给findContours() 我认为Mat.type() 是Java API 中的函数调用。
  • edgesCopy.type()在转换为灰度之前等于16,之后等于0
  • 我猜拆分到多通道行不通?

标签: java opencv javacpp


【解决方案1】:

天哪,我真傻。我应该使用来自 MatVector i.s.o 的输入。用于 Canny 检测的空垫。然后使用灰度作为@user3510227 建议我得到以下工作代码:

    // Load the image
    Mat image_orig = imread(inputFile);
    if ( image_orig.empty() ) { LOGGER.error("Empty image!");}

    this.image = new Mat();
    //Add a border to the image for processing sake
    copyMakeBorder(image_orig, this.image, 50, 50, 50, 50, BORDER_CONSTANT);

    //# Calculate the width and height of the image

    this.img_y = this.image.arrayHeight();
    this.img_x = this.image.arrayWidth();

    if (DEBUG)
        LOGGER.info("Image is " + this.img_x + "x" + this.img_x);

    //Split out each channel
    Mat red = new Mat();
    Mat green = new Mat();
    Mat blue = new Mat();

    MatVector v = new MatVector(red, green, blue);
    split(image, v);

    //Run canny edge detection on each channel
    Mat blue_edges = new Mat();
    Canny(v.get(0), blue_edges, 200, 250);
    Mat green_edges = new Mat();
    Canny(v.get(1), green_edges, 200, 250);
    Mat red_edges = new Mat();
    Canny(v.get(2), red_edges, 200, 250);

    //Join edges back into image
    Mat edges = new Mat();
    MatVector vEdges = new MatVector(red_edges, green_edges, blue_edges);
    merge(vEdges, edges);
    LOGGER.info("Type: " + edges.type());


    //convert to grayscale
    Mat gray = new Mat();
    cvtColor(edges, gray, COLOR_BGR2GRAY);
    Mat blur = new Mat();
    GaussianBlur(gray, blur, new Size(5,5), 0);
    Mat edgesCopy = new Mat();
    adaptiveThreshold(blur, edgesCopy, 255,1,1,11,2);


    //Find the contours        
    Mat hierarchy = new Mat();
    MatVector contours = new MatVector();

    findContours(edgesCopy, contours, hierarchy, RETR_TREE, CHAIN_APPROX_NONE);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-30
    • 2021-10-30
    相关资源
    最近更新 更多