OpenCV實現視頻綠幕背景替換功能的示例代碼

1、概述

案例:使用OpenCV實現視頻綠幕背景替換

算法步驟:

1.初始化VideoCapture並使用其open方法加載視頻

2.while循環加讀取frame capture.read(frame)

3.將frame轉hsv色彩空間

4.使用inRange函數生成遮罩mask

5.使用形態學操作降噪+邊緣平滑

6.使用resize將背景圖片的大小搞成視頻幀圖片的大小

7.創建一個目標Mat用於存放融合後的圖像(CV_8UC3)

8.向目標Mat中填入,指定的像素

9.循環輸出Mat

2、代碼示例

Vide_GreenCurtain_Background_Replacement::Vide_GreenCurtain_Background_Replacement(QWidget *parent)
    : MyGraphicsView{parent}
{
    this->setWindowTitle("視頻綠幕背景替換");
}
 
void Vide_GreenCurtain_Background_Replacement::dropEvent(QDropEvent *event){
    const char *filePath= "/Users/yangwei/Documents/tony/opencv/課程配套代碼與圖片/代碼與圖片/01.mp4";
    showVideoGreenCurtainBackgroundReplacement(filePath);
}
 
void Vide_GreenCurtain_Background_Replacement::showVideoGreenCurtainBackgroundReplacement(const char* filePath){
    background1 = imread("/Users/yangwei/Downloads/5bd38a8bd51c7f866b7a5b397b8c1807.jpeg");//海底世界
    background2 = imread("/Users/yangwei/Downloads/3e6d749dfbec37b624c387767a04f34e.jpeg");//m78星雲
    VideoCapture videoCapture;
    videoCapture.open(filePath);
    if(!videoCapture.isOpened()){//視頻是否打開瞭
        qDebug()<<"視頻打開失敗";
        return;
    }
    Mat frame,hsv;
    Mat mask;
    while(videoCapture.read(frame)){
        cvtColor(frame,hsv,COLOR_BGR2HSV);//將圖像轉為hsv色彩空間
        inRange(hsv,Scalar(35, 43, 46), Scalar(155, 255, 255),mask);//使用inRange過濾像素並生成遮罩
        //使用形態學閉操作去除圖像上的幹擾白點
        Mat kernel = getStructuringElement(MORPH_RECT,Size(3,3),Point(-1,-1));
        morphologyEx(mask,mask,MORPH_CLOSE,kernel,Point(-1,-1));
        //使用形態學腐蝕操作對mask邊緣進行腐蝕(去掉邊緣白色)
        erode(mask,mask,kernel);
        //使用高斯模糊平滑前景與背景區域的過度(此處指的是黑白過度處)
        GaussianBlur(mask,mask,Size(3,3),0,0);
        resizeImage(frame);
        showResult(frame,mask);
        waitKey(1);
    }
}
 
/**
 * 將圖像調整到指定的大小
 * @brief Vide_GreenCurtain_Background_Replacement::resizeImage
 * @param target
 */
void Vide_GreenCurtain_Background_Replacement::resizeImage(Mat &frame){
    qDebug()<<"width:"<<frame.cols<<"---->height:"<<frame.rows;
 
    cv::resize(background1,background1,frame.size());
    qDebug()<<"width:"<<background1.cols<<"---->height:"<<background1.rows;
}
 
/**
 * 填充像素輸出指定的圖像
 * @brief Vide_GreenCurtain_Background_Replacement::showResult
 * @param result
 */
void Vide_GreenCurtain_Background_Replacement::showResult(Mat &frame,Mat mask){
    Mat result = Mat::zeros(frame.size(),CV_8UC3);
    int width = frame.cols;
    int height = frame.rows;
    int dims = frame.channels();
    int m = 0;
    double wt = 0;
 
    int r = 0, g = 0, b = 0;
    int r1 = 0, g1 = 0, b1 = 0;
    int r2 = 0, g2 = 0, b2 = 0;
    for(int row=0;row<height;row++){
        uchar *currentImage = frame.ptr<uchar>(row);//原始幀圖像的一列像素
        uchar *bgImage = background1.ptr<uchar>(row);//背景圖像的一列像素
        uchar *maskImage = mask.ptr<uchar>(row);//遮罩的一列像素
        uchar *resultImage = result.ptr<uchar>(row);//最終輸出結果的一列像素
        for(int col=0;col<width;col++){
            m = *maskImage++;//取出像素
            if(m==255){//背景
                *resultImage++ = *bgImage++;
                *resultImage++ = *bgImage++;
                *resultImage++ = *bgImage++;
                currentImage+=3;
            }else if(m==0){//前景
                *resultImage++ = *currentImage++;
                *resultImage++ = *currentImage++;
                *resultImage++ = *currentImage++;
                bgImage+=3;
            }else{//過度部分像素
                b1 = *resultImage++;
                g1 = *resultImage++;
                r1 = *resultImage++;
 
                b2 = *currentImage++;
                g2 = *currentImage++;
                r2 = *currentImage++;
 
                // 權重
                wt = m / 255.0;
 
                // 緩和權重
                b = b1*wt + b2*(1.0 - wt);
                g = g1*wt + g2*(1.0 - wt);
                r = r1*wt + r2*(1.0 - wt);
 
                *resultImage++ = b;
                *resultImage++ = g;
                *resultImage++ = r;
            }
 
        }
    }
    imshow("result",result);
}

到此這篇關於OpenCV實現視頻綠幕背景替換功能的示例代碼的文章就介紹到這瞭,更多相關OpenCV視頻綠幕背景替換內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: