博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
rectify vuforia video background
阅读量:6963 次
发布时间:2019-06-27

本文共 3252 字,大约阅读时间需要 10 分钟。

Vuforia open web cam by itself while we have two ways( ) to access the source video background and get it rectified!
 
  1. result
     a stereo rectify result! (the test calibration reslut is not good)     
 
 
 
 
 
  1. use the Image Class
          we could get the background image like:
          Image  image = CameraDevice.Instance.GetCameraImage(Image.PIXEL_FORMAT.RGBA8888);
          then we could copy it to a texture and get the data
          webcam_texture = new Texture2D (weight, height, TextureFormat.RGB24, false);
          webcam_data = new Color32[webcam_texture.width * webcam_texture.height];
          image.CopyToTexture(webcam_texture);
          webcam_data = webcam_texture.GetPixels32 ();
          this data is passed down to opencv dll and rectify with cv::remap() function
 
          however,  this way is far to slow!!! copying the image to texture takes lots of time and I ahieve only 15fps in my environment.
 
  1. use and OpenGL texture
          Vuforia provide an example with shader program to modify the image in the background plane. we could encod the calibration map to a texture and work as coordinate lookup table, which works in GPU and would achieve high frame rate( in my environemnt, it's up to 60fps, up to the frame rate of my camera ).
          calibration maps is create like:
          C++: void
initUndistortRectifyMap
(InputArray
cameraMatrix, InputArray
distCoeffs, InputArray
R, InputArray
newCameraMatrix, Size
size, int
m1type, OutputArray
map1, OutputArray
map2
)
 
          we usually use a
CV_16SC2, mapx is a 2 channel 16-bit short, x and y coordinate. mapy is a 16-bit short, the fraction of interpolation. we change this maps to a 32 floating point each way cause GPU would interpolate itself with a floating point coordinate.
          
C++: void
convertMaps
(InputArray
map1, InputArray
map2, OutputArray
dstmap1, OutputArray
dstmap2, int
dstmap1type, bool
nninterpolation=false
)
 
          we create a two channel RG float texture as the lookup table
          texture = new Texture2D (width, height, TextureFormat.RGFloat, false);
          Material mat = GetComponent<Renderer>().material;
          mat.SetTexture ("_Texture2", texture);
 
          opencv dll open the calibration files, convert them to 32float, and passed them back to c sharp script as float array.
          map_x1 = new float[width * height];
          map_y1 = new float[width * height];
          IntPtr ptr =  get_map_x1 (opencv_wrapper_ptr);
          Marshal.Copy (ptr, map_x1, 0, width * height);
          ptr = get_map_y1 (opencv_wrapper_ptr);
          Marshal.Copy (ptr, map_y1, 0, width * height);
 
          then we feed the texture with normalized coordinate
          for (int i=0; i<width; ++i) {
            for(int j=0; j<height; ++j){
                Color color;
                color.r = map_x1[j*width+i]/(float)width;
                color.g = map_y1[j*width+i]/(float)height;
                color.b = 0.0f;
                color.a = 1.0f;
                texture.SetPixel(i,j,color);
                 }
             }
          
          In Shader file
          _Texture2 ("Texture 2 (RGFLOAT)", 2D) =  "white"{}
          uniform sampler2D _Texture2;
 
          in the fragment 
          //coordinate encoded in texture pixel
          float2 coord = tex2D (_Texture2, i.uv);
          half4 c = tex2D(_MainTex, coord);
     
          done!!!!
 
  1. Future work
          I am newbee to unity and shader language. I am not sure whether fetching a positin far away from current vertex would be slow(as I remember, this is very slow in mobile GPU with GLES). if so, the coordinate access should be computed in the vertex part not the fragment. but what I have come gots good framerate, so enjoy!!
          
 
 
 

转载于:https://www.cnblogs.com/flankechen/p/5238159.html

你可能感兴趣的文章
OpenNMS Log Correlator
查看>>
结构体 变迁
查看>>
KMP算法具体解释(转)
查看>>
doxygen可以生成C/C++代码的文档(根据注释)
查看>>
百度地图3.0实现图文并茂的覆盖物
查看>>
四、Android学习第四天——JAVA基础回顾(转)
查看>>
样条之埃尔米特(Hermite)插值函数
查看>>
javaweb学习总结(四十)——编写自己的JDBC框架
查看>>
Oracle中文乱码解决办法总结
查看>>
深入浅出Docker(一):Docker核心技术预览
查看>>
安卓开发_求好评功能
查看>>
Java中Scanner的使用方法
查看>>
Cocos2d-x 创建自己定义项目模板
查看>>
[Spring MVC] - Spring MVC环境搭建
查看>>
解决chrome extension无法下载的问题
查看>>
超酷的Android 侧滑(双向滑动菜单)效果
查看>>
Velocity魔法堂系列二:VTL语法详解
查看>>
正则表达式
查看>>
现行技术体系的问题总结
查看>>
fibonacci 数列及其应用
查看>>