ios - OpenCV 线/标尺检测

标签 ios opencv opencv3.0 objective-c++ opencv3.1

我正在尝试检测图像上的标尺,我将遵循下一个过程:

1)准备图像(模糊,Canny等)

2) 检测线

3) 准备一组平行线

所以,我有一个图像: enter image description here

该应用程序转换为: enter image description here

接下来我尝试了 HoughLinesP 方法,看起来我不能在我的情况下应用它,因为我不知道线的角度,所以找不到标尺垂直线,但找到了水平(例如)并且每条标尺线都由许多细线组成,这将是一个处理问题: enter image description here

代码:

std::vector<cv::Vec4i> lines_std;
cv::HoughLinesP( grayMat, lines_std, 1, CV_PI/90,  50, 10, 0 );

// drawing lines (with random color)
for( size_t i = 0; i < lines_std.size(); i++ )
{
    cv::line( originalMat, cv::Point(lines_std[i][0], lines_std[i][1]),
             cv::Point(lines_std[i][2], lines_std[i][3]), cv::Scalar(arc4random_uniform(155)+100,
                                                             arc4random_uniform(155)+100,
                                                             arc4random_uniform(155)+100), 1);
}

我也试过 LineSegmentDetector,得到了我预期的更接近的结果: enter image description here

代码:

vector<Vec4f> lines_std;
Ptr<LineSegmentDetector> ls = createLineSegmentDetector(LSD_REFINE_NONE);
ls->detect(grayMat, lines_std);

但在这里我遇到了一些问题(看起来没有办法自定义createLineSegmentDetector): 并不是所有的线都被检测到;线检测不是在中心,而是在两侧,有时只在左侧或右侧,但我需要得到粗线的中心,因为这将在接下来的计算中使用。

那么,找到所有行(并且每行只在粗线中心出现一次)的正确方法是什么?

更新

还尝试了 HoughLines:

矢量线;

cv::HoughLines(grayMat, lines, 1, CV_PI/90, 100 , 100, 0 );

for( size_t i = 0; i < lines.size(); i++ )
{
    float rho = lines[i][0], theta = lines[i][1];
    cv::Point pt1, pt2;
    double a = cos(theta), b = sin(theta);
    double x0 = a*rho, y0 = b*rho;

    pt1.x = cvRound(x0 + 1000*(-b));
    pt1.y = cvRound(y0 + 1000*(a));
    pt2.x = cvRound(x0 - 1000*(-b));
    pt2.y = cvRound(y0 - 1000*(a));

    cv::line( originalMat, pt1, pt2, cv::Scalar(0,255,0), 3, CV_AA);
}

但结果看起来也很奇怪(并且计算需要很多时间): enter image description here

最佳答案

我猜我找到了我应该遵循的方式:

1) 使线条尽可能细(在 Canny 变换之后):

cv::Mat skel(grayMat.size(), CV_8UC1, cv::Scalar(0));
cv::Mat temp(grayMat.size(), CV_8UC1);
cv::Mat elementSkel = cv::getStructuringElement(cv::MORPH_CROSS, cv::Size(3, 3));

bool done;
do
{
    cv::morphologyEx(grayMat, temp, cv::MORPH_OPEN, elementSkel);
    cv::bitwise_not(temp, temp);
    cv::bitwise_and(grayMat, temp, temp);
    cv::bitwise_or(skel, temp, skel);
    cv::erode(grayMat, grayMat, elementSkel);

    double max;
    cv::minMaxLoc(grayMat, 0, &max);
    done = (max == 0);
} while (!done);

看起来像这样:

enter image description here

2) 用 LineSigmentDetector 检测线:

vector<Vec4f> lines_std;
Ptr<LineSegmentDetector> ls = createLineSegmentDetector(LSD_REFINE_NONE);
ls->detect(skel, lines_std);

3)根据角度计算线角度和组id:

NSMutableDictionary *testHashMap = [[NSMutableDictionary alloc]init];

for( size_t i = 0; i < lines_std.size(); i++ )
{
    cv::Point p1 = cv::Point(lines_std[i][0], lines_std[i][1]);
    cv::Point p2 = cv::Point(lines_std[i][2], lines_std[i][3]);
    int angle = abs(atan2(p1.y - p2.y, p1.x - p2.x)); // int for rounding (for test only)

    NSMutableArray *idArray=testHashMap[[NSString stringWithFormat:@"%i", angle]];
    if(idArray == nil) {
        idArray = [[NSMutableArray alloc] init];
    }

    [idArray addObject:[NSNumber numberWithInt:i]];
    [testHashMap setObject:idArray forKey:[NSString stringWithFormat:@"%i", angle] ];
}

4) 找到标尺线组并绘制它:

for( NSInteger i = 0; i < [rulerIds count]; i++ )
{
    int itemId =   [[rulerIds objectAtIndex:i] integerValue];
    cv::Point p1 = cv::Point(lines_std[itemId][0], lines_std[itemId][1]);
    cv::Point p2 = cv::Point(lines_std[itemId][2], lines_std[itemId][3]);
    cv::line( originalMat, p1 ,  p2, cv::Scalar(0,255,0), 1);
}

我得到的结果:

enter image description here

更新

但如果我们很好地缩放此图像,仍然会看到重复的线条 为了消除重复,我做了简单的逻辑,通过为每个点建立平均值来合并线,例如在 3 条线(绿色)的情况下,我们在末尾有 3 个点:

enter image description here

关于ios - OpenCV 线/标尺检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42162229/

相关文章:

ios - "Different language linkage"— xcode 4 中的警告,xcode 5 中的错误

ios - 这些 MPMediaQuery 结果之间有什么区别?

c++ - 将 opencv 处理添加到 gstreamer 应用程序

opencv - 如何在opencv中的图像上应用我自己的过滤器

c++ - 当画面中出现蓝色时,图像处理失败。可能是什么原因?

c# - 如何使用 FindChessboardCorners

objective-c - UITextField 边框动画

ios - Alamofire 请求错误 - SWIFT 2.0

c++ - 如何在opencv中为图像添加边框,边框颜色必须与图像颜色相同

opencv - 我可以使用opencv将Gstreamer输出存储到缓冲区吗?如果我在管道中添加appsink