答案 0 :(得分:6)
一个简单的方法是:
在你的图像上,我用红色几乎笔直的线标记,用绿色标记曲线。您可以使用宽高比的阈值:
代码:
#include<opencv2/opencv.hpp>
using namespace cv;
int main()
{
// Load image
Mat1b img = imread("path_to_img", IMREAD_GRAYSCALE);
// Create output image
Mat3b out;
cvtColor(img, out, COLOR_GRAY2BGR);
// Find contours
vector<vector<Point>> contours;
findContours(img.clone(), contours, RETR_LIST, CHAIN_APPROX_NONE);
for (const auto& contour : contours)
{
// Find minimum area rectangle
RotatedRect rr = minAreaRect(contour);
// Compute aspect ratio
float aspect_ratio = min(rr.size.width, rr.size.height) / max(rr.size.width, rr.size.height);
// Define a threshold on the aspect ratio in [0, 1]
float thresh = 0.2f;
Vec3b color;
if (aspect_ratio < thresh) {
// Almost straight line
color = Vec3b(0,0,255); // RED
}
else {
// Curved line
color = Vec3b(0, 255, 0); // GREEN
}
// Color output image
for (const auto& pt : contour) {
out(pt) = color;
}
}
imshow("Out", out);
waitKey();
return 0;
}
答案 1 :(得分:2)
此值将接近&#39; 1&#39;直边。这个比率的值越高,边缘的曲率就越大。它可以大致但非常准确地估计边缘的弯曲度。
快乐编码