绿布原图
抠图后的图片
#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抠图内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!