在您的问题中生成图像的代码可以在your previous question 中找到。
所以我们知道洪水填充区域的值为127。
从这张图开始,你可以很容易的得到泛洪区域的掩码为:
Mat1b mask = (img == 127);
单通道掩码的值为黑色0 或白色255。
如果想要彩色图片,需要创建一个和img一样大小的黑色初始化图片,并根据蒙版设置像素为你喜欢的颜色(这里是绿色):
// Black initialized image, same size as img
Mat3b out(img.rows, img.cols, Vec3b(0,0,0));
Scalar some_color(0,255,0);
out.setTo(some_color, mask);
参考代码:
#include <opencv2/opencv.hpp>
using namespace cv;
int main()
{
Mat1b img = imread("path_to_floodfilled_image", IMREAD_GRAYSCALE);
// 127 is the color of the floodfilled region
Mat1b mask = (img == 127);
// Black initialized image, same size as img
Mat3b out(img.rows, img.cols, Vec3b(0,0,0));
Scalar some_color(0,255,0);
out.setTo(some_color, mask);
// Show results
imshow("Flood filled image", img);
imshow("Mask", mask);
imshow("Colored mask", out);
waitKey();
return 0;
}