【问题标题】:How to get co-ordinates of line with the help of OpenCv in android如何在 android 中借助 OpenCv 获取线的坐标
【发布时间】:2014-11-09 14:29:04
【问题描述】:

我想在 android 中借助 OpenCV 来坐标线。我研究了教程,这就是我的 api 调用是

Mat ImageMat = new Mat(croppedImage.getHeight(), croppedImage.getWidth(), CvType.CV_8U, new Scalar(4));
int threshold = 50;
int minLineSize = 100;
int lineGap = 20;

Mat lines = new Mat();
Imgproc.HoughLinesP(ImageMat, lines, 1, Math.PI / 180, threshold, minLineSize, lineGap);

我提供了一个包含一行的简单图像,但在“行”变量中,我得到了数百个坐标。我只是那条线的一个坐标。如何仅获取该单行的坐标。另外,测量 minLineSize 的单位是什么?我的行是 FirstName、LastName 等前面的行。

【问题讨论】:

  • 可能你的线太粗了,所以你会在粗线中发现很多 1 宽度的线。请发布您的原始图片,如果可能的话,请发布所有检测到的线条的图片。
  • minLineSize 应该以像素距离为单位(可能是欧几里得或欧几里得的近似值)
  • @Micka 图片现在有问题
  • 您想检测黑线吗?您必须首先转换为灰度和阈值
  • @Micka 请告诉步骤。我无法获得。

标签: java android opencv hough-transform


【解决方案1】:

这里是 C++ 代码。由于主要使用 OpenCV 函数,您可以轻松地将其移植到 android CV:

int main()
{
    // loading your image. you dont need theses parts
    cv::Mat input = cv::imread("../inputData/FormularLineDetection.png");


    // convert to grayscale: you will do something similar:
    cv::Mat gray;
    cv::cvtColor(input, gray, CV_BGR2GRAY);

    // computation of binary thresholding so that dark areas of the image will bevcome "foreground pixel".
    // If your image have bright features you'll have to choose different parameters.
    // If you want to detect contour lines instead you'll compute gradient magnitude first.
    cv::Mat mask;
    cv::threshold(gray, mask, 0, 255, CV_THRESH_BINARY_INV | CV_THRESH_OTSU);

    std::vector<cv::Vec4i> lines;
    //cv::HoughLinesP(mask, lines, 1, CV_PI/180.0, 50, 50, 10 );
    // I've changed the min-Size of a line to 1/3 of the images width. Maybe you'll have to adjust that parameter to your needs!
    cv::HoughLinesP(mask, lines, 1, CV_PI/180.0, 50, input.cols/3, 10 );



    // draw the lines to visualize: you might not do this at all
    for( size_t i = 0; i < lines.size(); i++ )
    {
        cv::Vec4i l = lines[i];
        cv::line( input, cv::Point(l[0], l[1]), cv::Point(l[2], l[3]), cv::Scalar(0,0,255), 3, CV_AA);
    }

    // display and save to disk
    cv::imshow("mask", mask);   // you might not want to display the image here.
    cv::imshow("output",input);
    cv::imwrite("../outputData/FormularLineDetection.png", input);

    cv::waitKey(0);
    return 0;
}

根据您的输入,我得到以下输出:

如您所见,检测到了您想要的线条,但此外还检测到了粗大的“线条”。您可能想尝试检测类似的结构并将其过滤掉!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-24
    • 2012-03-30
    • 2020-11-28
    • 1970-01-01
    • 1970-01-01
    • 2017-07-11
    • 1970-01-01
    相关资源
    最近更新 更多