gpt4 book ai didi

c++ - 使用 cv::goodFeaturesToTrack 拟合线

转载 作者:行者123 更新时间:2023-11-28 01:42:01 30 4
gpt4 key购买 nike

我有角点,我正在尝试使用 cv::fitline 来拟合线但是我得到了图片中显示的来自原点 0,0 的线。

如果有帮助的话,我还有投影矩阵和 View 矩阵以及相机 intersincs 参数

我正在尝试计算图中盒子的体积 enter image description here

int main( int argc, char** argv )
{


Mat src, src_copy, edges, dst;
src = imread( "freezeFrame__1508152029892.png", 0 );

src_copy = src.clone();

GaussianBlur( src, edges, Size( 5, 5 ), 1.5, 1.5 );

erode( edges, edges, Mat() );// these lines may need to be optimized
dilate( edges, edges, Mat() );
dilate( edges, edges, Mat() );
erode( edges, edges, Mat() );

Canny( edges, dst, 1, 10, 3 ); // canny parameters may need to be optimized
imshow( "canny", dst );

std::vector< cv::Point2f > corners;

// maxCorners – The maximum number of corners to return. If there are more corners
// than that will be found, the strongest of them will be returned
int maxCorners = 10;

// qualityLevel – Characterizes the minimal accepted quality of image corners;
// the value of the parameter is multiplied by the by the best corner quality
// measure (which is the min eigenvalue, see cornerMinEigenVal() ,
// or the Harris function response, see cornerHarris() ).
// The corners, which quality measure is less than the product, will be rejected.
// For example, if the best corner has the quality measure = 1500,
// and the qualityLevel=0.01 , then all the corners which quality measure is
// less than 15 will be rejected.
double qualityLevel = 0.01;

// minDistance – The minimum possible Euclidean distance between the returned corners
double minDistance = 20.;

// mask – The optional region of interest. If the image is not empty (then it
// needs to have the type CV_8UC1 and the same size as image ), it will specify
// the region in which the corners are detected
cv::Mat mask;

// blockSize – Size of the averaging block for computing derivative covariation
// matrix over each pixel neighborhood, see cornerEigenValsAndVecs()
int blockSize = 3;

// useHarrisDetector – Indicates, whether to use operator or cornerMinEigenVal()
bool useHarrisDetector = false;

// k – Free parameter of Harris detector
double k = 0.04;

cv::goodFeaturesToTrack( src, corners, maxCorners, qualityLevel, minDistance, mask, blockSize, useHarrisDetector, k );



std::vector<Vec4f> lines;
for ( int i = 0; i < corners.size(); i++ )
{

cv::Point2f pt = corners[i];
for ( int j = i + 1; j < corners.size(); j++ )
{

cv::Point2f endpt = corners[j];

std::vector<cv::Point2f> points;
points.push_back( pt );
points.push_back( endpt );


Vec4f line;
cv::fitLine( points, line, CV_DIST_L2, 0, 0.01, 0.01 );
lines.push_back( line );
}
}

for ( size_t i = 0; i < lines.size(); i++ )
{

cv::Vec4i v = lines[i];
line( src, Point( v[0], v[1] ), Point( v[2], v[3] ), Scalar( 0, 0, 255 ), 3, 4 );
}


for ( size_t i = 0; i < corners.size(); i++ )
{
cv::circle( src, corners[i], 10, cv::Scalar( 255. ), -1 );
}

imshow( "line src", src );
imshow("line dest", edges );
cv::waitKey( 0 );


return 0;
}

最佳答案

阅读文档:

line – Output line parameters. In case of 2D fitting, it should be a vector of 4 elements (like Vec4f) - (vx, vy, x0, y0), where (vx, vy) is a normalized vector collinear to the line and (x0, y0) is a point on the line. In case of 3D fitting, it should be a vector of 6 elements (like Vec6f) - (vx, vy, vz, x0, y0, z0), where (vx, vy, vz) is a normalized vector collinear to the line and (x0, y0, z0) is a point on the line.`

所以你必须按照以下方式画线:

Point2f linePoint = Point2f( v[2], v[3] );
Point2f lineDirection = Point2f( v[0], v[1]);
float factor = 50; // if lineDirection is already length 1, you could choose factor to be the desired line length
line( src, linePoint , linePoint+ factor*lineDirection + , Scalar( 0, 0, 255 ), 3, 4 );`

关于c++ - 使用 cv::goodFeaturesToTrack 拟合线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46787204/

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