我一直在试验opencv洪水填充,似乎遗漏了一些东西。
在解决另一个问题(https://stackoverflow.com/questions/25433662/display-regions-detected-by-cvpyrsegmentation-in-opencv)时,我创建了一个简单的测试应用程序来处理洪水填充:
void addRect(cv::Mat image, cv::Point start, cv::Scalar color, int height, int width)
{
cv::Size size(width, height);
cv::Rect rect(start, size);
cv::rectangle(image, rect, color, CV_FILLED );
}
void fillImage(cv::Mat image, cv::Point start, cv::Point end, cv::Scalar fill, cv::Scalar maskColor)
{
cv::Rect area(cv::Point(start.x, start.y), cv::Point(end.x, end.y) );
cv::Mat mask(image.rows + 2, image.cols + 2, CV_8UC1, cvScalar(0) );
cv::rectangle(image, area, cvScalar(1), CV_FILLED );
cv::floodFill(image, mask, start, fill);
}
int main(void)
{
printf("This is a test application to test the floodFill function in opencv.\n");
cv::Mat baseImage(512, 512, CV_8UC3, cvScalar(0,0,0) );
addRect(baseImage, cv::Point(50,50), cvScalar(0,0,255), 100, 100);
cv::Mat fillImageHit = baseImage.clone();
fillImage(fillImageHit, cv::Point(55, 100), cv::Point(200,200), cvScalar(255,0,0), cvScalar(0,0,255) );
cvNamedWindow("Base Image");
cv::imshow("Base Image", baseImage);
cvNamedWindow("Hit Fill Image");
cv::imshow("Hit Fill Image", fillImageHit);
cvWaitKey(0);
cvDestroyWindow("Base Image");
cvDestroyWindow("Hit Fill Image");
}
这是非常基本的,但是完整的面具区域已经填满;填充不会在矩形的边缘结束。任何提示?
答案 0 :(得分:0)
不是输入整个图像进行填充,而是使用矩形区域:
void fillImage(cv::Mat image, cv::Point start, cv::Point end, cv::Scalar fill, cv::Scalar maskColor)
{
cv::Rect area(cv::Point(start.x, start.y), cv::Point(end.x, end.y) );
cv::Mat mask(image.rows + 2, image.cols + 2, CV_8UC1, cvScalar(0) );
cv::rectangle(image, area, cvScalar(1), CV_FILLED );
cv::floodFill(cv::Mat(image,area), cv::Mat(mask,area) , start, fill);
}
我没有在这里设置环境,但我认为它应该有效!