OpenCV實(shí)現(xiàn)視頻綠幕背景替換功能的示例代碼
1、概述
案例:使用OpenCV實(shí)現(xiàn)視頻綠幕背景替換
算法步驟:
1.初始化VideoCapture并使用其open方法加載視頻
2.while循環(huán)加讀取frame capture.read(frame)
3.將frame轉(zhuǎn)hsv色彩空間
4.使用inRange函數(shù)生成遮罩mask
5.使用形態(tài)學(xué)操作降噪+邊緣平滑
6.使用resize將背景圖片的大小搞成視頻幀圖片的大小
7.創(chuàng)建一個(gè)目標(biāo)Mat用于存放融合后的圖像(CV_8UC3)
8.向目標(biāo)Mat中填入,指定的像素
9.循環(huán)輸出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()){//視頻是否打開(kāi)了
qDebug()<<"視頻打開(kāi)失敗";
return;
}
Mat frame,hsv;
Mat mask;
while(videoCapture.read(frame)){
cvtColor(frame,hsv,COLOR_BGR2HSV);//將圖像轉(zhuǎn)為hsv色彩空間
inRange(hsv,Scalar(35, 43, 46), Scalar(155, 255, 255),mask);//使用inRange過(guò)濾像素并生成遮罩
//使用形態(tài)學(xué)閉操作去除圖像上的干擾白點(diǎn)
Mat kernel = getStructuringElement(MORPH_RECT,Size(3,3),Point(-1,-1));
morphologyEx(mask,mask,MORPH_CLOSE,kernel,Point(-1,-1));
//使用形態(tài)學(xué)腐蝕操作對(duì)mask邊緣進(jìn)行腐蝕(去掉邊緣白色)
erode(mask,mask,kernel);
//使用高斯模糊平滑前景與背景區(qū)域的過(guò)度(此處指的是黑白過(guò)度處)
GaussianBlur(mask,mask,Size(3,3),0,0);
resizeImage(frame);
showResult(frame,mask);
waitKey(1);
}
}
/**
* 將圖像調(diào)整到指定的大小
* @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);//最終輸出結(jié)果的一列像素
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{//過(guò)度部分像素
b1 = *resultImage++;
g1 = *resultImage++;
r1 = *resultImage++;
b2 = *currentImage++;
g2 = *currentImage++;
r2 = *currentImage++;
// 權(quán)重
wt = m / 255.0;
// 緩和權(quán)重
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);
}到此這篇關(guān)于OpenCV實(shí)現(xiàn)視頻綠幕背景替換功能的示例代碼的文章就介紹到這了,更多相關(guān)OpenCV視頻綠幕背景替換內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解C語(yǔ)言中freopen()函數(shù)和fclose()函數(shù)的用法
這篇文章主要介紹了詳解C語(yǔ)言中freopen()函數(shù)和fclose()函數(shù)的用法,是C語(yǔ)言入門(mén)學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-08-08
C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單酒店管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單酒店管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
Visual C++ 常用數(shù)據(jù)類(lèi)型轉(zhuǎn)換方法詳解
本文純粹是總結(jié)一下有關(guān)類(lèi)型轉(zhuǎn)換的貼子,需要的朋友可以參考下2017-06-06
C語(yǔ)言關(guān)鍵字auto與register及static專(zhuān)項(xiàng)詳解
這篇文章主要解釋了c語(yǔ)言中什么是數(shù)據(jù)類(lèi)型,什么是變量,他們的真正含義是什么。分析了屬性關(guān)鍵字auto,register和static的用法2022-07-07
C語(yǔ)言實(shí)現(xiàn)BMP圖像處理(哈夫曼編碼)
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)BMP圖像哈夫曼編碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10
delete[] p->elems和free(p->elems)區(qū)別介紹
delete[]和free()都是釋放內(nèi)存的函數(shù),但它們具有不同的使用方法和適用情況,這篇文章主要介紹了delete[] p->elems和free(p->elems)有什么區(qū)別,需要的朋友可以參考下2023-04-04

