本例程用到的模型文件、源码和图片素材
贾志刚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文件