环境:
1、安装opencv3.3以上,因为3.3以上才有dnn模块。目前opencv已经更新到opencv4.0.0-alpha,下面网站对更新日志一目了然:
https://github.com/opencv/opencv/wiki/ChangeLog#version400
2、如果是window系统最好安装vs2015或者vs2017,因为vs2013已经不支持vc14,具体怎么搭建环境就不说了,网上一大把。当然在Ubuntu上也可以,并且实现起来更流畅。
模型:
模型用的是caffe的fcn,还需要一个.prototxt文件和pascal-classes.txt,prototxt文件记录分类标签以及模型的结构,pascal-classes.txt标记每个类别使用什么颜色。路径就在安装的opencv路径里,我的是:
pascal-classes.txt路径:E:/opencv/opencv3.4.1/opencv/sources/samples/data/dnn/pascal-classes.txt
内容:
background 0 0 0
aeroplane 128 0 0
bicycle 0 128 0
bird 128 128 0
boat 0 0 128
bottle 128 0 128
bus 0 128 128
car 128 128 128
cat 64 0 0
chair 192 0 0
cow 64 128 0
diningtable 192 128 0
dog 64 0 128
horse 192 0 128
motorbike 64 128 128
person 192 128 128
pottedplant 0 64 0
sheep 128 64 0
sofa 0 192 0
train 128 192 0
tvmonitor 0 64 128
prototxt路径:E:/opencv/opencv3.4.1/opencv/sources/samples/data/dnn/fcn8s-heavy-pascal.prototxt
FCN模型路径:E:/opencv/opencv3.4.1/opencv/sources/samples/data/dnn/fcn8s-heavy-pascal.caffemodel
FCN模型比较大,500多M,需要自己下载,下载路径:http://dl.caffe.berkeleyvision.org/fcn8s-heavy-pascal.caffemodel
代码:
#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 = "E:/opencv/opencv3.4.1/opencv/sources/samples/data/dnn/pascal-classes.txt";
String modelFile = "E:/opencv/opencv3.4.1/opencv/sources/samples/data/dnn/fcn8s-heavy-pascal.caffemodel";
String model_text_file = "E:/opencv/opencv3.4.1/opencv/sources/samples/data/dnn/fcn8s-heavy-pascal.prototxt";
vector<Vec3b> readColors();
int main(int argc, char** argv) {
Mat frame = imread("E:/opencv/opencv3.4.1/opencv/sources/samples/data/dnn/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 \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
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(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;
}
结果:
原始图像
分割结果:
运行时间:
总结:
图像分割运行速度比较慢,可能GPU会快一些。
图像分割算法比普通的对象检测难,最新出的Mask Rcnn不知道怎么样,有时间准备去试一试。