这篇文章主要介绍了OpenCV去除绿幕抠图,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
绿布原图
抠图后的图片
源码
#include <opencv2/opencv.hpp>
#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
using namespace cv;
using namespace std;
int main()
{
//1、设置需要去除的颜色
//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;
//转换图片,增加透明区域
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;
}
到此这篇关于OpenCV去除绿幕 抠图的文章就介绍到这了,更多相关OpenCV抠图内容请搜索编程学习网以前的文章希望大家以后多多支持编程学习网!
沃梦达教程
本文标题为:OpenCV去除绿幕抠图源码


基础教程推荐
猜你喜欢
- C语言基础全局变量与局部变量教程详解 2022-12-31
- C语言 structural body结构体详解用法 2022-12-06
- C++中的atoi 函数简介 2023-01-05
- C++使用easyX库实现三星环绕效果流程详解 2023-06-26
- 一文带你了解C++中的字符替换方法 2023-07-20
- C/C++编程中const的使用详解 2023-03-26
- C利用语言实现数据结构之队列 2022-11-22
- 如何C++使用模板特化功能 2023-03-05
- 详解c# Emit技术 2023-03-25
- C++详细实现完整图书管理功能 2023-04-04