OpenCv-C++-深度神经网络(DNN)模块-使用FCN模型实现图像分割

版权声明:本文为博主原创文章,转载请附上博文链接! https://blog.csdn.net/Daker_Huang/article/details/87641756

FCN是什么?中文名称是“全卷积网络”,它将传统CNN中的全连接层转化成一个个的卷积层。在传统的CNN结构中,前5层是卷积层,第6层和第7层分别是一个长度为4096的一维向量,第8层是长度为1000的一维向量,分别对应1000个类别的概率。如下图所示:
在这里插入图片描述
参考文章:https://blog.csdn.net/williamyi96/article/details/77415190
https://blog.csdn.net/taigw/article/details/51401448
实例:

#include<opencv2/opencv.hpp>
#include<opencv2/dnn.hpp>
#include<iostream>

using namespace cv;
using namespace std;
using namespace cv::dnn;

const size_t width = 500;
const size_t height = 500;//定义图像文件宽高

vector<Vec3b> labels_color();

string label_file = "D:/test/dnn/fcn/labelmap.txt";
string deploy_file = "D:/test/dnn/fcn/fcn8s-heavy-pascal.prototxt";
string model_file = "D:/test/dnn/fcn/fcn8s-heavy-pascal.caffemodel";



int main(int argc, char **argv)
{
	Mat src = imread("D:/test/person_bike.jpg");

	if (!src.data)
	{
		cout << "图像文件未找到!!!" << endl;
		return -1;
	}
	resize(src, src, Size(500, 500), 0, 0);
	vector<Vec3b>colors = labels_color();
	Net net;
	net = readNetFromCaffe(deploy_file, model_file);//读取二进制文件和描述文件
	float t1 = getTickCount();
	Mat inputblob = blobFromImage(src);
	net.setInput(inputblob, "data");
	Mat score=net.forward("score");
	float t2 = getTickCount();
	float t = (t2 - t1) / getTickFrequency();
	cout << "运行时间:" <<t<< endl;

	const int rows = score.size[2];   //图像的高
	const int cols = score.size[3];   //图像的宽
	const int chns = score.size[1];   //图像的通道数
	Mat maxCl(rows, cols, CV_8UC1);
	Mat maxVal(rows, cols, CV_32FC1);

	for (int c = 0; c < chns; c++) {
		for (int row = 0; row < rows; row++) {
			const float *ptrScore = score.ptr<float>(0, c, row);
			uchar *ptrMaxCl = maxCl.ptr<uchar>(row);
			float *ptrMaxVal = maxVal.ptr<float>(row);
			for (int col = 0; col < cols; col++) {
				if (ptrScore[col] > ptrMaxVal[col]) {
					ptrMaxVal[col] = ptrScore[col];
					ptrMaxCl[col] = (uchar)c;
				}
			}
		}
	}

	// look up colors
	Mat result = Mat::zeros(rows, cols, CV_8UC3);
	for (int row = 0; row < rows; row++) {
		const uchar *ptrMaxCl = maxCl.ptr<uchar>(row);
		Vec3b *ptrColor = result.ptr<Vec3b>(row);
		for (int col = 0; col < cols; col++) {
			ptrColor[col] = colors[ptrMaxCl[col]];
		}
	}
	Mat dst;
	addWeighted(src, 0.3, result, 0.7, 0, dst);  //图像合并
	imshow("FCN-demo", dst);

	waitKey(0);
	destroyAllWindows();
	return 0;

}

vector<Vec3b> labels_color()
{
	vector<Vec3b>colors;


	ifstream fp(label_file);//打开输入流,读入文件
	if (!fp.is_open())
	{
		printf("文件读入失败!!!\n");
		exit(-1);//直接退出

	}
	string names;
	int temp;
	Vec3b color;
	string line;//标签文件中都有对应的名字
	while (!fp.eof())//当文件没有读到结尾
	{
		getline(fp, line);//读取每一行
		stringstream ss(line); //分割字符串
		ss >> names; //读的第一个字符串是names
		ss >> temp;
		color[0] = temp;  //读一个字符串就给color
		ss >> temp;
		color[1] = temp;
		ss >> temp;
		color[2] = temp;
		colors.push_back(color);

	}
	return colors;
}




运行结果:
在这里插入图片描述
运行时间:
在这里插入图片描述
因为是用cpu跑的,所以时间非常慢,82秒。

猜你喜欢

转载自blog.csdn.net/Daker_Huang/article/details/87641756