/*
* cloning.cpp
*
* Author:
* Siddharth Kherada <siddharthkherada27[at]gmail[dot]com>
*
* This tutorial demonstrates how to use OpenCV seamless cloning
* module.
*
* 1- Normal Cloning
* 2- Mixed Cloning
* 3- Monochrome Transfer单色转移
* 4- Color Change
* 5- Illumination change
* 6- Texture Flattening
* The program takes as input a source and a destination image (for 1-3 methods)
* and outputs the cloned image.
* Step 1:
* -> In the source image, select the region of interest by left click mouse button. A Polygon ROI will be created by left clicking mouse button.
* -> To set the Polygon ROI, click the right mouse button or 'd' key.
* -> To reset the region selected, click the middle mouse button or 'r' key.
* Step 2:
* -> In the destination image, select the point where you want to place the ROI in the image by left clicking mouse button.
* -> To get the cloned result, click the right mouse button or 'c' key.
* -> To quit the program, use 'q' key.
*
* Result: The cloned image will be displayed.
*/
#include "opencv2/photo.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/core.hpp"
#include <iostream>
// we're NOT "using namespace std;" here, to avoid collisions between the beta variable and std::beta in c++17
using std::cin;
using std::cout;
using std::endl;
using std::string;
using namespace cv;
Mat img0, img1, img2, res, res1, final, final1, blend;
Point point;
int drag = 0;
int destx, desty;
int numpts = 100;//最多选100个点
Point* pts = new Point[100];
Point* pts2 = new Point[100];
Point* pts_diff = new Point[100];
int var = 0;
int flag = 0, flag1 = 0, flag4 = 0;
int minx, miny, maxx, maxy, lenx, leny;
int minxd, minyd, maxxd, maxyd, lenxd, lenyd;
int channel, num, kernel_size;
float alpha,beta;
float red, green, blue;
float low_t, high_t;
void source(int, int, int, int, void*);
void destination(int, int, int, int, void*);
void checkfile(char*);
//源图像上鼠标回调
void source(int event, int x, int y, int, void*)
{
if (event == EVENT_LBUTTONDOWN && !drag) //左键按下,第一个点选择前没有拖拽drag=0
{
if(flag1 == 0)
{
if(var==0)//点索引
img1 = img0.clone();//克隆源图像
point = Point(x, y);//鼠标左键单击点坐标
circle(img1,point,2,Scalar(0, 0, 255),-1, 8, 0);//画点 红色
pts[var] = point;
var++;//下一点索引
drag = 1;//点击一个点后触发拖拽模式
if(var>1)//有两个点后,绘制当前点与上一点的连线
line(img1,pts[var-2], point, Scalar(0, 0, 255), 2, 8, 0);
imshow("Source", img1);//显示源图像的克隆
}
}
if (event == EVENT_LBUTTONUP && drag)//左键抬起,拖拽
{
imshow("Source", img1);
drag = 0;// 左键抬起后结束拖拽模式
}
if (event == EVENT_RBUTTONDOWN)//单击右键按下
{
flag1 = 1;
img1 = img0.clone();
for(int i = var; i < numpts ; i++)//从第var+1个点开始 都设置位当前右键单击点
pts[i] = point;
if(var!=0)
{
const Point* pts3[1] = {&pts[0]};//起点开始的所有点
polylines( img1, pts3, &numpts,1, 1, Scalar(0,0,0), 2, 8, 0);//绘制多边形轮廓线
}
for(int i=0;i<var;i++)
{
minx = min(minx,pts[i].x);//找到所有点的最小边界x
maxx = max(maxx,pts[i].x);//找到所有点的最大边界x
miny = min(miny,pts[i].y);
maxy = max(maxy,pts[i].y);
}
lenx = maxx - minx;//边界框宽度
leny = maxy - miny;//边界框高度
//边界框中心点
int mid_pointx = minx + lenx/2;
int mid_pointy = miny + leny/2;
//每个点到中心点的距离矢量
for(int i=0;i<var;i++)
{
pts_diff[i].x = pts[i].x - mid_pointx;
pts_diff[i].y = pts[i].y - mid_pointy;
}
imshow("Source", img1);
}
if (event == EVENT_RBUTTONUP)//右键弹起
{
flag = var;//已选择点数
final = Mat::zeros(img0.size(),CV_8UC3);//
res1 = Mat::zeros(img0.size(),CV_8UC1);
const Point* pts4[1] = {&pts[0]};
fillPoly(res1, pts4,&numpts, 1, Scalar(255, 255, 255), 8, 0);//填充多边形,白色, 形成掩膜
bitwise_and(img0, img0, final,res1);//多边形裁剪
imshow("Source", img1);
if(num == 4)
{
colorChange(img0,res1,blend,red,green,blue); // 对感兴趣区域进行颜色调整
imshow("Color Change Image", blend);
waitKey(0);
}
else if(num == 5)
{
illuminationChange(img0,res1,blend,alpha,beta);//改变亮度 消除高亮
imshow("Illum Change Image", blend);
waitKey(0);
}
else if(num == 6)
{
textureFlattening(img0,res1,blend,low_t,high_t,kernel_size);//纹理扁平化
imshow("Texture Flattened", blend);
waitKey(0);
}
}
if (event == EVENT_MBUTTONDOWN)//中键按下,清空,初始化
{
for(int i = 0; i < numpts ; i++)
{
pts[i].x=0;//初始化所有点为原点
pts[i].y=0;
}
var = 0;
flag1 = 0;
minx = INT_MAX; miny = INT_MAX; maxx = INT_MIN; maxy = INT_MIN;
imshow("Source", img0);
if(num == 1 || num == 2 || num == 3)
imshow("Destination",img2);
drag = 0;
}
}
//目标图像上鼠标回调
void destination(int event, int x, int y, int, void*)
{
Mat im1;
minxd = INT_MAX; minyd = INT_MAX; maxxd = INT_MIN; maxyd = INT_MIN;
im1 = img2.clone();//目标图像克隆
if (event == EVENT_LBUTTONDOWN)//左键按下
{
flag4 = 1;
if(flag1 == 1)
{
point = Point(x, y); //单击点,作为中心点
for(int i=0;i<var;i++)
{
pts2[i].x = point.x + pts_diff[i].x;//计算多边形角点
pts2[i].y = point.y + pts_diff[i].y;
}
for(int i=var;i<numpts;i++)//没有选择的点
{
pts2[i].x = point.x + pts_diff[0].x;//其他点位置同第一个点坐标
pts2[i].y = point.y + pts_diff[0].y;
}
const Point* pts5[1] = {&pts2[0]};
polylines( im1, pts5, &numpts,1, 1, Scalar(0,0,255), 2, 8, 0);//再目标图像上绘制多边形,以单击点为中心
destx = x;
desty = y;
imshow("Destination", im1);//显示克隆的目标图像
}
}
if (event == EVENT_RBUTTONUP)//右键弹起
{
for(int i=0;i<flag;i++)//遍历已选择的点
{
minxd = min(minxd,pts2[i].x);//寻找最小的x
maxxd = max(maxxd,pts2[i].x);//最大的x
minyd = min(minyd,pts2[i].y);
maxyd = max(maxyd,pts2[i].y);
}
if(maxxd > im1.size().width || maxyd > im1.size().height || minxd < 0 || minyd < 0)//超出边界
{
cout << "Index out of range" << endl;
exit(1);
}
final1 = Mat::zeros(img2.size(),CV_8UC3);
res = Mat::zeros(img2.size(),CV_8UC1);
for(int i=miny, k=minyd;i<(miny+leny);i++,k++)//遍历行
for(int j=minx,l=minxd ;j<(minx+lenx);j++,l++)//遍历列
{
for(int c=0;c<channel;c++)//遍历通道
{
final1.at<uchar>(k,l*channel+c) = final.at<uchar>(i,j*channel+c);//裁剪多边形后的final的 像素复制到final1
}
}
const Point* pts6[1] = {&pts2[0]};
fillPoly(res, pts6, &numpts, 1, Scalar(255, 255, 255), 8, 0);//以单击点为中心绘制多边形 形成掩膜res
if(num == 1 || num == 2 || num == 3)
{ //img0:目标图像,img2背景图像,res1掩膜,point目标影像的中心在背景图像上的坐标,输出blend,num融合方式NORMAL_CLONE 、MIXED_CLONE 和 MONOCHROME_TRANSFER 。
seamlessClone(img0,img2,res1,point,blend,num);//图像无缝融合。
imshow("Cloned Image", blend);
imwrite("cloned.png",blend);
waitKey(0);
}
//重置多边形角点坐标
for(int i = 0; i < flag ; i++)
{
pts2[i].x=0;
pts2[i].y=0;
}
//初始化多边形边界值
minxd = INT_MAX; minyd = INT_MAX; maxxd = INT_MIN; maxyd = INT_MIN;
}
im1.release();
}
int main()
{
cout << endl;
cout << "Cloning Module" << endl;
cout << "---------------" << endl;
cout << "Step 1:" << endl;
cout << " -> 在源图像中,单击鼠标左键选择感兴趣的区域。 左键单击鼠标按钮将创建多边形 ROI" << endl;
cout << " -> 要设置多边形 ROI,请单击鼠标右键或使用 'd' 键" << endl;
cout << " -> 要重置所选区域,请单击鼠标中键或使用“r”键。” << endl;
cout << "Step 2:" << endl;
cout << " -> 在目标图像中,单击鼠标左键选择要在图像中放置 ROI 的点。” << endl;
cout << " -> 要获得克隆的结果,请单击鼠标右键或使用“c”键。” << endl;
cout << " -> 要退出程序,请使用 'q' 键。” << endl;
cout << endl;
cout << "Options: " << endl;
cout << endl;
cout << "1) Normal Cloning " << endl;
cout << "2) Mixed Cloning " << endl;
cout << "3) Monochrome Transfer " << endl;
cout << "4) Local Color Change " << endl;
cout << "5) Local Illumination Change " << endl;
cout << "6) Texture Flattening " << endl;
cout << endl;
cout << “按数字 1-6 从上述技术中进行选择:”;
cin >> num;
cout << endl;
minx = INT_MAX; miny = INT_MAX; maxx = INT_MIN; maxy = INT_MIN;
minxd = INT_MAX; minyd = INT_MAX; maxxd = INT_MIN; maxyd = INT_MIN;
int flag3 = 0;
if(num == 1 || num == 2 || num == 3)//
{
string src,dest;
cout << "Enter Source Image: ";
cin >> src;
cout << "Enter Destination Image: ";
cin >> dest;
img0 = imread(samples::findFile(src));
img2 = imread(samples::findFile(dest));
if(img0.empty())
{
cout << "Source Image does not exist" << endl;
exit(2);
}
if(img2.empty())
{
cout << "Destination Image does not exist" << endl;
exit(2);
}
channel = img0.channels();
res = Mat::zeros(img2.size(),CV_8UC1);
res1 = Mat::zeros(img0.size(),CV_8UC1);
final = Mat::zeros(img0.size(),CV_8UC3);
final1 = Mat::zeros(img2.size(),CV_8UC3);
source image ///
namedWindow("Source", 1);
setMouseCallback("Source", source, NULL);//源图片上鼠标回调source
imshow("Source", img0);//显示源图像
/// destination image ///
namedWindow("Destination", 1);
setMouseCallback("Destination", destination, NULL);//目标图片上鼠标回调destination
imshow("Destination",img2);
}
else if(num == 4)//对感兴趣区域进行颜色调整
{
string src;
cout << "Enter Source Image: ";
cin >> src;
cout << "Enter RGB values: " << endl;
cout << "Red: ";
cin >> red;
cout << "Green: ";
cin >> green;
cout << "Blue: ";
cin >> blue;
img0 = imread(samples::findFile(src));
if(img0.empty())
{
cout << "Source Image does not exist" << endl;
exit(2);
}
res1 = Mat::zeros(img0.size(),CV_8UC1);
final = Mat::zeros(img0.size(),CV_8UC3);
source image ///
namedWindow("Source", 1);
setMouseCallback("Source", source, NULL);
imshow("Source", img0);
}
else if(num == 5)//消除高亮
{
string src;
cout << "Enter Source Image: ";
cin >> src;
cout << "alpha: ";
cin >> alpha;
cout << "beta: ";
cin >> beta;
img0 = imread(samples::findFile(src));
if(img0.empty())
{
cout << "Source Image does not exist" << endl;
exit(2);
}
res1 = Mat::zeros(img0.size(),CV_8UC1);
final = Mat::zeros(img0.size(),CV_8UC3);
source image ///
namedWindow("Source", 1);
setMouseCallback("Source", source, NULL);
imshow("Source", img0);
}
else if(num == 6)//纹理扁平化
{
string src;
cout << "Enter Source Image: ";
cin >> src;
cout << "low_threshold: ";
cin >> low_t;
cout << "high_threshold: ";
cin >> high_t;
cout << "kernel_size: ";
cin >> kernel_size;
img0 = imread(samples::findFile(src));
if(img0.empty())
{
cout << "Source Image does not exist" << endl;
exit(2);
}
res1 = Mat::zeros(img0.size(),CV_8UC1);
final = Mat::zeros(img0.size(),CV_8UC3);
source image ///
namedWindow("Source", 1);
setMouseCallback("Source", source, NULL);
imshow("Source", img0);
}
else
{
cout << "选择了错误的选项Wrong Option Chosen" << endl;
exit(1);
}
for(;;)
{
char key = (char)waitKey(0);
if(key == 'd' && flag3 == 0)
{
flag1 = 1;
flag3 = 1;
img1 = img0.clone();
for(int i = var; i < numpts ; i++)
pts[i] = point;//后续未选择的点 设置为当前单击点
if(var!=0)
{
const Point* pts3[1] = {&pts[0]};
polylines( img1, pts3, &numpts,1, 1, Scalar(0,0,0), 2, 8, 0);//绘制多边形
}
for(int i=0;i<var;i++)//寻找已单击点的边界
{
minx = min(minx,pts[i].x);
maxx = max(maxx,pts[i].x);
miny = min(miny,pts[i].y);
maxy = max(maxy,pts[i].y);
}
lenx = maxx - minx;//边界长度
leny = maxy - miny;//边界高度
int mid_pointx = minx + lenx/2;//边界框中心点
int mid_pointy = miny + leny/2;
for(int i=0;i<var;i++)
{
pts_diff[i].x = pts[i].x - mid_pointx;//所有点与中心点的距离矢量
pts_diff[i].y = pts[i].y - mid_pointy;
}
flag = var;//已计算所有点到中心点的距离矢量
final = Mat::zeros(img0.size(),CV_8UC3);//
res1 = Mat::zeros(img0.size(),CV_8UC1);
const Point* pts4[1] = {&pts[0]};
fillPoly(res1, pts4,&numpts, 1, Scalar(255, 255, 255), 8, 0);//绘制白色多边形 掩膜
bitwise_and(img0, img0, final,res1);//裁剪多边形得到final
imshow("Source", img1);
}
else if(key == 'r')
{ //重置多边形角点坐标和其他变量
for(int i = 0; i < numpts ; i++)
{
pts[i].x=0;
pts[i].y=0;
}
var = 0;
flag1 = 0;
flag3 = 0;
flag4 = 0;
minx = INT_MAX; miny = INT_MAX; maxx = INT_MIN; maxy = INT_MIN;
imshow("Source", img0);//
if(num == 1 || num == 2 || num == 3)
imshow("Destination",img2);
drag = 0;
}
else if ((num == 1 || num == 2 || num == 3) && key == 'c' && flag1 == 1 && flag4 == 1)
{
seamlessClone(img0,img2,res1,point,blend,num);//无缝融合
imshow("Cloned Image", blend);
imwrite("cloned.png",blend);
}
else if (num == 4 && key == 'c' && flag1 == 1)
{
colorChange(img0,res1,blend,red,green,blue);//感兴趣区域颜色改变
imshow("Color Change Image", blend);
imwrite("cloned.png",blend);
}
else if (num == 5 && key == 'c' && flag1 == 1) //
{
illuminationChange(img0,res1,blend,alpha,beta);//消除高亮
imshow("Illum Change Image", blend);
imwrite("cloned.png",blend);
}
else if (num == 6 && key == 'c' && flag1 == 1)
{
textureFlattening(img0,res1,blend,low_t,high_t,kernel_size);//纹理扁平化
imshow("Texture Flattened", blend);
imwrite("cloned.png",blend);
}
else if(key == 'q')
break;
}
return 0;
}
参考:
//一、 OpenCV之bitwise_and、bitwise_not等图像基本运算及掩膜
https://blog.youkuaiyun.com/u011028345/article/details/77278467?spm=1001.2101.3001.6661.1&utm_medium=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1.pc_relevant_paycolumn_v3&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1.pc_relevant_paycolumn_v3&utm_relevant_index=1
1.图像基本运算
图像的基本运算有很多种,比如两幅图像可以相加、相减、相乘、相除、位运算、平方根、对数、绝对值等;图像也可以放大、缩小、旋转,还可以截取其中的一部分作为ROI(感兴趣区域)进行操作,各个颜色通道还可以分别提取及对各个颜色通道进行各种运算操作。总之,对于图像可以进行的基本运算非常的多,只是挑了些常用的操作详解。
void add(InputArray src1, InputArray src2, OutputArray dst,InputArray mask=noArray(), int dtype=-1);//dst = src1 + src2
void subtract(InputArray src1, InputArray src2, OutputArray dst,InputArray mask=noArray(), int dtype=-1);//dst = src1 - src2
void multiply(InputArray src1, InputArray src2,OutputArray dst, double scale=1, int dtype=-1);//dst = scale*src1*src2
void divide(InputArray src1, InputArray src2, OutputArray dst,double scale=1, int dtype=-1);//dst = scale*src1/src2
void divide(double scale, InputArray src2,OutputArray dst, int dtype=-1);//dst = scale/src2
void scaleAdd(InputArray src1, double alpha, InputArray src2, OutputArray dst);//dst = alpha*src1 + src2
void addWeighted(InputArray src1, double alpha, InputArray src2,double beta, double gamma, OutputArray dst, int dtype=-1);//dst = alpha*src1 + beta*src2 + gamma
void sqrt(InputArray src, OutputArray dst);//计算每个矩阵元素的平方根
void pow(InputArray src, double power, OutputArray dst);//src的power次幂
void exp(InputArray src, OutputArray dst);//dst = e**src(**表示指数的意思)
void log(InputArray src, OutputArray dst);//dst = log(abs(src))
上述的基本操作中都属于将基础数学运算应用于图像像素的处理中,下面将着重介绍
bitwise_and、bitwise_or、bitwise_xor、bitwise_not这四个按位操作函数。
void bitwise_and(InputArray src1, InputArray src2,OutputArray dst, InputArray mask=noArray());//dst = src1 & src2
void bitwise_or(InputArray src1, InputArray src2,OutputArray dst, InputArray mask=noArray());//dst = src1 | src2
void bitwise_xor(InputArray src1, InputArray src2,OutputArray dst, InputArray mask=noArray());//dst = src1 ^ src2
void bitwise_not(InputArray src, OutputArray dst,InputArray mask=noArray());//dst = ~src
bitwise_and是对二进制数据进行“与”操作,即对图像(灰度图像或彩色图像均可)每个像素值进行二进制“与”操作,1&1=1,1&0=0,0&1=0,0&0=0
bitwise_or是对二进制数据进行“或”操作,即对图像(灰度图像或彩色图像均可)每个像素值进行二进制“或”操作,1|1=1,1|0=0,0|1=0,0|0=0
bitwise_xor是对二进制数据进行“异或”操作,即对图像(灰度图像或彩色图像均可)每个像素值进行二进制“异或”操作,1^1=0,1^0=1,0^1=1,0^0=0
bitwise_not是对二进制数据进行“非”操作,即对图像(灰度图像或彩色图像均可)每个像素值进行二进制“非”操作,~1=0,~0=1
2.掩膜(mask)
2.1在有些图像处理的函数中有的参数里面会有mask参数,即此函数支持掩膜操作,首先何为掩膜以及有什么用,如下:
数字图像处理中的掩膜的概念是借鉴于PCB制版的过程,在半导体制造中,许多芯片工艺步骤采用光刻技术,用于这些步骤的图形“底片”称为掩膜(也称作“掩模”),其作用是:在硅片上选定的区域中对一个不透明的图形模板遮盖,继而下面的腐蚀或扩散将只影响选定的区域以外的区域。
图像掩膜与其类似,用选定的图像、图形或物体,对处理的图像(全部或局部)进行遮挡,来控制图像处理的区域或处理过程。
数字图像处理中,掩模为二维矩阵数组,有时也用多值图像,图像掩模主要用于:
①提取感兴趣区,用预先制作的感兴趣区掩模与待处理图像相乘,得到感兴趣区图像,感兴趣区内图像值保持不变,而区外图像值都为0。
②屏蔽作用,用掩模对图像上某些区域作屏蔽,使其不参加处理或不参加处理参数的计算,或仅对屏蔽区作处理或统计。
③结构特征提取,用相似性变量或图像匹配方法检测和提取图像中与掩模相似的结构特征。
④特殊形状图像的制作。
2.2 在所有图像基本运算的操作函数中,凡是带有掩膜(mask)的处理函数,其掩膜都参与运算(输入图像运算完之后再与掩膜图像或矩阵运算)。
// 转换面具为灰度图像
cvtColor(faceMaskSmall, grayMaskSmall, CV_BGR2GRAY);
// 隔离图像上像素的边缘,仅与面具有关(即面具的白色区域剔除),下面函数将大于230像素的值置为0,小于的置为255
threshold(grayMaskSmall, grayMaskSmallThresh, 230, 255, CV_THRESH_BINARY_INV);
// 通过反转上面的图像创建掩码(因为不希望背景影响叠加)
bitwise_not(grayMaskSmallThresh, grayMaskSmallThreshInv);
//使用位“与”运算来提取面具精确的边界
bitwise_and(faceMaskSmall, faceMaskSmall, maskedFace, grayMaskSmallThresh);
// 使用位“与”运算来叠加面具
bitwise_and(frameROI, frameROI, maskedFrame, grayMaskSmallThreshInv);
https://blog.youkuaiyun.com/steph_curry/article/details/78785494?spm=1001.2101.3001.6650.6&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7ERate-6.pc_relevant_aa&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7ERate-6.pc_relevant_aa&utm_relevant_index=10
opencv算术运算:bitwise_and()
用bitwise_and裁剪一幅图的一部分:
#include<opencv2/opencv.hpp>
#include<iostream>
using namespace std;
using namespace cv;
int main()
{
const char* imagename = "C://Users//huashuo111//Desktop//test2.bmp";
//从文件中读入图像
Mat img = imread(imagename,IMREAD_GRAYSCALE);
//如果读入图像失败
if(img.empty())
{
fprintf(stderr, "Can not load image %s\n", imagename);
return-1;
}
// resize(img,img,Size(),0.5,0.5);
Mat mask(img.rows,img.cols,CV_8UC1,Scalar(0,0,0));
circle(mask,Point(mask.rows/2,mask.cols/2),150,CV_RGB(255,255,255),-1);
Mat r;
const uchar white=255;
bitwise_and(img,mask,r);
for(int i=0;i<r.rows;i++)
for(int j=0;j<r.cols;j++)
{
if(!mask.at<uchar>(i,j))
r.at<uchar>(i,j)=white;
}
imshow("img",img);
imshow("mask",mask);
imshow("result",r);
//此函数等待按键,按键盘任意键就返回
waitKey();
return 0;
}
//二、 cv2.fillConvexPoly()与cv2.fillPoly()填充多边形
https://www.cnblogs.com/Ph-one/p/12082692.html
#cv2.fillConvexPoly()
cv2.fillConvexPoly()函数可以用来填充凸多边形,只需要提供凸多边形的顶点即可.
我们来画一个三角形
img = np.zeros((1080, 1920, 3), np.uint8)
triangle = np.array([[0, 0], [1500, 800], [500, 400]])
cv2.fillConvexPoly(img, triangle, (255, 255, 255))
plt.imshow(img)
plt.show()
/
#cv2.fillPoly()
cv2.fillPoly()函数可以用来填充任意形状的图型.可以用来绘制多边形,工作中也经常使用非常多个边来近似的画一条曲线.cv2.fillPoly()函数可以一次填充多个图型.
img = np.zeros((1080, 1920, 3), np.uint8)
area1 = np.array([[250, 200], [300, 100], [750, 800], [100, 1000]])
area2 = np.array([[1000, 200], [1500, 200], [1500, 400], [1000, 400]])
cv2.fillPoly(img, [area1, area2], (255, 255, 255))
plt.imshow(img)
plt.show()
https://xiaotaoguo.com/p/opencv-fill-polygon/
使用 OpenCV 的 fillPoly() 函数画多边形
cv::fillPoly() 有多个重载版本,这里记录以下两个:
第一个版本
CV_EXPORTS_W void fillPoly(InputOutputArray img, InputArrayOfArrays pts,
const Scalar& color, int lineType = LINE_8, int shift = 0,
Point offset = Point() );
这个 API 比较好理解,img 是需要画多边形的图像, pts 是多边形的各个顶点,color 和 lineType 分别是多边形的颜色和边框类型,shift 和 offset 分别是对点坐标的偏移和多边形整体的偏移。需要注意的是由于这个 API 不限定用来画凸多边形,也可以用来画凹多边形或者自身交叉的多边形,所以 pts 的顺序需要保证是你想要的多边形的顶点顺序。下面用两个例子说明。
cv::Mat image(cv::Size(540, 540), CV_8UC1);
std::vector<cv::Point> fillContSingle;
//将轮廓的所有点添加到向量
fillContSingle.push_back(cv::Point(200, 100));
fillContSingle.push_back(cv::Point(200, 200));
fillContSingle.push_back(cv::Point(100, 200));
fillContSingle.push_back(cv::Point(300, 100));
cv::fillPoly( image, std::vector<std::vector<cv::Point>>{fillContSingle}, cv::Scalar(128));
第二个版本
CV_EXPORTS void fillPoly(Mat& img, const Point** pts,
const int* npts, int ncontours,
const Scalar& color, int lineType = LINE_8, int shift = 0,
Point offset = Point() );
//三、OpenCV无缝融合应用(二)--指定目标颜色改变(附C++源码)
https://cloud.tencent.com/developer/article/1806437
本期将介绍并演示OpenCV中使用colorChange实现图像中指定目标颜色改变的效果。
介绍
colorChange与seamlessClone同属于Seamless Cloning部分,算法均来自下面这篇论文:
OpenCV 无缝融合seamlessClone(),调试颜色colorChange(),消除高亮illuminationChange(),纹理扁平化textureFlattening()
https://www.cnblogs.com/ybqjymy/p/15699052.html
//四、OpenCV:seamlessClone泊松融合(C++/Python)
https://learnopencv.com/seamless-cloning-using-opencv-python-cpp/
https://blog.youkuaiyun.com/u012348774/article/details/81229281
前言
OpenCV3中有许多让人激动的新特性,今天我们介绍关于图像融合相关的函数 。
下图1展示了使用OpenCV图像融合的一个示例,其中的目标(飞机)是通过图像融合的方式合成到背景图像上。与图2中的直接贴图到背景上想比,不难发现图像融合的神奇之处。
seamlessClone(Mat src, Mat dst, Mat mask, Point center, Mat output, int flags)
src 目标影像,在本次给出的示例中是飞机。
dst 背景图像,在本次示例中是天空。
mask 目标影像上的mask,表示目标影像上那些区域是感兴趣区域。如果只对飞机感兴趣,那么mask上就只有飞机所在的区域。
center 目标影像的中心在背景图像上的坐标!注意是目标影像的中心!
flags 选择融合的方式,目前有 NORMAL_CLONE 、MIXED_CLONE 和 MONOCHROME_TRANSFER 三种方法。
output 输出图像