Commit 369b235e authored by BRUNEAU Julien's avatar BRUNEAU Julien
Browse files

+ Add quality setting for saved images (8bits, 16bits, 32bits). Only 8bits is...

+ Add quality setting for saved images (8bits, 16bits, 32bits). Only 8bits is png, the reste is openEXR format.

+ Add the possibility to save the motion vectors of the optical flow instead of the heatmap
parent 0e3ecfdc
......@@ -7,6 +7,7 @@ public abstract class Filter
{
// configuration
public string name;
public int quality;
public bool supportsAntialiasing;
public bool needsRescale;
......@@ -66,7 +67,7 @@ public class FilterSegmentation : Filter
public override void initParams()
{
quality = ConfigReader.imgSegmentation.quality;
}
public override void OnSceneChange(Renderer r, ref MaterialPropertyBlock mpb)
......@@ -93,7 +94,7 @@ public class FilterCategory : Filter
public override void initParams()
{
quality = ConfigReader.imgCategories.quality;
}
public override void OnSceneChange(Renderer r, ref MaterialPropertyBlock mpb)
......@@ -126,6 +127,8 @@ public class FilterDepth : Filter
public override void initParams()
{
quality = ConfigReader.imgDepth.quality;
maxDist = ConfigReader.imgDepth.maxDepth;
minDist = ConfigReader.imgDepth.minDepth;
exponent = ConfigReader.imgDepth.exponent;
......@@ -158,12 +161,15 @@ public class FilterNormal : Filter
public override void initParams()
{
quality = ConfigReader.imgNormals.quality;
}
}
public class FilterOpticalFlow : Filter
{
public bool motionVector;
int ncols = 0;
Color[] colorwheel;
......@@ -184,6 +190,16 @@ public class FilterOpticalFlow : Filter
createCam(mainCam);
}
public override void initParams()
{
quality = ConfigReader.imgOpticalFlow.quality;
motionVector = ConfigReader.imgOpticalFlow.motionVector;
if (motionVector && !(quality == 16 || quality == 32))
quality = 16;
}
public override void onCameraChange(Camera main, int display)
{
camera.RemoveAllCommandBuffers();
......@@ -204,6 +220,7 @@ public class FilterOpticalFlow : Filter
mat.SetFloat("_Sensitivity", sensitivity);
mat.SetColorArray("_ColorWheel", colorwheel);
mat.SetInt("_ncols", ncols);
mat.SetInt("_MotionVector", motionVector ? 1 : 0);
var cb = new CommandBuffer();
cb.Blit(null, BuiltinRenderTextureType.CurrentActive, mat);
......@@ -243,8 +260,4 @@ public class FilterOpticalFlow : Filter
for (i = 0; i < BM; i++) setcols(255 * i / BM, 0, 255, k++);
for (i = 0; i < MR; i++) setcols(255, 0, 255 - 255 * i / MR, k++);
}
public override void initParams()
{
}
}
......@@ -42,47 +42,9 @@
sampler2D _CameraMotionVectorsTexture;
uniform float4 _ColorWheel[60];
uniform uint _ncols;
//float3 Hue(float H)
//{
// float R = abs(H * 6 - 3) - 1;
// float G = 2 - abs(H * 6 - 2);
// float B = 2 - abs(H * 6 - 4);
// return saturate(float3(R,G,B));
//}
//float3 HSVtoRGB(float3 HSV)
//{
// return float3(((Hue(HSV.x) - 1) * HSV.y + 1) * HSV.z);
//}
float _Sensitivity;
//float3 MotionVectorsToOpticalFlow(float2 motion)
//{
// // Currently is based on HSV encoding from:
// // "Optical Flow in a Smart Sensor Based on Hybrid Analog-Digital Architecture" by P. Guzman et al
// // http://www.mdpi.com/1424-8220/10/4/2975
// // Analogous to http://docs.opencv.org/trunk/d7/d8b/tutorial_py_lucas_kanade.html
// // but might need to swap or rotate axis!
uniform uint _MotionVector;
// // @TODO: support other HSV encodings (using lookup texture)
// // https://www.microsoft.com/en-us/research/wp-content/uploads/2007/10/ofdatabase_iccv_07.pdf
// // https://people.csail.mit.edu/celiu/SIFTflow/
// // some MATLAB code: https://github.com/suhangpro/epicflow/blob/master/utils/flow-code-matlab/computeColor.m
// float angle = atan2(-motion.y, -motion.x);
// float hue = angle / (UNITY_PI * 2.0) + 0.5; // convert motion angle to Hue
// float value = length(motion) * _Sensitivity; // convert motion strength to Value
// return HSVtoRGB(float3(hue, 1, value)); // HSV -> RGB
//}
//fixed4 frag(v2f i) : SV_Target
//{
// float2 motion = tex2D(_CameraMotionVectorsTexture, i.uv).rg;
// float3 rgb = MotionVectorsToOpticalFlow(motion);
// return float4(rgb, 1);
//}
float4 frag(v2f i) : SV_Target
{
......@@ -91,6 +53,9 @@
motion.x = motion.x * _ScreenParams.x;
motion.y = motion.y * _ScreenParams.y;
if (_MotionVector>0)
return float4(motion.x, motion.y, 0, 1);
float rad = sqrt(motion.x*motion.x + motion.y*motion.y);
float a = atan2(-motion.y, -motion.x) / UNITY_PI;
......
......@@ -240,12 +240,12 @@ public class FiltersControl : MonoBehaviour
if (!System.IO.Directory.Exists(subPath))
System.IO.Directory.CreateDirectory(subPath);
Save(f.camera, Path.Combine(subPath, filenameWithoutExtension), width, height, f.supportsAntialiasing, f.needsRescale);
Save(f.camera, Path.Combine(subPath, filenameWithoutExtension), width, height, f.supportsAntialiasing, f.needsRescale, f.quality);
}
}
}
private void Save(Camera cam, string filename, int width, int height, bool supportsAntialiasing, bool needsRescale)
private void Save(Camera cam, string filename, int width, int height, bool supportsAntialiasing, bool needsRescale, int quality=8)
{
var mainCamera = GetComponent<Camera>();
var depth = 24;
......@@ -259,7 +259,6 @@ public class FiltersControl : MonoBehaviour
RenderTexture.GetTemporary(mainCamera.pixelWidth, mainCamera.pixelHeight, depth, format, readWrite, antiAliasing);
var tex = new Texture2D(width, height, TextureFormat.RGBAFloat, true);
var prevActiveRT = RenderTexture.active;
var prevCameraRT = cam.targetTexture;
......@@ -281,11 +280,26 @@ public class FiltersControl : MonoBehaviour
tex.ReadPixels(new Rect(0, 0, tex.width, tex.height), 0, 0);
tex.Apply();
Debug.Log(tex.GetPixels(500, 500, 1, 1)[0]);
// encode texture into PNG
var bytes = tex.EncodeToPNG();
filename = filename + ".png";
//var bytes = tex.EncodeToEXR(Texture2D.EXRFlags.OutputAsFloat);
//filename = filename + ".exr";
byte[] bytes;
if (quality==32)
{
bytes = tex.EncodeToEXR(Texture2D.EXRFlags.OutputAsFloat);
filename = filename + ".exr";
}
else if (quality==16)
{
bytes = tex.EncodeToEXR(Texture2D.EXRFlags.None);
filename = filename + ".exr";
}
else
{
bytes = tex.EncodeToPNG();
filename = filename + ".png";
}
File.WriteAllBytes(filename, bytes);
// restore state and cleanup
......
......@@ -461,16 +461,22 @@ public class DataImgOriginal
{
[XmlAttribute]
public bool record = true;
[XmlAttribute]
public int quality = 8;
}
public class DataImgSegmentation
{
[XmlAttribute]
public bool record = false;
[XmlAttribute]
public int quality = 8;
}
public class DataImgCategories
{
[XmlAttribute]
public bool record = false;
[XmlAttribute]
public int quality = 8;
}
public class DataImgDepth
......@@ -478,6 +484,8 @@ public class DataImgDepth
[XmlAttribute]
public bool record = false;
[XmlAttribute]
public int quality = 8;
[XmlAttribute]
public float minDepth=0;
[XmlAttribute]
public float maxDepth=50;
......@@ -489,12 +497,19 @@ public class DataImgNormals
{
[XmlAttribute]
public bool record = false;
[XmlAttribute]
public int quality = 8;
}
public class DataImgOpticalFlow
{
[XmlAttribute]
public bool record = false;
[XmlAttribute]
public int quality = 8;
[XmlAttribute]
public bool motionVector = false;
}
public class DataBodyBoundingBoxes
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment