您可以使用floodFill来做到这一点,假设您知道要填充的形状内的一个点。
结果从您的“通道红色”开始:
代码:
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
int main()
{
// Your image
Mat1b img = imread("path_to_image", IMREAD_GRAYSCALE);
// Assume you know a point inside the shape
Point seed(930, 370);
// Apply floodfill
floodFill(img, seed, Scalar(255));
// Show image
imshow("Result", img);
waitKey();
return 0;
}
更新
用 填充两个蒙版中的轮廓后drawContours(... CV_FILLED)
,您可以简单地对这两个蒙版进行异或运算:
代码:
#include <opencv2\opencv.hpp>
#include <vector>
#include <algorithm>
using namespace std;
using namespace cv;
int main()
{
// Load the two mask
Mat1b channel_red_mask = imread("channel_red.png", IMREAD_GRAYSCALE);
Mat1b channel_blue_mask = imread("channel_blue.png", IMREAD_GRAYSCALE);
// Use just the bottom right part
Rect roi(Point(800, 270), Point(channel_red_mask.cols, channel_red_mask.rows));
channel_red_mask = channel_red_mask(roi).clone();
channel_blue_mask = channel_blue_mask(roi).clone();
// Fill all contours, in both masks
{
vector<vector<Point>> contours;
findContours(channel_red_mask.clone(), contours, RETR_LIST, CHAIN_APPROX_SIMPLE);
for (int i = 0; i < contours.size(); ++i)
{
drawContours(channel_red_mask, contours, i, Scalar(255), CV_FILLED);
}
}
{
vector<vector<Point>> contours;
findContours(channel_blue_mask.clone(), contours, RETR_LIST, CHAIN_APPROX_SIMPLE);
for (int i = 0; i < contours.size(); ++i)
{
drawContours(channel_blue_mask, contours, i, Scalar(255), CV_FILLED);
}
}
// XOR the masks
Mat1b xored = channel_red_mask ^ channel_blue_mask;
imshow("XOR", xored);
waitKey();
return 0;
}