gpt4 book ai didi

c++ - OpenCV 在轮廓上画线 (c++)

转载 作者:太空宇宙 更新时间:2023-11-03 22:56:11 32 4
gpt4 key购买 nike

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

 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);

最佳答案

这可能意味着霍夫变换确实设法找到了您图片上的任何线条。在这种情况下,您应该先对图像进行预过滤。比如可以试试Otsu的thresholding和Gaussian blur。如果我是你,我会首先尝试将不同的参数传递给 cv::HoughLines(尤其是 threshold——“检测”一条线的最小交叉点数)

关于c++ - OpenCV 在轮廓上画线 (c++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30053604/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com