1. 程式人生 > >OpenCV影象分割實戰C++(一)Grabcut摳圖與證件照背景替換

OpenCV影象分割實戰C++(一)Grabcut摳圖與證件照背景替換

Grabcut摳圖

步驟:

  • 輸入原影象
  • 矩形輸入
  • 開始分類
  • GMM描述
  • GMM訓練分類
  • Graph Cut分類
  • 不斷迭代直到收斂分類

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 <opencv2/opencv.hpp>
#include <opencv2/xfeatures2d.hpp>
#include<opencv2/face.hpp>
#include<iostream> #include<math.h> #include <string> #include<fstream> 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 } }

結果:
這裡寫圖片描述
這裡寫圖片描述

證件照背景替換
步驟:

  • 開始
  • 資料組裝
  • KMeans分割
  • 背景去除
  • 遮罩生成
  • 遮罩模糊
  • 通道混合輸出

程式碼:

#include <opencv2/opencv.hpp>
#include <opencv2/xfeatures2d.hpp>
#include<opencv2/face.hpp>
#include<iostream>
#include<math.h>
#include <string> 
#include<fstream> 

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<Vec3b>(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<Vec3b>(row, col)[0] = 0;
                dst.at<Vec3b>(row, col)[1] = 0;
                dst.at<Vec3b>(row, col)[2] = 0;
                mask.at<uchar>(row, col) = 0;
            }
            else {
                dst.at<Vec3b>(row, col) = src.at<Vec3b>(row, col);
                mask.at<uchar>(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<uchar>(row, col);
            if (m == 255) {
                result.at<Vec3b>(row, col) = src.at<Vec3b>(row, col);//前景
            }
            else if (m == 0) {
                result.at<Vec3b>(row, col) = color; // 背景
            }
            else {//因為高斯模糊的關係,所以mask元素的顏色除了黑白色還有黑白邊緣經過模糊後的非黑白值
                w = m / 255.0;
                b1 = src.at<Vec3b>(row, col)[0];
                g1 = src.at<Vec3b>(row, col)[1];
                r1 = src.at<Vec3b>(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<Vec3b>(row, col)[0] = b;//最終邊緣顏色值
                result.at<Vec3b>(row, col)[1] = g;
                result.at<Vec3b>(row, col)[2] = r;

            }
        }
    }
    cout << "time=" << (getTickCount() - time) / getTickFrequency() << endl;
    imshow("backgroud repalce", result);
    waitKey(0);
}

結果:
這裡寫圖片描述