当前位置: 首页 > news >正文

android流光字体实现,【流光算法代码实现】Demo

dbe3d0fae3a8

图片发自简书App

简介

本篇讲解使用opencv提供的流光法算法接口,实现物体跟踪。范例代码为参考修改tvl1_optical_flow.cpp实现。

具体实现

实现代码

#include

#include

#include "opencv2/video/tracking.hpp"

#include "opencv2/highgui/highgui.hpp"

using namespace cv;

using namespace std;

inline bool isFlowCorrect(Point2f u)

{

return !cvIsNaN(u.x) && !cvIsNaN(u.y) && fabs(u.y) < 1e9;

}

static Vec3b computeColor(float fx, float fy)

{

static bool first = true;

// relative lengths of color transitions:

// these are chosen based on perceptual similarity

// (e.g. one can distinguish more shades between red and yellow

//  than between yellow and green)

const int RY = 15;

const int YG = 6;

const int GC = 4;

const int CB = 11;

const int BM = 13;

const int MR = 6;

const int NCOLS = RY + YG + GC + CB + BM + MR;

static Vec3i colorWheel[NCOLS];

if (first){

int k = 0;

for (int i = 0; i < RY; ++i, ++k)

colorWheel[k] = Vec3i(255, 255 * i / RY, 0);

for (int i = 0; i < YG; ++i, ++k)

colorWheel[k] = Vec3i(255 - 255 * i / YG, 255, 0);

for (int i = 0; i < GC; ++i, ++k)

colorWheel[k] = Vec3i(0, 255, 255 * i / GC);

for (int i = 0; i < CB; ++i, ++k)

colorWheel[k] = Vec3i(0, 255 - 255 * i / CB, 255);

for (int i = 0; i < BM; ++i, ++k)

colorWheel[k] = Vec3i(255 * i / BM, 0, 255);

for (int i = 0; i < MR; ++i, ++k)

colorWheel[k] = Vec3i(255, 0, 255 - 255 * i / MR);

first = false;

}

const float rad = sqrt(fx * fx + fy * fy);

const float a = atan2(-fy, -fx) / (float)CV_PI;

const float fk = (a + 1.0f) / 2.0f * (NCOLS - 1);

const int k0 = static_cast(fk);

const int k1 = (k0 + 1) % NCOLS;

const float f = fk - k0;

Vec3b pix;

for (int b = 0; b < 3; b++)

{

const float col0 = colorWheel[k0][b] / 255.f;

const float col1 = colorWheel[k1][b] / 255.f;

float col = (1 - f) * col0 + f * col1;

if (rad <= 1)

col = 1 - rad * (1 - col); // increase saturation with radius

else

col *= .75; // out of range

pix[2 - b] = static_cast(255.f * col);

}

return pix;

}

static void drawOpticalFlow(const Mat_& flow, Mat& dst, float maxmotion = -1)

{

dst.create(flow.size(), CV_8UC3);

dst.setTo(Scalar::all(0));

// determine motion range:

float maxrad = maxmotion;

if (maxmotion <= 0)

{

maxrad = 1;

for (int y = 0; y < flow.rows; ++y)

{

for (int x = 0; x < flow.cols; ++x)

{

Point2f u = flow(y, x);

if (!isFlowCorrect(u))

continue;

maxrad = max(maxrad, sqrt(u.x * u.x + u.y * u.y));

}

}

}

for (int y = 0; y < flow.rows; ++y)

{

for (int x = 0; x < flow.cols; ++x)

{

Point2f u = flow(y, x);

if (isFlowCorrect(u))

dst.at(y, x) = computeColor(u.x / maxrad, u.y / maxrad);

}

}

}

int main(int argc, const char* argv[])

{

Mat frame0;

Mat frame1;

Mat_ flow;

Ptr tvl1 = createOptFlow_DualTVL1();

Mat out;

if (argc < 2){

cerr << "Usage : " << argv[0] << "" << endl;

return -1;

}

VideoCapture cap;

cap.open(argv[1]);

while(1){

cap >> frame0;

if(frame0.empty()){

cerr<< "video is over!!" << endl;

break;

}

cvtColor(frame0, frame0, CV_BGR2GRAY);

if(!frame1.empty()){

const double start = (double)getTickCount();

tvl1->calc(frame0, frame1, flow);

const double timeSec = (getTickCount() - start) / getTickFrequency();

cout << "calcOpticalFlowDual_TVL1 : " << timeSec << " sec" << endl;

drawOpticalFlow(flow, out);

imshow("out", out);

imshow("src", frame0);

waitKey(10);

}

frame0.copyTo(frame1);

}

waitKey();

return 0;

}

代码讲解

1、创建了一个DenseOpticalFlow实例,同时获得打开了需要跟踪处理的video视频到cap中。

Ptr tvl1 = createOptFlow_DualTVL1();

Mat out;

if (argc < 2){

cerr << "Usage : " << argv[0] << "" << endl;

return -1;

}

VideoCapture cap;

cap.open(argv[1]);

2、在循环中,不断的读取video的帧数据到frame0中,接着cvtColor将frame0中的数据,灰阶化。判断到存储前一帧数据为空,也就是表示

