Grabcut抠图
步骤:
API:
void grabCut(
InputArray img, // 待分割图像,8bit,3通道
// 输入输出参数,保存处理后的结果,8bit单通道掩码(与img同rows cols),mask元素值只能为 GC_BGD, GC_FGD, GC_PR_BGD, GC_PR_FGD 之一
InputOutputArray mask, // 如果没有手动标记 GC_BGD或GC_FGD ,那么结果只会有 GC_PR_BGD或GC_PR_FGD
Rect rect, // 当 mode=GC_INIT_WITH_RECT时使用,rect外部的为GC_BGD,rect内部的为GC_FGD
InputOutputArray bgdModel, // 背景模型(内部使用)
InputOutputArray fgdModel, //前景模型(内部使用)
int iterCount, // 迭代次数,必须大于0
int mode = GC_EVAL // GC_INIT_WITH_RECT表示用矩形框初始化Grabcut,GC_INIT_WITH_MASK表示用掩码图像初始化Grabcut, GC_EVAL表示执行分割
);
代码:
#include
#include
#include
#include
#include
#include
#include
using namespace cv::face;
using namespace cv;
using namespace std;
using namespace cv::xfeatures2d;
int numRun = 0;//记录run了几次
Rect rect;
bool init = false;
Mat src, mask, bgmodel, fgmodel;
void showImage()//显示选择的前景区域
{
Mat result, binMask;
binMask.create(mask.size(), CV_8UC1);
binMask = mask & 1;//&=操作符重载
if (init)//init后给result设置背景色,前景色
{
cout << "binMask depth=" << binMask.depth() << ",type=" << binMask.type() << endl;
src.copyTo(result, binMask);
}
else
{
src.copyTo(result);
}
rectangle(result, rect, Scalar(0, 0, 255), 2, 8);//绘制红色的矩形框
imshow("src", result);
}
void setROIMask()//设置背景 前景 区域
{ // GC_FGD = 1 // 属于前景色的像素
// GC_BGD =0; // 属于背景色的像素
// GC_PR_FGD = 3 // 可能属于前景的像素
// GC_PR_BGD = 2 // 可能属于背景的像素
mask.setTo(Scalar::all(GC_BGD));//设置为Grabcut的背景色
rect.x = max(0, rect.x);//max min都是防止rect未初始化导致的差错
rect.y = max(0, rect.y);
rect.width = min(rect.width, src.cols - rect.x);
rect.height = min(rect.height, src.rows - rect.y);
mask(rect).setTo(Scalar(GC_PR_FGD));//rect区域设置为Grabcut的前景, mask(rect)获取的Mat也是浅拷贝,指针还是指向原mask矩阵
}
void onMouse(int event, int x, int y, int flags, void* param)//鼠标响应事件
{
switch (event)
{
case EVENT_LBUTTONDOWN://鼠标左键按下事件
rect.x = x;
rect.y = y;
rect.width = 1;
rect.height = 1;
init = false;
numRun = 0;
break;
case EVENT_MOUSEMOVE://鼠标移动事件
if (flags&EVENT_FLAG_LBUTTON)//左键按下
{
rect = Rect(Point(rect.x, rect.y), Point(x, y));//随鼠标移动的矩形框 左上 右下
showImage();
}
break;
case EVENT_LBUTTONUP://鼠标左键抬起事件
if (rect.width > 1 && rect.height > 1)
{
setROIMask();
showImage();
}
break;
default:
break;
}
}
void runGrabCut()// Grabcut抠图,算法耗时
{
if (rect.width < 2 || rect.height < 2)
return;//框太小
if (init)
{
grabCut(src, mask, rect, bgmodel, fgmodel, 1, GC_EVAL);//分割,抠图
}
else
{
grabCut(src, mask, rect, bgmodel, fgmodel, 1, GC_INIT_WITH_RECT);// 初始化,也有一定的图像分割的作用,但是上面的执行分割可以在此基础上更进一步的分割
init = true;
}
}
int main()
{
src = imread("C:/Users/Administrator/Desktop/pic/5.jpg");
mask.create(src.size(), CV_8UC1);
mask.setTo(Scalar::all(GC_BGD));//背景为黑色
namedWindow("src", CV_WINDOW_AUTOSIZE);
setMouseCallback("src", onMouse, 0);
imshow("src", src);
while (true)
{
char c = (char)waitKey(0);
if (c == 'b') // 按字母 b
{
runGrabCut();
numRun++;
showImage();
printf("current iteative times : %d\n", numRun);
}
if ((int)c == 27) break;//esc
}
}
证件照背景替换
步骤:
代码:
#include
#include
#include
#include
#include
#include
#include
using namespace cv::face;
using namespace cv;
using namespace std;
using namespace cv::xfeatures2d;
int main() {
Mat src = imread("C:/Users/Administrator/Desktop/pic/z5.jpg");
imshow("src", src);
//组装数据
int width = src.cols;
int height = src.rows;
int samplecount = width * height;
int dims = src.channels();
//行数为src的像素点数,列数为通道数,每列数据分别为src的bgr,从上到下 从左到右顺序读数据
Mat points(samplecount, dims, CV_32F, Scalar(10));
int ind = 0;
for (int row = 0; row < height; row++) {
for (int col = 0; col < width; col++) {
ind = row * width + col;//
Vec3b bgr = src.at(row, col);
points.at<float>(ind, 0) = static_cast<int>(bgr[0]);
points.at<float>(ind, 1) = static_cast<int>(bgr[1]);
points.at<float>(ind, 2) = static_cast<int>(bgr[2]);
}
}
//运行kmeans
int numCluster = 4;
Mat labels;
Mat centers;
TermCriteria criteria = TermCriteria(TermCriteria::EPS + TermCriteria::COUNT, 10, 0.1);
kmeans(points, numCluster, labels, criteria, 3, KMEANS_PP_CENTERS, centers);
//去背景+遮罩生成
Mat mask = Mat::zeros(src.size(), CV_8UC1);
int index = src.rows * 2 + 2;//不取边缘的左上点,往里靠2个位置
int cindex = labels.at<int>(index, 0);
int height1 = src.rows;
int width1 = src.cols;
Mat dst;//人的轮廓周围会有一些杂点,所以需要腐蚀和高斯模糊取干扰
src.copyTo(dst);
for (int row = 0; row < height1; row++) {
for (int col = 0; col < width1; col++) {
index = row * width1 + col;
int label = labels.at<int>(index, 0);
if (label == cindex) {
dst.at(row, col)[0] = 0;
dst.at(row, col)[1] = 0;
dst.at(row, col)[2] = 0;
mask.at(row, col) = 0;
}
else {
dst.at(row, col) = src.at(row, col);
mask.at(row, col) = 255;//人脸部分设为白色,以便于下面的腐蚀与高斯模糊
}
}
}
imshow("dst", dst);
imshow("mask", dst);
//腐蚀+高斯模糊
Mat k = getStructuringElement(MORPH_RECT, Size(3, 3));
erode(mask, mask, k);
GaussianBlur(mask, mask, Size(3, 3), 0, 0);
imshow("gaosimohu", mask);
//通道混合
RNG rng(12345);
Vec3b color;
color[0] = 180;//rng.uniform(0, 255);
color[1] =180;//rng.uniform(0, 255);
color[2] =238;//rng.uniform(0, 255);
Mat result(src.size(), src.type());
double w = 0.0;
int b = 0, g = 0, r = 0;
int b1 = 0, g1 = 0, r1 = 0;
int b2 = 0, g2 = 0, r2 = 0;
double time = getTickCount();
for (int row = 0; row < height1; row++) {
for (int col = 0; col < width; col++) {
int m = mask.at(row, col);
if (m == 255) {
result.at(row, col) = src.at(row, col);//前景
}
else if (m == 0) {
result.at(row, col) = color; // 背景
}
else {//因为高斯模糊的关系,所以mask元素的颜色除了黑白色还有黑白边缘经过模糊后的非黑白值
w = m / 255.0;
b1 = src.at(row, col)[0];
g1 = src.at(row, col)[1];
r1 = src.at(row, col)[2];
b2 = color[0];
g2 = color[0];
r2 = color[0];
b = b1 * w + b2 * (1.0 - w);
g = g1 * w + g2 * (1.0 - w);
r = r1 * w + r2 * (1.0 - w);
result.at(row, col)[0] = b;//最终边缘颜色值
result.at(row, col)[1] = g;
result.at(row, col)[2] = r;
}
}
}
cout << "time=" << (getTickCount() - time) / getTickFrequency() << endl;
imshow("backgroud repalce", result);
waitKey(0);
}