DNN系列5_FCN模型实现图像分割

本例程用到的模型文件、源码和图片素材

贾志刚OpenCV3.3深度神经网络DNN模块系列学习资料整理

5 FCN模型图像分割

5.1 FCN模型模型与数据介绍

FCN模型
支持20个分割标签


 使用模型实现图像分割

5.2 模型文件

 二进制模型
- fcn8s-heavy-pascal.caffemodel   官网下载
 网络描述
- fcn8s-heavy-pascal.prototxt
 分割信息
- pascal-classes.txt
- 20个分类

5.3 使用模型实现图像分割

 编码处理
- 加载Caffem模型
- 使用模型预测

实例5:FCN模型实现图像分割

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

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

const size_t width = 300;
const size_t height = 300;
String labelFile = "D:/opencv3.3/opencv/sources/samples/data/dnn/pascal-classes.txt";
String modelFile = "D:/opencv3.3/opencv/sources/samples/data/dnn/fcn8s-heavy-pascal.caffemodel";
String model_text_file = "D:/opencv3.3/opencv/sources/samples/data/dnn/fcn8s-heavy-pascal.prototxt";

vector<Vec3b> readColors();
int main(int argc, char** argv) {
	Mat frame = imread("rgb.jpg");
	if (frame.empty()) {
		printf("could not load image...\n");
		return -1;
	}
	namedWindow("input image", CV_WINDOW_AUTOSIZE);
	imshow("input image", frame);
	resize(frame, frame, Size(500, 500));//改变尺寸
	vector<Vec3b> colors = readColors();
//
	// init net  初始化网络
	Net net = readNetFromCaffe(model_text_file, modelFile);
	Mat blobImage = blobFromImage(frame);

	// use net   使用网络
	float time = getTickCount();
	net.setInput(blobImage, "data");
	Mat score = net.forward("score");
	float tt = getTickCount() - time;
	printf("time consume: %.2f ms \n", (tt / getTickFrequency()) * 1000);
	
	// segmentation and display   分割并显示
	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);

	// setup LUT  LUT查找
	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;
	imshow("FCN-demo1", result);
	addWeighted(frame, 0.3, result, 0.7, 0, dst);//增加宽度
	imshow("FCN-demo", dst);

	waitKey(0);
	return 0;
}

vector<Vec3b> readColors() {
	vector<Vec3b> colors;
	ifstream fp(labelFile);
	if (!fp.is_open()) {
		printf("could not open the file...\n");
		exit(-1);
	}
	string line;
	while (!fp.eof()) {
		getline(fp, line);
		if (line.length()) {
			stringstream ss(line);
			string name;
			ss >> name;
			int temp;
			Vec3b color;
			ss >> temp;
			color[0] = (uchar)temp;
			ss >> temp;
			color[1] = (uchar)temp;
			ss >> temp;
			color[2] = (uchar)temp;
			colors.push_back(color);
		}
	}
	return colors;
}

                                   

 

 pascal可实现分割的种类和显示颜色(BGR)可见 pascal-classes.txt文件

                       

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

亦我飞也

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值