刚刚读取到第一帧数据时候,不进入处理函数中,直接跳过。最后将frame0中的帧数据,保存到frame1中。frame0进入下一次循环,获得新一帧

数据。

while(1){

cap >> frame0;

if(frame0.empty()){

cerr<< "video is over!!" << endl;

break;

}

cvtColor(frame0, frame0, CV_BGR2GRAY);

if(!frame1.empty()){

...........

...........

}

frame0.copyTo(frame1);

}

3、当检测到frame1保存了前一帧数据之后,进入到流光法计算中。首先获得当前时钟getTickCount。使用tvl1->calc分别传入当前

帧(frame0)和前一帧(frame1),将获得的位置偏移保存到flow中。接着计算出calc函数处理花费的时间,之后使用函数

drawOpticalFlow,利用flow中的位置偏移,根据偏移位置的方向和速度,从而在out图像,对应位置赋予不同的颜色和饱和度。最后将

当前帧图像和处理之后的out图像分别显示出来。

const double start = (double)getTickCount();

tvl1->calc(frame0, frame1, flow);

const double timeSec = (getTickCount() - start) / getTickFrequency();

cout << "calcOpticalFlowDual_TVL1 : " << timeSec << " sec" << endl;

drawOpticalFlow(flow, out);

imshow("out", out);

imshow("src", frame0);

waitKey(10);

相关文章:

  • c语言求今年第m月的天数,在C语言中 使用switch语句编写“显示某年某月的天数”程序:...
  • signature=735f4378ec01919f23285d0d2557be19,OPENSSL编程 第二十章 椭圆曲线
  • bubbles html5游戏源码,html5 canvas弹性气泡爆破 | 撒花动画
  • html 页面定位失效,html5自动定位,总是定位失败,是什么原因,求指点,多谢...
  • html照片墙效果,超酷CSS3相册照片墙动画特效
  • html select 加入css样式,css为select添加样式(无脚本)实现
  • 计算机基础知识(单选题),计算机基础知识单选题
  • 桌面上计算机删除后怎么复原,电脑桌面上出现一个图标,删掉后重启桌面又恢复了?怎么才能彻底删除?...
  • 计算机日常维护小知识,计算机日常维护小常识
  • html5按钮组水平均分,ichart.js绘制虚线、平均分虚线效果的实现代码_javascript技巧...
  • 学计算机可以考统计师吗,统计师如何备考呢?
  • html 发光字,介绍几种常见发光字的制作步骤方法
  • html中元相关元素包括,tabs_panels.html
  • 计算机专业论文周进展300字,毕业设计周进展记录模板
  • 计算机网络第3班第六章,计算机网络教程第3版彭澎第6章J课件教学.ppt
  • 《网管员必读——网络组建》(第2版)电子课件下载
  • 【vuex入门系列02】mutation接收单个参数和多个参数
  • CSS 专业技巧
  • Docker 1.12实践:Docker Service、Stack与分布式应用捆绑包
  • Java 最常见的 200+ 面试题:面试必备
  • JavaScript 基础知识 - 入门篇(一)
  • Joomla 2.x, 3.x useful code cheatsheet
  • JSONP原理
  • Just for fun——迅速写完快速排序
  • laravel5.5 视图共享数据
  • PHP 小技巧
  • 订阅Forge Viewer所有的事件
  • 诡异!React stopPropagation失灵
  • 极限编程 (Extreme Programming) - 发布计划 (Release Planning)
  • 老板让我十分钟上手nx-admin
  • 区块链共识机制优缺点对比都是什么
  • 推荐一个React的管理后台框架
  • 在Docker Swarm上部署Apache Storm:第1部分
  • 【云吞铺子】性能抖动剖析(二)
  • 阿里云API、SDK和CLI应用实践方案
  • 分布式关系型数据库服务 DRDS 支持显示的 Prepare 及逻辑库锁功能等多项能力 ...
  • 智能情侣枕Pillow Talk,倾听彼此的心跳
  • !$boo在php中什么意思,php前戏
  • (06)金属布线——为半导体注入生命的连接
  • (3)nginx 配置(nginx.conf)
  • (31)对象的克隆
  • (Oracle)SQL优化技巧(一):分页查询
  • (TipsTricks)用客户端模板精简JavaScript代码
  • (八)Flask之app.route装饰器函数的参数
  • (笔试题)合法字符串
  • (多级缓存)缓存同步
  • (附源码)spring boot校园健康监测管理系统 毕业设计 151047
  • (附源码)计算机毕业设计SSM保险客户管理系统
  • (机器学习的矩阵)(向量、矩阵与多元线性回归)
  • (免费领源码)Python#MySQL图书馆管理系统071718-计算机毕业设计项目选题推荐
  • (转)菜鸟学数据库(三)——存储过程
  • (转)母版页和相对路径
  • (转载)PyTorch代码规范最佳实践和样式指南
  • .NET 2.0中新增的一些TryGet,TryParse等方法
  • .NET Core 控制台程序读 appsettings.json 、注依赖、配日志、设 IOptions