轮廓上的OpenCV绘制线条(c ++)

时间:2015-05-05 12:54:34

标签: c++ opencv lines contour

我想在下面的图片上画线,我可以计算每一行的长度。我的问题是,当我用下面的代码尝试它时,我的图像变得完全白色。

 std::vector<cv::Vec2f> lines;
 cv::HoughLines(drawing_small, lines, 1, CV_PI/180, 50, 0, 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( drawing_small, pt1, pt2, cv::Scalar(0,100,0), 3, CV_AA);
 }

enter image description here 像这样的东西: enter image description here

如果有人能说我能做什么,我会很高兴。

更新

这就是我之前做的事情:

cv::findContours(dst, contours_small, hierarchy_small, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, cv::Point(0, 0) );
//Detecting Contours
std::vector<cv::Point2f> ContCenter_small(contours_small.size());
cv::Mat drawing_small = cv::Mat::zeros( dst.size(), CV_8UC3 );
for( int i = 0; i < contours_small.size(); i++ )
{
    ContArea_small[i] = moments(contours_small[i], false);
    ContCenter_small[i] = cv::Point2f(ContArea_small[i].m10/ContArea_small[i].m00, ContArea_small[i].m01/ContArea_small[i].m00);
    cv::Scalar color_small = cv::Scalar(0,255,0);
    if(ContArea_small[i].m00 > 2000)
    {
    drawContours( drawing_small, contours_small, i, color_small, CV_FILLED , 8, hierarchy_small, 1, cv::Point() );
    }
}
cv::imwrite("contour.jpg",drawing_small);
cv::dilate(drawing_small, drawing_small,C,cv::Point(-1,-1),1,1,20);
cv::threshold(drawing_small,drawing_small,100,255,cv::THRESH_BINARY_INV);
cv::GaussianBlur(drawing_small,drawing_small,cv::Size(9,9),11);

2 个答案:

答案 0 :(得分:1)

这可能意味着Hough Transform确实设法在你的照片上找到任何线条。在这种情况下,您应该首先预先过滤图像。例如,您可以尝试Otsu的阈值处理和高斯模糊。如果我是你,那么我首先要尝试将不同的参数传递给cv :: HoughLines(尤其是threshold - “检测”一条线的最小交叉点数量)

答案 1 :(得分:0)

确保上绘制线条并输出源图像而不是某些已处理的图像。你能告诉我们更多关于你所做的事情的代码吗?