一:API函数介绍
OpenCV3.x的图像计算模块多了新算法API-无缝克隆(Seamless Cloning),主要是针对图像编辑,局部修改等应用场景实现迁移对象与原图像场景的无缝克隆。相关函数与参数说明如下:
void seamlessClone( InputArray src, InputArray dst, InputArray mask, Point p, OutputArray blend, int flags);
@param src Input 8-bit 3-channel image.
@param dst Input 8-bit 3-channel image.
@param mask Input 8-bit 1 or 3-channel image.
@param p Point in dst image where object is placed.
@param blend Output image with the same size and type as dst.
@param flags Cloning method that could be one of the following:
- **NORMAL_CLONE** The power of the method is fully expressed when inserting objects with
complex outlines into a new background
- **MIXED_CLONE** The classic method, color-based selection and alpha masking might be time consuming and often leaves an undesirable halo. Seamless cloning, even averaged with the original image, is not effective. Mixed seamless cloning based on a loose selection proves effective.
- **MONOCHROME_TRANSFER** Monochrome transfer allows the user to easily replace certain features of
one object by alternative features. */
支持的克隆方法有三种分别如下
- NORMAL_CLONE
把待克隆的src对象完整的插入到dst目标图像图像中去,不改变其轮廓特征与结构
- MIXED_CLONE
混合克隆跟正常克隆相比,它会把背景颜色与纹理考虑进去,对轮廓特征与背景实现透明通道混合。
- MONOCHROME_TRANSFER
基于特征的迁移融合,只会把特征融合到背景图像当中。
二、实验
一般我们使用无缝克隆时候最常用设置就是正常克隆,都是想无缝替换或者融合特定对象到场景中去。演示程序主要是基于图像二值化实现自动遮罩层提取生成,然后基于遮罩图像,原图像、目标图像使用无缝克隆算法生成混合之后的输出图像。
int main()
{
Mat src, dst;
src = imread("D:\\cv_study\\Exercise\\Seamless Cloning\\logo.png");
dst = imread("D:\\cv_study\\Exercise\\Seamless Cloning\\R.jpg");
Mat mask,gray;
cvtColor(src, gray, CV_BGR2GRAY);
threshold(gray, mask,150, 255, THRESH_BINARY|| THRESH_OTSU);
Mat element = getStructuringElement(MORPH_RECT, Size(10, 10),Point(-1,-1));
dilate(mask, mask, element);
Mat blend;
seamlessClone(src, dst, mask, Point(600, 739), blend, NORMAL_CLONE);
waitKey(0);
return 0;
}
由上面的结果可以看出,克隆的还是比较好的。
int main()
{
Mat src, dst;
src = imread("D:\\cv_study\\Exercise\\Seamless Cloning\\apple.jpg");
dst = imread("D:\\cv_study\\Exercise\\Seamless Cloning\\R.jpg");
Mat mask,gray;
cvtColor(src, gray, CV_BGR2GRAY);
threshold(gray, mask,150, 255, THRESH_BINARY|| THRESH_OTSU);
Mat element = getStructuringElement(MORPH_RECT, Size(8, 8),Point(-1,-1));
dilate(mask, mask, element);
Mat blend;
seamlessClone(src, dst, mask, Point(512, 512), blend, NORMAL_CLONE);
waitKey(0);
return 0;
}
可以看到,结果还是不错的,今天讲的内容比较简单,不足之处请多多见谅,小编会继续努力。