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抠图内容请搜索编程宝库以前的文章,希望以后支持编程宝库

本文为大家分享了C语言实现简单弹跳小球的具体代码,供大家参考。本节利用 printf 函数 实现一个在屏幕上弹跳的小球,内容简单容易入门,这也是以后我们在设计更多游戏中可能用到的东西。绘制静止的小球我们将以如图坐标系 ...