opencv3.2 dnn 图像分割
下载 http://dl.caffe.berkeleyvision.org/fcn32s-heavy-pascal.caffemodel
在opencv_contrib-3.2.0/modules/dnn/samples目录中找到下面的文件
fcn32s-heavy-pascal.prototxt
pascal-classes.txt
图片 space_shuttle.jpg

#include <opencv2/dnn.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/core/ocl.hpp>
using namespace cv;
using namespace cv::dnn; #include <fstream>
#include <iostream>
#include <cstdlib>
using namespace std; static const string fcnType = "fcn32s"; static vector<cv::Vec3b> readColors(const string &filename = "pascal-classes.txt")
{
vector<cv::Vec3b> colors; ifstream fp(filename.c_str());
if (!fp.is_open())
{
cerr << "File with colors not found: " << filename << endl;
exit(-);
} string line;
while (!fp.eof())
{
getline(fp, line);
if (line.length())
{
stringstream ss(line); string name; ss >> name;
int temp;
cv::Vec3b color;
ss >> temp; color[] = temp;
ss >> temp; color[] = temp;
ss >> temp; color[] = temp;
colors.push_back(color);
}
} fp.close();
return colors;
} static void colorizeSegmentation(dnn::Blob &score, const vector<cv::Vec3b> &colors, cv::Mat &segm)
{
const int rows = score.rows();
const int cols = score.cols();
const int chns = score.channels(); cv::Mat maxCl(rows, cols, CV_8UC1);
cv::Mat maxVal(rows, cols, CV_32FC1);
for (int ch = ; ch < chns; ch++)
{
for (int row = ; row < rows; row++)
{
const float *ptrScore = score.ptrf(, ch, row);
uchar *ptrMaxCl = maxCl.ptr<uchar>(row);
float *ptrMaxVal = maxVal.ptr<float>(row);
for (int col = ; col < cols; col++)
{
if (ptrScore[col] > ptrMaxVal[col])
{
ptrMaxVal[col] = ptrScore[col];
ptrMaxCl[col] = ch;
}
}
}
} segm.create(rows, cols, CV_8UC3);
for (int row = ; row < rows; row++)
{
const uchar *ptrMaxCl = maxCl.ptr<uchar>(row);
cv::Vec3b *ptrSegm = segm.ptr<cv::Vec3b>(row);
for (int col = ; col < cols; col++)
{
ptrSegm[col] = colors[ptrMaxCl[col]];
}
} } int main(int argc, char **argv)
{
cv::dnn::initModule(); //Required if OpenCV is built as static libs
cv::ocl::setUseOpenCL(false); //OpenCL switcher String modelTxt = fcnType + "-heavy-pascal.prototxt";
String modelBin = fcnType + "-heavy-pascal.caffemodel";
String imageFile = (argc > ) ? argv[] : "space_shuttle.jpg"; vector<cv::Vec3b> colors = readColors(); //! [Create the importer of Caffe model]
Ptr<dnn::Importer> importer;
try //Try to import Caffe GoogleNet model
{
importer = dnn::createCaffeImporter(modelTxt, modelBin);
}
catch (const cv::Exception &err) //Importer can throw errors, we will catch them
{
cerr << err.msg << endl;
}
//! [Create the importer of Caffe model] if (!importer)
{
cerr << "Can't load network by using the following files: " << endl;
cerr << "prototxt: " << modelTxt << endl;
cerr << "caffemodel: " << modelBin << endl;
cerr << fcnType << "-heavy-pascal.caffemodel can be downloaded here:" << endl;
cerr << "http://dl.caffe.berkeleyvision.org/" << fcnType << "-heavy-pascal.caffemodel" << endl;
exit(-);
} //! [Initialize network]
dnn::Net net;
importer->populateNet(net);
importer.release(); //We don't need importer anymore
//! [Initialize network] //! [Prepare blob]
Mat img = imread(imageFile);
if (img.empty())
{
cerr << "Can't read image from the file: " << imageFile << endl;
exit(-);
} resize(img, img, Size(, )); //FCN accepts 500x500 RGB-images
dnn::Blob inputBlob = dnn::Blob::fromImages(img); //Convert Mat to dnn::Blob batch of images
//! [Prepare blob] //! [Set input blob]
net.setBlob(".data", inputBlob); //set the network input
//! [Set input blob] //! [Make forward pass]
double t = (double)cv::getTickCount();
net.forward(); //compute output
t = (double)cv::getTickCount() - t;
printf("processing time: %.1fms\n", t*./getTickFrequency());
//! [Make forward pass] //! [Gather output]
dnn::Blob score = net.getBlob("score"); cv::Mat colorize;
colorizeSegmentation(score, colors, colorize);
cv::Mat show;
cv::addWeighted(img, 0.4, colorize, 0.6, 0.0, show);
cv::imshow("show", show);
cv::waitKey();
return ;
} //main
opencv3.2 dnn 图像分割的更多相关文章
- caffe+opencv3.3dnn模块 完成手写数字图片识别
最近由于项目需要用到caffe,学习了下caffe的用法,在使用过程中也是遇到了些问题,通过上网搜索和问老师的方法解决了,在此记录下过程,方便以后查看,也希望能为和我一样的新手们提供帮助. 顺带附上老 ...
- OpenCV3.0 3.1版本的改进
摘要 OpenCV现在更新到了3.1版本,相对OpenCV2有了很大改进,其中对于硬件加速,移动开发(IOS,android)的支持成为亮点. 新版的OpenCV采用了内 ...
- Jetson TX1 install Opencv3
https://jkjung-avt.github.io/opencv3-on-tx2/ 注意:在编译的时候会遇到内存空间不足的情况,可以插入U盘,将程序拷贝到U盘内编译,然后安装到Jetson上.U ...
- 基于OpenCV做“三维重建”(0)-- OpenCV3.2+VIZ6.3.0在vs2012下的编译和使用
一.问题提出 ViZ对于显示3维的效果图来说,非常有帮助:我在使用OpenCV进行双目测距的过程中,有一些参数希望能够通过可视化的方法显示出来,所以参考了这方面相关的资料.做了一些实验 ...
- [Android Studio] Using API of OpenCV DNN
前言 一.故事背景 NDK方法人脸识别 OpenCV4Android系列: 1. OpenCV4Android开发实录(1):移植OpenCV3.3.0库到Android Studio 2.OpenC ...
- [OpenCV] Install OpenCV 3.3 with DNN
OpenCV 3.3 Aug 3, 2017 OpenCV 3.3 has been released with greatly improved Deep Learning module and l ...
- [OpenCV] Install OpenCV 3.4 with DNN
目标定位 一.开始全面支持 Tensorflow OpenCV3.4 新功能 当前最新进展OpenCV 3.4 dev:https://github.com/opencv/opencv/tree/ma ...
- OpenCV-3.3.0测试
安装包目录下/samples/cpp里是各种例程 其中example_cmake里CMakeLists.txt已写好,直接cmake,make就可以,example.cpp是一个调用笔记本摄像头并显示 ...
- Ubuntu + CUDA9 + CUDNN7 + OpenCV3.4 + contrib +CAFFE-master
安装ubuntu时赞美Rufus(建议ubuntu16.04.04),过程参考 https://www.cnblogs.com/willnote/p/6725594.html 安 装 前 一 定 要 ...
随机推荐
- Linux内存初始化(四) 创建系统内存地址映射
一.前言 经过内存初始化代码分析(一)和内存初始化代码分析(二)的过渡,我们终于来到了内存初始化的核心部分:paging_init.当然本文不能全部解析完该函数(那需要的篇幅太长了),我们只关注创建系 ...
- Linux中网络通信中 使用的结构体
"+++++++++++++++++++++++++ Linux TCP/UDP通信中的结构体 +++++++++++++++++++++++++++++++++++++++" s ...
- Python fabs() 函数
描述 fabs() 方法返回数字的绝对值,如math.fabs(-10) 返回10.0. fabs() 函数类似于 abs() 函数,但是他有两点区别: abs() 是内置函数. fabs() 函数在 ...
- C# GridView 给某行或某列绑定点击事件和鼠标事件
protected void GridViewEx1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType = ...
- background-image:url(data:image/gif;base64,XXXX) base64方式将本地图片添加到文档中
background-image:url(data:image/gif;base64,R0lGODlhCwAMAMZjAElxvlNvtVRxtkp1v0p9wVh7vkqBwl58vml6vml7v ...
- [转]VTH changes in DC from Hspice
Hello, everyone. I’d like to know the threshold of the MOS transistor. And I found the “.print vth() ...
- Linux常用命令的解释
作者博客已转移,http://www.daniubiji.cn/archives/25 Linux简介及Ubuntu安装 常见指令 系统管理命令 打包压缩相关命令 关机/重启机器 Linux管道 Li ...
- Python并发编程实例教程
有关Python中的并发编程实例,主要是对Threading模块的应用,文中自定义了一个Threading类库. 一.简介 我们将一个正在运行的程序称为进程.每个进程都有它自己的系统状态,包含内存状态 ...
- Spark使用总结与分享【转】
背景 使用spark开发已有几个月.相比于python/hive,scala/spark学习门槛较高.尤其记得刚开时,举步维艰,进展十分缓慢.不过谢天谢地,这段苦涩(bi)的日子过去了.忆苦思甜,为了 ...
- python 合并字典,相同 key 的 value 如何相加?
x = { 'apple': 1, 'banana': 2 } y = { 'banana': 10, 'pear': 11 } 需要把两个字典合并,最后输出结果是: { 'apple': 1, 'b ...