OpenCV3.3深度神经网络DNN模块 实例5:FCN模型实现图像分割

发布时间 2023-08-18 09:30:53作者: 一杯清酒邀明月
  1 #include <opencv2/opencv.hpp>
  2 #include <opencv2/dnn.hpp>
  3 #include <iostream>
  4  
  5 using namespace cv;
  6 using namespace cv::dnn;
  7 using namespace std;
  8  
  9 const size_t width = 300;
 10 const size_t height = 300;
 11 String labelFile = "D:/opencv3.3/opencv/sources/samples/data/dnn/pascal-classes.txt";
 12 String modelFile = "D:/opencv3.3/opencv/sources/samples/data/dnn/fcn8s-heavy-pascal.caffemodel";
 13 String model_text_file = "D:/opencv3.3/opencv/sources/samples/data/dnn/fcn8s-heavy-pascal.prototxt";
 14  
 15 vector<Vec3b> readColors();
 16 int main(int argc, char** argv) {
 17     Mat frame = imread("rgb.jpg");
 18     if (frame.empty()) {
 19         printf("could not load image...\n");
 20         return -1;
 21     }
 22     namedWindow("input image", CV_WINDOW_AUTOSIZE);
 23     imshow("input image", frame);
 24     resize(frame, frame, Size(500, 500));//改变尺寸
 25     vector<Vec3b> colors = readColors();
 26 //
 27     // init net  初始化网络
 28     Net net = readNetFromCaffe(model_text_file, modelFile);
 29     Mat blobImage = blobFromImage(frame);
 30  
 31     // use net   使用网络
 32     float time = getTickCount();
 33     net.setInput(blobImage, "data");
 34     Mat score = net.forward("score");
 35     float tt = getTickCount() - time;
 36     printf("time consume: %.2f ms \n", (tt / getTickFrequency()) * 1000);
 37     
 38     // segmentation and display   分割并显示
 39     const int rows = score.size[2];
 40     const int cols = score.size[3];
 41     const int chns = score.size[1];
 42     Mat maxCl(rows, cols, CV_8UC1);
 43     Mat maxVal(rows, cols, CV_32FC1);
 44  
 45     // setup LUT  LUT查找
 46     for (int c = 0; c < chns; c++) {
 47         for (int row = 0; row < rows; row++) {
 48             const float *ptrScore = score.ptr<float>(0, c, row);
 49             uchar *ptrMaxCl = maxCl.ptr<uchar>(row);
 50             float *ptrMaxVal = maxVal.ptr<float>(row);
 51             for (int col = 0; col < cols; col++) {
 52                 if(ptrScore[col] > ptrMaxVal[col]) {
 53                     ptrMaxVal[col] = ptrScore[col];
 54                     ptrMaxCl[col] = (uchar)c;
 55                 }
 56             }
 57         }
 58     }
 59  
 60     // look up colors 找到对应颜色
 61     Mat result = Mat::zeros(rows, cols, CV_8UC3);
 62     for (int row = 0; row < rows; row++) {
 63         const uchar *ptrMaxCl = maxCl.ptr<uchar>(row);
 64         Vec3b *ptrColor = result.ptr<Vec3b>(row);
 65         for (int col = 0; col < cols; col++) {
 66             ptrColor[col] = colors[ptrMaxCl[col]];
 67         }
 68     }
 69     Mat dst;
 70     imshow("FCN-demo1", result);
 71     addWeighted(frame, 0.3, result, 0.7, 0, dst);//增加宽度
 72     imshow("FCN-demo", dst);
 73  
 74     waitKey(0);
 75     return 0;
 76 }
 77  
 78 vector<Vec3b> readColors() {
 79     vector<Vec3b> colors;
 80     ifstream fp(labelFile);
 81     if (!fp.is_open()) {
 82         printf("could not open the file...\n");
 83         exit(-1);
 84     }
 85     string line;
 86     while (!fp.eof()) {
 87         getline(fp, line);
 88         if (line.length()) {
 89             stringstream ss(line);
 90             string name;
 91             ss >> name;
 92             int temp;
 93             Vec3b color;
 94             ss >> temp;
 95             color[0] = (uchar)temp;
 96             ss >> temp;
 97             color[1] = (uchar)temp;
 98             ss >> temp;
 99             color[2] = (uchar)temp;
100             colors.push_back(color);
101         }
102     }
103     return colors;
104 }

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