首页 > 其他分享 >OpenCV3.3深度神经网络DNN模块 实例5:FCN模型实现图像分割

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

时间:2023-08-18 09:33:33浏览次数:57  
标签:const Mat int frame DNN FCN col OpenCV3.3 row

  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文件

标签:const,Mat,int,frame,DNN,FCN,col,OpenCV3.3,row
From: https://www.cnblogs.com/ybqjymy/p/17639516.html

相关文章

  • OpenCV3.3深度神经网络DNN模块 实例1:读取单张PNG文件(opencv3.3环境测试)
    1#include<opencv2/opencv.hpp>2#include<opencv2/dnn.hpp>//dnn模块类3#include<iostream>45usingnamespacecv;6usingnamespacestd;78intmain(intargc,char**argv){9Matsrc=imread("tx.png");10......
  • 端侧DNN部署——模型转换
    1端侧推理框架经验总结总结下最近用过的一些框架,并介绍他们的主要特点和转换过程。onnxruntimencnnmnntensorflowlitehuaweihiaipaddlelite2模型部署转换过程我们以torchvision库中的resnet50模型为例,介绍模型转换的过程。2.1pytorch转换到otherspytorch是目......
  • 异步通知example+fcntl
    1,异步通知异步通知的意思是:一旦设备就绪,则主动通知应用程序,这样应用程序根本就不需要查询设备状态,这一点非常类似于硬件上“中断”的概念,比较准确的称谓是“信号驱动的异步I/O”。信号是在软件层次上对中断机制的一种模拟,在原理上,一个进程收到一个信号与处理器收到一个中......
  • 【Ubuntu】Cuda10.2与cuDNN7.6.5的安装
    本文是Cuda10.2与cuDNN7.6.5安装记录,系统环境是Ubuntu18.04所使用的显卡是GeForceRTX2080,因为不是30系的显卡,所以Cuda安装10.2就足够了因为项目需要,要配置一下深度学习环境,一直没有整理和总结配置过程,就想记录一下,如果有错误的地方还请大家批评指正Cuda10.2的安装......
  • FCN-全卷积网络-pytorch搭建
    代码摘自:https://github.com/sovit-123/Semantic-Segmentation-using-Fully-Convlutional-Networks预备知识:下载预训练权重,抽取出网络层实例:运行如下代码,自动下载到C:\Users\**\.cache\torch\hub\checkpoints目录下。vgg=models.vgg16(pretrained=True)抽取网络层,vgg.fe......
  • 深度学习TensorFlow和CUDA、cudnn、Pytorch以及英伟达显卡对应版本对照表
    一、TensorFlow对应版本对照表版本Python版本编译器cuDNNCUDAtensorflow-2.9.03.7-3.10 8.111.2tensorflow-2.8.03.7-3.10 8.111.2tensorflow-2.7.03.7-3.9 8.111.2tensorflow-2.6.03.6-3.9GCC7.3.18.111.2tensorflow-2.5.03.6-3.9GCC......
  • m基于OFDM+QPSK和DNN深度学习信道估计的无线图像传输matlab仿真,输出误码率曲线,并用
    1.算法仿真效果matlab2022a仿真结果如下:     2.算法涉及理论知识概要       基于OFDM+QPSK和DNN深度学习信道估计的无线图像传输"是一种无线通信系统,它利用正交频分复用(OFDM)和四相位偏移键控(QPSK)技术来传输图像数据,并借助深度神经网络(DNN)来进行信道估计,从......
  • 如何拉取指定CPU架构的并且指定ubuntu版本的并且指定cuda和cudnn版本的docker镜像
    本篇讲的重点是如何拉取带有cuda和cudnn的docker镜像,因此这些的镜像源的频道为NVIDIA:官方地址:https://hub.docker.com/r/nvidia/cuda   根据官方资料我们知道NVIDIA的docker的tag分为三类:  base版本、runtime版本、devel版本:base版本只安装了cuda,runtime版本安装......
  • ubuntu22.04 cuda cudnn tensorRT安装
    1:查看当前安装驱动版本信息driverversion:525.116.03cudaversion:12.0注意:nvidia官网下载和打开巨慢的问题把nvidia.com换成nvidia.cn2:下载cudaversion12.0版本并安装https://developer.nvidia.com/cuda-toolkit-archivehttps://developer.nvidia.com/cuda-12-0-0......
  • cuda/cudnn 环境安装及查询
    引用出处:https://www.autodl.com/docs/cuda/注意:如果没有二次编译代码的需求,正常情况下不需要单独安装CUDA/cuDNN,因为框架都内置了编译好的CUDA,框架版本和CUDA版本是对应的,只需要关注框架版本即可,无需独立关注CUDA版本。查询默认CUDA/cuDNN版本¶注意:通过nvidia-smi命令查看到......