根據DFS的結果實現一個簡單的背景虛化功能

#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <fstream>
#include <vector>



using namespace std;
using namespace cv;




int main() {

	Mat src,dst;

	uchar* src_data = nullptr;
	uchar* dst_data = nullptr;

	int depth;
	vector<vector<int>> arr;
	vector<int> line;

	ifstream infile;
	infile.open("txt1.txt");

	if (!infile.good())
	{
		cout << "open file error" << endl;
		system("pause");
		return -1;
	}

	while (1)
	{
		if (infile.eof()) break;
		infile >> depth;
		line.push_back(depth);
		if (line.size()==640)
		{
			arr.push_back(line);
			line.clear();
		}


	}

	//讀入原始圖片
	src = imread("output.bmp", IMREAD_UNCHANGED);
	int nr = src.rows; // number of rows 480
	int nc = src.cols * src.channels(); // total number of elements per line
	/*for (int j = 0; j < nr; j++) {
		src_data = src.ptr<uchar>(j);
	}*/

	//顯示原始圖片
	cv::namedWindow("origin", WINDOW_AUTOSIZE); // 創建一個窗
	cv::imshow("origin", src);
	//waitKey(0);
	//cv::destroyWindow("origin");

	//高斯模糊
	GaussianBlur(src, dst, Size(15, 15), 2);
	cv::namedWindow("GS", WINDOW_AUTOSIZE); // 創建一個窗
	imwrite("C:\\firstOpenCV\\Project1\\Project1\\mid.bmp", dst);
	cv::imshow("GS", dst);

	//waitKey(0);//表示無限等待
	//cv::destroyWindow("GS");


	//恢復被模糊的前景
	dst = imread("mid.bmp", IMREAD_UNCHANGED);
	//int nr = dst.rows; // number of rows
	//cout << nr << endl;
	//int nc = dst.cols * dst.channels(); // total number of elements per line
	for (int j = 0; j < nr; j++) {
		src_data = src.ptr<uchar>(j);
		dst_data = dst.ptr<uchar>(j);
		for (int i = 0; i < nc; i++) {
			if (arr[j][i] > 128) {
				dst_data[i] = src_data[i];
			}
		}
	}

	//顯示背景虛化的圖片
	cv::namedWindow("BackBlur", WINDOW_AUTOSIZE); // 創建一個窗
	imwrite("C:\\firstOpenCV\\Project1\\Project1\\last.bmp", dst);
	cv::imshow("BackBlur", dst);



	return 0;
}

實現的結果並不是很好,圖片中清楚的部分很分散,因爲後續處理太粗糙了——直接把圖片中模糊的前景像素值恢復爲原始值(捂臉)。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章