在绘制轮廓时,会用到drawContours。如果是绘制整幅图片所有外轮廓,同时是同一种颜色,使用方法为:
drawContours(InputImage, contours, -1, Scalar(255));
如果想要绘制图片中的部分外轮廓,同时不同轮廓显示不同颜色,书中或网上的方法为:
for( int i = 0; i >=0; i = hierarchy[i][0])
{
cv::Scalar color( rand()&255, rand()&255, rand()&255 );
cv::drawContours( imgEmpty, outerline, i, color, CV_FILLED, 2, hierarchy );
}
但这种方法编译器会提示内存溢出。
正确方法改为:
for (int i = 0; i >=0; i = hierarchy[i][0])
{
cv::Scalar color(rand() & 255, rand() & 255, rand() & 255);
cv::drawContours(input1, contours, i, color, 2, 8, hierarchy,0,Point());
}
也可以写做:
for (int i = 0; i
cv::Scalar color(rand() & 255, rand() & 255, rand() & 255);
cv::drawContours(input1, contours, i, color, 2, 8, hierarchy,0,Point());
}