windows编译tensorflow tensorflow单机多卡程序的框架 tensorflow的操作 tensorflow的变量初始化和scope 人体姿态检测 segmentation标注工具 tensorflow模型恢复与inference的模型简化 利用多线程读取数据加快网络训练 tensorflow使用LSTM pytorch examples 利用tensorboard调参 深度学习中的loss函数汇总 纯C++代码实现的faster rcnn tensorflow使用记录 windows下配置caffe_ssd use ubuntu caffe as libs use windows caffe like opencv windows caffe implement caffe model convert to keras model flappyBird DQN Joint Face Detection and Alignment using Multi-task Cascaded Convolutional Neural Networks Fast-style-transfer tensorflow安装 tensorflow DQN Fully Convolutional Models for Semantic Segmentation Transposed Convolution, Fractionally Strided Convolution or Deconvolution 基于tensorflow的分布式部署 用python实现mlp bp算法 用tensorflow和tflearn搭建经典网络结构 Data Augmentation Tensorflow examples Training Faster RCNN with Online Hard Example Mining 使用Tensorflow做Prisma图像风格迁移 RNN(循环神经网络)推导 深度学习中的稀疏编码思想 利用caffe与lmdb读写图像数据 分析voc2007检测数据 用python写caffe网络配置 ssd开发 将KITTI的数据格式转换为VOC Pascal的xml格式 Faster RCNN 源码分析 在Caffe中建立Python layer 在Caffe中建立C++ layer 为什么CNN反向传播计算梯度时需要将权重旋转180度 Caffe使用教程(下) Caffe使用教程(上) CNN反向传播 Softmax回归 Caffe Ubuntu下环境配置

C++小程序

2017年02月26日

opencv3 跟踪API

// Standard include files
#include <opencv2/opencv.hpp>
#include <opencv2/tracking.hpp>
 
using namespace cv;
using namespace std;
 
int main(int argc, char **argv)
{
    // Set up tracker. 
    // Instead of MIL, you can also use 
    // BOOSTING, KCF, TLD, MEDIANFLOW or GOTURN  
    Ptr<Tracker> tracker = Tracker::create( "MIL" );
 
    // Read video
    VideoCapture video("videos/chaplin.mp4");
     
    // Check video is open
    if(!video.isOpened())
    {
        cout << "Could not read video file" << endl;
        return 1;
    }
 
    // Read first frame. 
    Mat frame;
    video.read(frame);
 
    // Define an initial bounding box
    Rect2d bbox(287, 23, 86, 320);
 
    // Uncomment the line below if you 
    // want to choose the bounding box
    // bbox = selectROI(frame, false);
     
    // Initialize tracker with first frame and bounding box
    tracker->init(frame, bbox);
 
    while(video.read(frame))
    {
        // Update tracking results
        tracker->update(frame, bbox);
 
        // Draw bounding box
        rectangle(frame, bbox, Scalar( 255, 0, 0 ), 2, 1 );
 
        // Display result
        imshow("Tracking", frame);
        int k = waitKey(1);
        if(k == 27) break;
 
    }
 
    return 0; 
     
}

opencv中Mat中元素值得读取方法

利用 at 函数读取

单通道图像读取方式

Mat img = imread(filename,IMREAD_GRAYSCALE);  
for( size_t nrow = 0; nrow < img.rows; nrow++)  
{  
    for(size_t ncol = 0; ncol < img.cols; ncol++)  
    {  
        uchar val = img.at<uchar>(nrow,ncol);      
    }  
} 

三通道图像读取方式

Mat img = imread(filename,IMREAD_COLOR);  
for( size_t nrow = 0; nrow < img.rows; nrow++)  
{  
    for(size_t ncol = 0; ncol < img.cols; ncol++)  
    {  
       Vec3i bgr = img.at<Vec3b>(nrow,ncol);//用Vec3b也行  
       cout   << "("<<bgr.val[0]<<","  
              <<bgr.val[1]<<","  
              <<bgr.val[2]<<")";  
    }  
    cout << endl;  
}

在使用 at 函数的情况下需要预先知道Mat变量中存储的元素类型,如果类型不匹配就会出现读错误。所以可以采用c++ boost库中的BOOST_TYPEOF来获取图像的元素数据类型

Mat img = imread(filename);
typedef BOOST_TYPEOF(*img.data) ElementType
for( size_t nrow = 0; nrow < img.rows; nrow++)  
{  
    for(size_t ncol = 0; ncol < img.cols; ncol++)  
    {  
        cout<<img.at<ElementType>(nrow,ncol);      
    }  
}  

使用指针读取

Mat img = imread(filename,IMREAD_COLOR);
for( size_t nrow = 0; nrow < img3.rows; nrow++)  
{  
    uchar* data = img.ptr<uchar>(nrow);  
    for(size_t ncol = 0; ncol < img.cols * img.channels(); ncol++)  
    {  
        cout << int( data[ncol] ) ;  
    }  
    cout << endl;  
}

使用迭代器

Mat img = imread(filename,IMREAD_GRAYSCALE);
MatIterator_<uchar> it = img.begin<uchar>(), it_end = img.end<uchar>();  
for(int cnt = 1; it != it_end; ++it)  
{  
    cout << ( int(*it) ) ;  
    if( (cnt++ % img.cols) ==0 )
    {
        cout << endl;    	
    }	    
}

使用矩阵元素的地址定位知识

Mat img(rows, cols,CV_8U, Scalar(0));
for( size_t nrow = 0; nrow < img.rows; nrow++)
{  
    for(size_t ncol = 0; ncol < img.cols; ncol++)
    {     
        cout<<(int)(*(img.data+img.step[0]*nrow+img.step[1]*ncol));
    }
} 

blog comments powered by Disqus