光流转RGB可视化的C++实现

本文介绍了如何使用C++将稠密光流场转换为Munsell颜色体系的RGB图像,作为光流算法开发的可视化手段。实现参考了Python的flownet2,并提醒在处理过程中注意数据类型的选择,避免使用unsigned char存储数值。提供了相应的源代码文件如flo2img_tmp.hpp和flo2img_tmp.cpp,并给出了在Windows环境下结合Caffe和FlowNet2/Liteflownet进行测试的建议。
摘要由CSDN通过智能技术生成

介绍

将稠密光流场按照孟塞尔颜色体系(Munsell Color System)转化为RBG图,这个其实没什么卵用,只不过是开发光流估计算法或者光流应用时提供一种结果可视化的途径。

实现过程时参照python版本的flownet2提供的代码。

需要注意的是不要用unsigned char 类型的变量存储数值变量,我用一天的时间才把这个bug修复

flo2img_tmp.hpp

#ifndef FLO2IMGTMP_HPP_
#define FLO2IMGTMP_HPP_


#ifdef USE_OPENCV
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#endif  // USE_OPENCV
//typedef unsigned short UINT;

class Flo2Img {
public:
	Flo2Img();
	
private:
	unsigned short colorwheel[55][3];
	unsigned short ncols;

};

void flo2img(cv::Mat& flo, cv::Mat &img);
#endif

flo2img_tmp.cpp

#ifdef USE_OPENCV
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#endif  // USE_OPENCV
#include <flo2img_tmp.hpp>
#include <iostream>
#include "math.h"

#define UNKNOWN_THRESH  1e5
#define EPS 1e-10
#define pi 3.1415926


void flo2img(cv::Mat& flo, cv::Mat& img)
{
	
	unsigned char ncols;

	unsigned short RY = 15;
	unsigned short YG = 6;
	unsigned short GC = 4;
	unsigned short CB = 11;
	unsigned short BM = 13;
	unsigned short MR = 6;
	ncols = RY + YG + GC + CB + BM + MR;
	float colorwheel[55][3];
	unsigned short nchans = 3;
	unsigned short col = 0;
	//RY
	for (int i = 0; i<RY; i++)
	{
		colorwheel[col + i][0] = 255;
		colorwheel[col + i][1] = 255 * i / RY;
		colorwheel[col + i][2] = 0;
		//std::cout << colorwheel[i][1] << '\n';
	}
	col += RY;
	//YG
	for (int i = 0; i<YG; i++)
	{
		colorwheel[col + i][0] = 255 - 255 * i / YG;
		colorwheel[col + i][1] = 255;
		colorwheel[col + i][2] = 0;
	}
	col += YG;
	//GC
	for (int i = 0; i < GC; i++)
	{
		colorwheel[col + i][1] = 255;
		colorwheel[col + i][2] = 255 * i / GC;
		colorwheel[col + i][0] = 0;
	}
	col += GC;
	//CB
	for (int i = 0; i < CB; i++)
	{
		colorwheel[col + i][1] = 255 - 255 * i / CB;
		colorwheel[col + i][2] = 255;
		colorwheel[col + i][0] = 0;
	}
	col += CB;
	//BM
	for (int i = 0; i < BM; i++)
	{
		colorwheel[col + i][2] = 255;
		colorwheel[col + i][0] = 255 * i / BM;
		colorwheel[col + i][1] = 0;
	}
	col += BM;
	//MR
	for (int i = 0; i < MR; i++)
	{
		colorwheel[col + i][2] = 255 - 255 * i / MR;
		colorwheel[col + i][0] = 255;
		colorwheel[col
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值