iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > 其他教程 >OpenCV去除绿幕抠图源码
  • 112
分享到

OpenCV去除绿幕抠图源码

2024-04-02 19:04:59 112人浏览 安东尼
摘要

绿布原图 抠图后的图片 源码 #include <OpenCV2/opencv.hpp> #include <iOStream> #include <

绿布原图

绿布原图

抠图后的图片

在这里插入图片描述

源码

#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;
                
            }
        }     
    }
    imshow("result", temp);
    imwrite("E:/img/result.png", temp);
    waiTKEy(0);
    return 0;
}

到此这篇关于OpenCV去除绿幕 抠图的文章就介绍到这了,更多相关OpenCV抠图内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: OpenCV去除绿幕抠图源码

本文链接: https://www.lsjlt.com/news/148747.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作