如何在 OpenCV 中延长绘制的线条?

How to extend a drawn line in OpenCV?

我有一个创建人脸检测程序的项目。其中一个要求是我在脸的中心画一条线。

现在我这样做的方法是首先在两只眼睛上画一条线(通过获取每只眼睛的坐标并将它们用作线点)然后画一条垂直线。这样我也可以考虑面部旋转。

然而,这条线和穿过眼睛的线一样长。如果可能的话,我希望这条垂直线穿过整个脸。附件是我的一个旋转面部图像的输出示例。我需要绿线更长。

我用伪代码写出来

  • 取图像中最远的右手边缘坐标 (row1,col1)
  • 取直线右侧的点 (row2,col2)
  • 从你的行 (row2,col2) 到 (row2,col1) 画一条线
  • 对左侧重复

这将导致从线条的任一侧到图像边缘绘制两条额外的线条。

很遗憾,您的图像是 jpeg,所以它有压缩痕迹。

我第一次尝试提取线,但是没有给出很好的角度信息,所以我在绿色区域分割后使用cv::minAreaRect。

cv::Mat green;
cv::inRange(input, cv::Scalar(0,100,0), cv::Scalar(100,255,100),green);


// instead of this, maybe try to find contours and choose the right one, if there are more green "objects" in the image
std::vector<cv::Point2i> locations; 
cv::findNonZero(green, locations);

// find the used color to draw in same color later
cv::Vec3d averageColorTmp(0,0,0);
for(unsigned int i=0; i<locations.size(); ++i)
{
    averageColorTmp += input.at<cv::Vec3b>(locations[i]);
}
averageColorTmp *= 1.0/locations.size();

// compute direction of the segmented region
cv::RotatedRect line = cv::minAreaRect(locations);

// extract directions:
cv::Point2f start = line.center;
cv::Point2f rect_points[4];
line.points(rect_points);
cv::Point2f direction1 = rect_points[0] - rect_points[1];
cv::Point2f direction2 = rect_points[0] - rect_points[3];

// use dominant direction
cv::Point2f lineWidthDirection;
lineWidthDirection = (cv::norm(direction1) < cv::norm(direction2)) ? direction1 : direction2;
double lineWidth = cv::norm(lineWidthDirection);
cv::Point2f lineLengthDirection;
lineLengthDirection = (cv::norm(direction1) > cv::norm(direction2)) ? direction1 : direction2;
lineLengthDirection = 0.5*lineLengthDirection; // because we operate from center;

// input parameter:
// how much do you like to increase the line size?
// value of 1.0 is original size
// value of > 1 is increase of line
double scaleFactor = 3.0;

// draw the line
cv::line(input, start- scaleFactor*lineLengthDirection, start+ scaleFactor*lineLengthDirection, cv::Scalar(averageColorTmp[0],averageColorTmp[1],averageColorTmp[2]), lineWidth);

得出这个结果:

如果图像中没有其他绿色,只有绿线,这种方法应该非常稳健。如果图像中还有其他绿色部分,则应先提取轮廓并选择正确的轮廓来分配变量 locations