gpt4 book ai didi

python - 如何使用opencv或python检测2条相交(交叉)的曲线?

转载 作者:行者123 更新时间:2023-12-02 16:08:57 27 4
gpt4 key购买 nike

给定一个具有2条相交曲线的图像,如下图所示,我如何使用opencv或python检测和区分2条曲线? (所以我需要2条单独的曲线)

enter image description here

最佳答案

您可以扫描每一列,并从连接的零件中识别出簇。

伪算法:

list of curves
for each column
if a pixel region is black, surrounded by white
add it to the list of curves for which it continues the curve best

最棘手的部分是如何定义已给定曲线的最佳延续方式。如果仅选择到曲线最接近点的距离,则会在连接处遇到问题。
创建一条从曲线的上一个点到最后一个点到新点的直线,并测量到曲线的最后一个点的距离,效果很好。但是,由于测量的距离很小(可能存在舍入问题等),因此我没有对所有列进行迭代,而是仅对第5列进行了迭代。

enter image description here

这里的C++代码:
double distance_to_Line(cv::Point line_start, cv::Point line_end, cv::Point point)
{
double normalLength = _hypot(line_end.x - line_start.x, line_end.y - line_start.y);
double distance = (double)((point.x - line_start.x) * (line_end.y - line_start.y) - (point.y - line_start.y) * (line_end.x - line_start.x)) / normalLength;
return abs(distance);
}

int main()
{
cv::Mat in = cv::imread("C:/StackOverflow/Input/splitCurves.png", cv::IMREAD_GRAYSCALE);

std::vector<std::vector <cv::Point2f> > clusters;
float maxDist = 10.0f; //heuristic

// looping like this is quite expensive, but we need to scan column-wise
// will be cheaper to first transpose the image and then scan row-wise!!
for (int x = 0; x < in.cols; x+=5)
{
bool active = false;
cv::Point2f cCluster;
int cClusterSupportSize = 0;
for (int y = 0; y < in.rows; ++y)
{
cv::Point2f cPoint = cv::Point2f(x, y);
bool cActive = in.at<unsigned char>(y,x) == 0; // is the pixel black?
if (cActive)
{
cCluster += cPoint;
cClusterSupportSize += 1;
active = cActive;
}

if (active && !cActive)
{
// creating cluster:
cv::Point2f finishedCluster = 1.0f / cClusterSupportSize * cCluster;

cCluster = cv::Point2f();
cClusterSupportSize = 0;
active = false;

// adding cluster to list
int bestCluster = -1;
float bestDist = FLT_MAX;
for (int i = 0; i < clusters.size(); ++i)
{
float distToClusters = FLT_MAX;

// compute dist from apprximating a line through the last two points of the curve
// special case: no 2 points yet:
if (clusters[i].size() == 1)
{
float cDist = cv::norm(finishedCluster - clusters[i].back());
if (cDist < distToClusters) distToClusters = cDist;
}
else
{
// test continuity by testing whether adding the new point would make the last point still be placed well on the curve
cv::Point2f lineA = clusters[i][clusters[i].size() - 1];
cv::Point2f lineB = clusters[i][clusters[i].size() - 2];
//cv::Point2f lineB = finishedCluster;
// get dist from the current point to that line:

float cDist = distance_to_Line(lineA, lineB, finishedCluster);
if (cDist < distToClusters) distToClusters = cDist;
}



/*
for (int j = 0; j < clusters[i].size(); ++j)
{
// get dist to the curve
cv::Point2f lineA =
//float cDist = cv::norm(finishedCluster - clusters[i][j]);
if (cDist < distToClusters) distToClusters = cDist;
}
*/

if (distToClusters < maxDist)
{
if (distToClusters < bestDist)
{
bestDist = distToClusters;
bestCluster = i;
}
}
}

if (bestCluster < 0)
{
std::vector<cv::Point2f> newCluster;
newCluster.push_back(finishedCluster);
clusters.push_back(newCluster);
}
else
{
clusters[bestCluster].push_back(finishedCluster);
}

}

}
}

cv::Mat out;
cv::cvtColor(in, out, cv::COLOR_GRAY2BGR);
for (int i = 0; i < clusters.size(); ++i)
{
cv::Scalar color = cv::Scalar(i*rand() % 255, (i+2)*rand() % 255, (i+1) * 100);
if (i == 0) color = cv::Scalar(0, 255, 0);
if (i == 1) color = cv::Scalar(0, 0, 255);
for (int j = 1; j < clusters[i].size(); ++j)
{
cv::line(out, clusters[i][j - 1], clusters[i][j], color, 2);
}
}
cv::imwrite("C:/StackOverflow/Input/splitCurves_out.png", out);
cv::imshow("out", out);
cv::waitKey(0);
}

关于python - 如何使用opencv或python检测2条相交(交叉)的曲线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58517136/

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