- Home /
Efficient Way to Get Pure RGB Data toCSV
Hey everyone, I'm currently trying to pull RGB data from my camera on update using the following code:
// assumes you have your RenderTexture renderTexture
Texture2D tex2d = new Texture2D(cam.width, cam.height, TextureFormat.RGB24, false);
RenderTexture.active = cam;
tex2d.ReadPixels(new Rect(0, 0, cam.width, cam.height), 0, 0);
tex2d.Apply();
//super janky solution to gathering the data, write it to a text file!
using (StreamWriter writer = new StreamWriter(@"C:\Users\Oliver\Camera\Assets\data.csv", true))
{
//maybe exit this to select r, g, and b from each pixel and add the values to a list, then to string, then to array and write to line
//join each distance in array seperated by commas into a line and append it to a file.
writer.WriteLine(String.Join(",", new List<Color>(tex2d.GetPixels()).ConvertAll(i => i.ToString()).ToArray())); //OR writer.Write(i + ",");
}
The end result is writing to the CSV in the following format ColA, ColB, ColC, ColD "RGBA(0 "| " 0 "| " 0 " | " 1) " ... <----- only one pixel value taking up 4 cells
However, I want the end product to be an array of ONLY RGB values in each cell, so... ColA, ColB, ColC, ColD "[0,0,0]"| " [0,185,12] "| " [122,89,0] " | " [0,0,0] " ... <---- 4pixel values taking up 4 cells
Is there an efficient way to do this (i.e. clean the data before writing to the CSV? ) The only way I can think of to do this is to make 2 nested for-loops and comb through each pixel and put it in this format then concat. to an array of arrays and write to the CSV.
Would this slow the collection of pixels values so that it takes longer than the 'update' function? Am I better of doing post-processing on the data to clean it up the way I want?
Thanks in advance for any help suggestions!
Your answer
Follow this Question
Related Questions
rgb value detail 0 Answers
PNG import problem 2019.1.0f2 0 Answers
How to make camera position relative to a specific target. 1 Answer
Convert RGB to RYB color scheme? 0 Answers