OpenCV去除綠幕摳圖源碼
更新時間:2022年05月13日 14:59:52 作者:拽拽就是我
這篇文章主要介紹了OpenCV去除綠幕摳圖,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
綠布原圖

摳圖后的圖片

源碼
#include <opencv2/opencv.hpp>
#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
using namespace cv;
using namespace std;
int main()
{
//1、設(shè)置需要去除的顏色
//2、顏色比對
//3、展示效果
//只有png有透明度空間,jpg是沒有透明度空間的
Mat srcImg = imread("E:/img/lvbu.jpg", -1);
cout << srcImg.channels() << endl;
Vec3b color(0, 255, 0); //綠色
//int tempr = 0;
int tempc = 0;
//先把圖片放大,做完摳圖后再縮小。
Mat temp;
//轉(zhuǎn)換圖片,增加透明區(qū)域
cvtColor(srcImg, temp, COLOR_RGB2BGRA);
for (int i = 0; i < srcImg.rows; ++i) {
for (int j = 0; j < srcImg.cols; ++j) {
Vec3b &pixel = srcImg.at<Vec3b>(i, j);
Vec4b &pixel_temp = temp.at<Vec4b>(i, j);
if (pixel[0] <= 30 && pixel[1] >= 210 && pixel[2] <= 30) {
tempc = j + 1; //把符合要求的下一個點也摳掉
pixel_temp[3] = 0;
//pixel[0] = 255;
//pixel[1] = 255;
//pixel[2] = 255;
}
else if (tempc == j - 1) {
pixel_temp[3] = 0;
/*pixel[0] = 255;
pixel[1] = 255;
pixel[2] = 255;*/
}
}
}
imshow("result", temp);
imwrite("E:/img/result.png", temp);
waitKey(0);
return 0;
}到此這篇關(guān)于OpenCV去除綠幕 摳圖的文章就介紹到這了,更多相關(guān)OpenCV摳圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++?基本數(shù)據(jù)類型中int、long等整數(shù)類型取值范圍及原理分析
這篇文章主要介紹了C++?基本數(shù)據(jù)類型中int、long等整數(shù)類型取值范圍及原理分析,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-11-11

