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 安 装 前 一 定 要 ...
随机推荐
- CMA概述
前言 本文是近期学习CMA模块的一个学习笔记,方便日后遗忘的时候,回来查询以便迅速恢复上下文. 学习的基本方法是这样的:一开始,我自己先提出了若干的问题,然后带着这些问题查看网上的资料,代码,最后整理 ...
- add printer driver error 1802修复说明
1.重启电脑后 ,将服务"Print Spooler"服务重新启动2.srclient.dll文件拷贝到c盘 windows/system32目录下3.连接好打印机USB接口,重装 ...
- ubuntu 12.04下如何编译hadoop2.4
问题导读: 1.如果获取hadoop src maven包?2.编译hadoop需要装哪些软件?3.如何编译hadoop2.4?扩展:编译hadoop为何安装这些软件? 一.首先下载hadoop源码 ...
- Qt Quick + OpenGL + Bullet初次測试
Qt Quick + OpenGL + Bullet初次測试 眼下Qt的Quick模块已经表现得很出色,并且可以预留接口来渲染OpenGL场景.一般来说,已经可以满足大部分编程须要了.这次呢.尝试使用 ...
- 大数问题:求n的阶乘
题目:求100! 这看起来是一个非常简答的问题,递归解之毫无压力 int func(int n){ if(n <= 1) return 1; else return n*func(n-1); } ...
- 基于PCIe的高速接口设计
基于PCIe的高速接口设计 由 judyzhong 于 星期四, 03/03/2016 - 13:49 发表 作者:李晓宁,姚远程,秦明伟 2016年微型机与应用第1期 摘要:PCIe总线是第三代I/ ...
- cocos2d-x笔记-CCGLProgram
引擎提供了CCGLProgram类来处理着色器相关操作,对当前绘图程序进行了封装,其中使用频率最高的应该是获取着色器程序的接口: const GLuint getProgram(); 该接口返回了当前 ...
- 在Ubuntu环境中qemu-kvm网桥的配置
在文件/etc/network/interfaces中添加以下内容 auto lo iface lo inet loopback #auto eth0 #iface eth0 inet manual ...
- 每天一个linux命令(1):tail 命令
tail 命令从指定点开始将文件写到标准输出.使用tail命令的-f选项可以方便的查阅正在改变的日志文件,tail -f filename会把filename里最尾部的内容显示在屏幕上,并且不但刷新, ...
- 交叉编译busybox
交叉编译器: 3.3.2# wget -c http://www.busybox.net/downloads/busybox-1.7.0.tar.bz2# tar jxvf busybox-1.7.0 ...