您可以使用 HSV 颜色空间来提取色调信息。
这里有一些cmets的代码,如果有任何问题欢迎提问:
int main(int argc, char* argv[])
{
cv::Mat input = cv::imread("C:/StackOverflow/Input/coloredLines.png");
// convert to HSV color space
cv::Mat hsvImage;
cv::cvtColor(input, hsvImage, CV_BGR2HSV);
// split the channels
std::vector<cv::Mat> hsvChannels;
cv::split(hsvImage, hsvChannels);
// hue channels tells you the color tone, if saturation and value aren't too low.
// red color is a special case, because the hue space is circular and red is exactly at the beginning/end of the circle.
// in literature, hue space goes from 0 to 360 degrees, but OpenCV rescales the range to 0 up to 180, because 360 does not fit in a single byte. Alternatively there is another mode where 0..360 is rescaled to 0..255 but this isn't as common.
int hueValue = 0; // red color
int hueRange = 15; // how much difference from the desired color we want to include to the result If you increase this value, for example a red color would detect some orange values, too.
int minSaturation = 50; // I'm not sure which value is good here...
int minValue = 50; // not sure whether 50 is a good min value here...
cv::Mat hueImage = hsvChannels[0]; // [hue, saturation, value]
// is the color within the lower hue range?
cv::Mat hueMask;
cv::inRange(hueImage, hueValue - hueRange, hueValue + hueRange, hueMask);
// if the desired color is near the border of the hue space, check the other side too:
// TODO: this won't work if "hueValue + hueRange > 180" - maybe use two different if-cases instead... with int lowerHueValue = hueValue - 180
if (hueValue - hueRange < 0 || hueValue + hueRange > 180)
{
cv::Mat hueMaskUpper;
int upperHueValue = hueValue + 180; // in reality this would be + 360 instead
cv::inRange(hueImage, upperHueValue - hueRange, upperHueValue + hueRange, hueMaskUpper);
// add this mask to the other one
hueMask = hueMask | hueMaskUpper;
}
// now we have to filter out all the pixels where saturation and value do not fit the limits:
cv::Mat saturationMask = hsvChannels[1] > minSaturation;
cv::Mat valueMask = hsvChannels[2] > minValue;
hueMask = (hueMask & saturationMask) & valueMask;
cv::imshow("desired color", hueMask);
// now perform the line detection
std::vector<cv::Vec4i> lines;
cv::HoughLinesP(hueMask, lines, 1, CV_PI / 360, 50, 50, 10);
// draw the result as big green lines:
for (unsigned int i = 0; i < lines.size(); ++i)
{
cv::line(input, cv::Point(lines[i][0], lines[i][1]), cv::Point(lines[i][2], lines[i][3]), cv::Scalar(0, 255, 0), 5);
}
cv::imwrite("C:/StackOverflow/Output/coloredLines_mask.png", hueMask);
cv::imwrite("C:/StackOverflow/Output/coloredLines_detection.png", input);
cv::imshow("input", input);
cv::waitKey(0);
return 0;
}
使用此输入图像:
将提取这种“红色”颜色(调整hueValue 和hueRange 以检测不同的颜色):
并且 HoughLinesP 从掩码中检测到这些行(应该与 HoughLines 类似地使用):
这是另一组非线条的图像...
关于您的不同问题:
有两个函数 HoughLines 和 HoughLinesP。 HoughLines 不会提取线条长度,但您可以在后处理中通过再次检查边缘掩码(HoughLines 输入)的哪些像素对应于提取的线条来计算它。
-
参数:
image - 边缘图像(应该清晰吗?)
线条 - 由角度和位置给出的线条,没有长度或某物。它们被无限长地解释
rho - 累加器分辨率。越大,在略微扭曲的线条的情况下应该越健壮,但提取线条的位置/角度越不准确
阈值 - 越大误报越少,但你可能会错过一些行
theta - 角度分辨率:越小,可以检测到的不同线越多(取决于方向)。如果您的线的方向不适合角度步长,则可能无法检测到该线。例如,如果您CV_PI/180 将检测到1° 分辨率,如果您的线路具有0.5°(例如33.5°)方向,则可能会被错过。
我对所有参数都不是很确定,也许你得看看关于霍夫线检测的文献,或者其他人可以在这里添加一些提示。
如果你改为使用cv::HoughLinesP,将检测到具有起点和终点的线段,这更容易解释,你可以从cv::norm(cv::Point(lines[i][0], lines[i][1]) - cv::Point(lines[i][2], lines[i][3]))计算线段长度