菜鸟一枚,
这是我的第四个博客,
刚刚入门Opencv,
想将自己的学习过程分享给大家!!!
#include "stdafx.h"
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp> //Opencv highgui 模块
#include <opencv2/imgproc/imgproc.hpp> //Opencv 图像处理头文件
using namespace cv;
using namespace std;
Mat src; //原始图
Mat zoom_out; //缩小图
Mat sample(Mat &image); //RGB转化为样本数据
int main()
{
src = imread("D:/opencv/opencvSRC/people1.jpg");
if (src.empty())
{
printf("could not input image...");
return -1;
}
imshow("原始图", src);
pyrDown(src, zoom_out, Size(src.cols / 2, src.rows / 2));
imshow("缩小图", zoom_out);
Mat points = sample(zoom_out);
//运行KMean
int numCluster = 3;
Mat labels;
Mat centers;
TermCriteria critera = TermCriteria(TermCriteria::EPS + TermCriteria::COUNT, 10, 0.1);
kmeans(points, numCluster, labels, critera, 3, KMEANS_PP_CENTERS, centers);
int index = zoom_out.rows * 2 + 2;
int cindex = labels.at<int>(index, 0);
int height = zoom_out.rows;
int width = zoom_out.cols;
Mat dst;
zoom_out.copyTo(dst);
Mat mask = Mat::zeros(zoom_out.size(), CV_8UC1);
for (int row = 0; row < height; row++)
{
for (int col = 0; col < width; col++)
{
index = row*width + col;
int label = labels.at<int>(index, 0);
if (label == cindex) //去除背景
{
dst.at<Vec3b>(row, col)[0] = 0;
dst.at<Vec3b>(row, col)[1] = 0;
dst.at<Vec3b>(row, col)[2] = 0;
mask.at<uchar>(row, col) = 0;
}
else
{
mask.at<uchar>(row, col) = 255;
}
}
}
imshow("mask", mask);
//腐蚀、高斯模糊
Mat k = getStructuringElement(MORPH_RECT, Size(3, 3), Point(-1, -1));
dilate(mask, mask, k);
//imshow("膨胀mask", mask);
GaussianBlur(mask, mask, Size(3, 3), 0, 0);
//imshow("高斯mask", mask);
//通道混合
Vec3b color;
color[0] = theRNG().uniform(0, 255);
color[1] = theRNG().uniform(0, 255);
color[2] = theRNG().uniform(0, 255);
Mat result(zoom_out.size(), zoom_out.type());
double w = 0.0;
int b = 0, g = 0, r = 0;
int b1 = 0, g1 = 0, r1 = 0;
int b2 = 0, g2 = 0, r2 = 0;
for (int row = 0; row < height; row++)
{
for (int col = 0; col < width; col++)
{
int m = mask.at<uchar>(row, col);
if (m == 255)
{
result.at<Vec3b>(row, col) = zoom_out.at<Vec3b>(row, col); //前景
}
else if (m == 0)
{
result.at<Vec3b>(row, col) = color; //背景
}
else
{
w = m / 255.0;
b1 = zoom_out.at<Vec3b>(row, col)[0]; //前景
g1 = zoom_out.at<Vec3b>(row, col)[1];
r1 = zoom_out.at<Vec3b>(row, col)[2];
b2 = color[0]; //背景
g2 = color[1];
r2 = color[2];
b = b1*w + b2*(1.0 - w);
g = g1*w + g2*(1.0 - w);
r = r1*w + r2*(1.0 - w);
result.at<Vec3b>(row, col)[0] = b;
result.at<Vec3b>(row, col)[1] = g;
result.at<Vec3b>(row, col)[2] = r;
}
}
}
imshow("背景替换", result);
waitKey(0);
return 0;
}
Mat sample(Mat &image)
{
int width = image.cols;
int height = image.rows;
int dims = image.channels();
//初始化定义
int sampleCount = width*height;
int clusterCount = 3;
Mat point(sampleCount, dims, CV_32F, Scalar(10));
Mat labels;
Mat centers(clusterCount, 1, point.type());
//RGB数据转为样本数据
int index = 0;
for (int row = 0; row < height; row++)
{
for (int col = 0; col < width; col++)
{
index = row*width + col;
Vec3b bgr = image.at<Vec3b>(row, col);
point.at<float>(index, 0) = static_cast<int>(bgr[0]);
point.at<float>(index, 1) = static_cast<int>(bgr[1]);
point.at<float>(index, 2) = static_cast<int>(bgr[2]);
}
}
return point;
}
程序运行结果:
注:样本图片源于百度图片
谢谢同学们的阅读!!!